api.c 2.6 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582585835858458585585865858758588585895859058591585925859358594585955859658597585985859958600586015860258603586045860558606586075860858609586105861158612586135861458615586165861758618586195862058621586225862358624586255862658627586285862958630586315863258633586345863558636586375863858639586405864158642586435864458645586465864758648586495865058651586525865358654586555865658657586585865958660586615866258663586645866558666586675866858669586705867158672586735867458675586765867758678586795868058681586825868358684586855868658687586885868958690586915869258693586945869558696586975869858699587005870158702587035870458705587065870758708587095871058711587125871358714587155871658717587185871958720587215872258723587245872558726587275872858729587305873158732587335873458735587365873758738587395874058741587425874358744587455874658747587485874958750587515875258753587545875558756587575875858759587605876158762587635876458765587665876758768587695877058771587725877358774587755877658777587785877958780587815878258783587845878558786587875878858789587905879158792587935879458795587965879758798587995880058801588025880358804588055880658807588085880958810588115881258813588145881558816588175881858819588205882158822588235882458825588265882758828588295883058831588325883358834588355883658837588385883958840588415884258843588445884558846588475884858849588505885158852588535885458855588565885758858588595886058861588625886358864588655886658867588685886958870588715887258873588745887558876588775887858879588805888158882588835888458885588865888758888588895889058891588925889358894588955889658897588985889958900589015890258903589045890558906589075890858909589105891158912589135891458915589165891758918589195892058921589225892358924589255892658927589285892958930589315893258933589345893558936589375893858939589405894158942589435894458945589465894758948589495895058951589525895358954589555895658957589585895958960589615896258963589645896558966589675896858969589705897158972589735897458975589765897758978589795898058981589825898358984589855898658987589885898958990589915899258993589945899558996589975899858999590005900159002590035900459005590065900759008590095901059011590125901359014590155901659017590185901959020590215902259023590245902559026590275902859029590305903159032590335903459035590365903759038590395904059041590425904359044590455904659047590485904959050590515905259053590545905559056590575905859059590605906159062590635906459065590665906759068590695907059071590725907359074590755907659077590785907959080590815908259083590845908559086590875908859089590905909159092590935909459095590965909759098590995910059101591025910359104591055910659107591085910959110591115911259113591145911559116591175911859119591205912159122591235912459125591265912759128591295913059131591325913359134591355913659137591385913959140591415914259143591445914559146591475914859149591505915159152591535915459155591565915759158591595916059161591625916359164591655916659167591685916959170591715917259173591745917559176591775917859179591805918159182591835918459185591865918759188591895919059191591925919359194591955919659197591985919959200592015920259203592045920559206592075920859209592105921159212592135921459215592165921759218592195922059221592225922359224592255922659227592285922959230592315923259233592345923559236592375923859239592405924159242592435924459245592465924759248592495925059251592525925359254592555925659257592585925959260592615926259263592645926559266592675926859269592705927159272592735927459275592765927759278592795928059281592825928359284592855928659287592885928959290592915929259293592945929559296592975929859299593005930159302593035930459305593065930759308593095931059311593125931359314593155931659317593185931959320593215932259323593245932559326593275932859329593305933159332593335933459335593365933759338593395934059341593425934359344593455934659347593485934959350593515935259353593545935559356593575935859359593605936159362593635936459365593665936759368593695937059371593725937359374593755937659377593785937959380593815938259383593845938559386593875938859389593905939159392593935939459395593965939759398593995940059401594025940359404594055940659407594085940959410594115941259413594145941559416594175941859419594205942159422594235942459425594265942759428594295943059431594325943359434594355943659437594385943959440594415944259443594445944559446594475944859449594505945159452594535945459455594565945759458594595946059461594625946359464594655946659467594685946959470594715947259473594745947559476594775947859479594805948159482594835948459485594865948759488594895949059491594925949359494594955949659497594985949959500595015950259503595045950559506595075950859509595105951159512595135951459515595165951759518595195952059521595225952359524595255952659527595285952959530595315953259533595345953559536595375953859539595405954159542595435954459545595465954759548595495955059551595525955359554595555955659557595585955959560595615956259563595645956559566595675956859569595705957159572595735957459575595765957759578595795958059581595825958359584595855958659587595885958959590595915959259593595945959559596595975959859599596005960159602596035960459605596065960759608596095961059611596125961359614596155961659617596185961959620596215962259623596245962559626596275962859629596305963159632596335963459635596365963759638596395964059641596425964359644596455964659647596485964959650596515965259653596545965559656596575965859659596605966159662596635966459665596665966759668596695967059671596725967359674596755967659677596785967959680596815968259683596845968559686596875968859689596905969159692596935969459695596965969759698596995970059701597025970359704597055970659707597085970959710597115971259713597145971559716597175971859719597205972159722597235972459725597265972759728597295973059731597325973359734597355973659737597385973959740597415974259743597445974559746597475974859749597505975159752597535975459755597565975759758597595976059761597625976359764597655976659767597685976959770597715977259773597745977559776597775977859779597805978159782597835978459785597865978759788597895979059791597925979359794597955979659797597985979959800598015980259803598045980559806598075980859809598105981159812598135981459815598165981759818598195982059821598225982359824598255982659827598285982959830598315983259833598345983559836598375983859839598405984159842598435984459845598465984759848598495985059851598525985359854598555985659857598585985959860598615986259863598645986559866598675986859869598705987159872598735987459875598765987759878598795988059881598825988359884598855988659887598885988959890598915989259893598945989559896598975989859899599005990159902599035990459905599065990759908599095991059911599125991359914599155991659917599185991959920599215992259923599245992559926599275992859929599305993159932599335993459935599365993759938599395994059941599425994359944599455994659947599485994959950599515995259953599545995559956599575995859959599605996159962599635996459965599665996759968599695997059971599725997359974599755997659977599785997959980599815998259983599845998559986599875998859989599905999159992599935999459995599965999759998599996000060001600026000360004600056000660007600086000960010600116001260013600146001560016600176001860019600206002160022600236002460025600266002760028600296003060031600326003360034600356003660037600386003960040600416004260043600446004560046600476004860049600506005160052600536005460055600566005760058600596006060061600626006360064600656006660067600686006960070600716007260073600746007560076600776007860079600806008160082600836008460085600866008760088600896009060091600926009360094600956009660097600986009960100601016010260103601046010560106601076010860109601106011160112601136011460115601166011760118601196012060121601226012360124601256012660127601286012960130601316013260133601346013560136601376013860139601406014160142601436014460145601466014760148601496015060151601526015360154601556015660157601586015960160601616016260163601646016560166601676016860169601706017160172601736017460175601766017760178601796018060181601826018360184601856018660187601886018960190601916019260193601946019560196601976019860199602006020160202602036020460205602066020760208602096021060211602126021360214602156021660217602186021960220602216022260223602246022560226602276022860229602306023160232602336023460235602366023760238602396024060241602426024360244602456024660247602486024960250602516025260253602546025560256602576025860259602606026160262602636026460265602666026760268602696027060271602726027360274602756027660277602786027960280602816028260283602846028560286602876028860289602906029160292602936029460295602966029760298602996030060301603026030360304603056030660307603086030960310603116031260313603146031560316603176031860319603206032160322603236032460325603266032760328603296033060331603326033360334603356033660337603386033960340603416034260343603446034560346603476034860349603506035160352603536035460355603566035760358603596036060361603626036360364603656036660367603686036960370603716037260373603746037560376603776037860379603806038160382603836038460385603866038760388603896039060391603926039360394603956039660397603986039960400604016040260403604046040560406604076040860409604106041160412604136041460415604166041760418604196042060421604226042360424604256042660427604286042960430604316043260433604346043560436604376043860439604406044160442604436044460445604466044760448604496045060451604526045360454604556045660457604586045960460604616046260463604646046560466604676046860469604706047160472604736047460475604766047760478604796048060481604826048360484604856048660487604886048960490604916049260493604946049560496604976049860499605006050160502605036050460505605066050760508605096051060511605126051360514605156051660517605186051960520605216052260523605246052560526605276052860529605306053160532605336053460535605366053760538605396054060541605426054360544605456054660547605486054960550605516055260553605546055560556605576055860559605606056160562605636056460565605666056760568605696057060571605726057360574605756057660577605786057960580605816058260583605846058560586605876058860589605906059160592605936059460595605966059760598605996060060601606026060360604606056060660607606086060960610606116061260613606146061560616606176061860619606206062160622606236062460625606266062760628606296063060631606326063360634606356063660637606386063960640606416064260643606446064560646606476064860649606506065160652606536065460655606566065760658606596066060661606626066360664606656066660667606686066960670606716067260673606746067560676606776067860679606806068160682606836068460685606866068760688606896069060691606926069360694606956069660697606986069960700607016070260703607046070560706607076070860709607106071160712607136071460715607166071760718607196072060721607226072360724607256072660727607286072960730607316073260733607346073560736607376073860739607406074160742607436074460745607466074760748607496075060751607526075360754607556075660757607586075960760607616076260763607646076560766607676076860769607706077160772607736077460775607766077760778607796078060781607826078360784607856078660787607886078960790607916079260793607946079560796607976079860799608006080160802608036080460805608066080760808608096081060811608126081360814608156081660817608186081960820608216082260823608246082560826608276082860829608306083160832608336083460835608366083760838608396084060841608426084360844608456084660847608486084960850608516085260853608546085560856608576085860859608606086160862608636086460865608666086760868608696087060871608726087360874608756087660877608786087960880608816088260883608846088560886608876088860889608906089160892608936089460895608966089760898608996090060901609026090360904609056090660907609086090960910609116091260913609146091560916609176091860919609206092160922609236092460925609266092760928609296093060931609326093360934609356093660937609386093960940609416094260943609446094560946609476094860949609506095160952609536095460955609566095760958609596096060961609626096360964609656096660967609686096960970609716097260973609746097560976609776097860979609806098160982609836098460985609866098760988609896099060991609926099360994609956099660997609986099961000610016100261003610046100561006610076100861009610106101161012610136101461015610166101761018610196102061021610226102361024610256102661027610286102961030610316103261033610346103561036610376103861039610406104161042610436104461045610466104761048610496105061051610526105361054610556105661057610586105961060610616106261063610646106561066610676106861069610706107161072610736107461075610766107761078610796108061081610826108361084610856108661087610886108961090610916109261093610946109561096610976109861099611006110161102611036110461105611066110761108611096111061111611126111361114611156111661117611186111961120611216112261123611246112561126611276112861129611306113161132611336113461135611366113761138611396114061141611426114361144611456114661147611486114961150611516115261153611546115561156611576115861159611606116161162611636116461165611666116761168611696117061171611726117361174611756117661177611786117961180611816118261183611846118561186611876118861189611906119161192611936119461195611966119761198611996120061201612026120361204612056120661207612086120961210612116121261213612146121561216612176121861219612206122161222612236122461225612266122761228612296123061231612326123361234612356123661237612386123961240612416124261243612446124561246612476124861249612506125161252612536125461255612566125761258612596126061261612626126361264612656126661267612686126961270612716127261273612746127561276612776127861279612806128161282612836128461285612866128761288612896129061291612926129361294612956129661297612986129961300613016130261303613046130561306613076130861309613106131161312613136131461315613166131761318613196132061321613226132361324613256132661327613286132961330613316133261333613346133561336613376133861339613406134161342613436134461345613466134761348613496135061351613526135361354613556135661357613586135961360613616136261363613646136561366613676136861369613706137161372613736137461375613766137761378613796138061381613826138361384613856138661387613886138961390613916139261393613946139561396613976139861399614006140161402614036140461405614066140761408614096141061411614126141361414614156141661417614186141961420614216142261423614246142561426614276142861429614306143161432614336143461435614366143761438614396144061441614426144361444614456144661447614486144961450614516145261453614546145561456614576145861459614606146161462614636146461465614666146761468614696147061471614726147361474614756147661477614786147961480614816148261483614846148561486614876148861489614906149161492614936149461495614966149761498614996150061501615026150361504615056150661507615086150961510615116151261513615146151561516615176151861519615206152161522615236152461525615266152761528615296153061531615326153361534615356153661537615386153961540615416154261543615446154561546615476154861549615506155161552615536155461555615566155761558615596156061561615626156361564615656156661567615686156961570615716157261573615746157561576615776157861579615806158161582615836158461585615866158761588615896159061591615926159361594615956159661597615986159961600616016160261603616046160561606616076160861609616106161161612616136161461615616166161761618616196162061621616226162361624616256162661627616286162961630616316163261633616346163561636616376163861639616406164161642616436164461645616466164761648616496165061651616526165361654616556165661657616586165961660616616166261663616646166561666616676166861669616706167161672616736167461675616766167761678616796168061681616826168361684616856168661687616886168961690616916169261693616946169561696616976169861699617006170161702617036170461705617066170761708617096171061711617126171361714617156171661717617186171961720617216172261723617246172561726617276172861729617306173161732617336173461735617366173761738617396174061741617426174361744617456174661747617486174961750617516175261753617546175561756617576175861759617606176161762617636176461765617666176761768617696177061771617726177361774617756177661777617786177961780617816178261783617846178561786617876178861789617906179161792617936179461795617966179761798617996180061801618026180361804618056180661807618086180961810618116181261813618146181561816618176181861819618206182161822618236182461825618266182761828618296183061831618326183361834618356183661837618386183961840618416184261843618446184561846618476184861849618506185161852618536185461855618566185761858618596186061861618626186361864618656186661867618686186961870618716187261873618746187561876618776187861879618806188161882618836188461885618866188761888618896189061891618926189361894618956189661897618986189961900619016190261903619046190561906619076190861909619106191161912619136191461915619166191761918619196192061921619226192361924619256192661927619286192961930619316193261933619346193561936619376193861939619406194161942619436194461945619466194761948619496195061951619526195361954619556195661957619586195961960619616196261963619646196561966619676196861969619706197161972619736197461975619766197761978619796198061981619826198361984619856198661987619886198961990619916199261993619946199561996619976199861999620006200162002620036200462005620066200762008620096201062011620126201362014620156201662017620186201962020620216202262023620246202562026620276202862029620306203162032620336203462035620366203762038620396204062041620426204362044620456204662047620486204962050620516205262053620546205562056620576205862059620606206162062620636206462065620666206762068620696207062071620726207362074620756207662077620786207962080620816208262083620846208562086620876208862089620906209162092620936209462095620966209762098620996210062101621026210362104621056210662107621086210962110621116211262113621146211562116621176211862119621206212162122621236212462125621266212762128621296213062131621326213362134621356213662137621386213962140621416214262143621446214562146621476214862149621506215162152621536215462155621566215762158621596216062161621626216362164621656216662167621686216962170621716217262173621746217562176621776217862179621806218162182621836218462185621866218762188621896219062191621926219362194621956219662197621986219962200622016220262203622046220562206622076220862209622106221162212622136221462215622166221762218622196222062221622226222362224622256222662227622286222962230622316223262233622346223562236622376223862239622406224162242622436224462245622466224762248622496225062251622526225362254622556225662257622586225962260622616226262263622646226562266622676226862269622706227162272622736227462275622766227762278622796228062281622826228362284622856228662287622886228962290622916229262293622946229562296622976229862299623006230162302623036230462305623066230762308623096231062311623126231362314623156231662317623186231962320623216232262323623246232562326623276232862329623306233162332623336233462335623366233762338623396234062341623426234362344623456234662347623486234962350623516235262353623546235562356623576235862359623606236162362623636236462365623666236762368623696237062371623726237362374623756237662377623786237962380623816238262383623846238562386623876238862389623906239162392623936239462395623966239762398623996240062401624026240362404624056240662407624086240962410624116241262413624146241562416624176241862419624206242162422624236242462425624266242762428624296243062431624326243362434624356243662437624386243962440624416244262443624446244562446624476244862449624506245162452624536245462455624566245762458624596246062461624626246362464624656246662467624686246962470624716247262473624746247562476624776247862479624806248162482624836248462485624866248762488624896249062491624926249362494624956249662497624986249962500625016250262503625046250562506625076250862509625106251162512625136251462515625166251762518625196252062521625226252362524625256252662527625286252962530625316253262533625346253562536625376253862539625406254162542625436254462545625466254762548625496255062551625526255362554625556255662557625586255962560625616256262563625646256562566625676256862569625706257162572625736257462575625766257762578625796258062581625826258362584625856258662587625886258962590625916259262593625946259562596625976259862599626006260162602626036260462605626066260762608626096261062611626126261362614626156261662617626186261962620626216262262623626246262562626626276262862629626306263162632626336263462635626366263762638626396264062641626426264362644626456264662647626486264962650626516265262653626546265562656626576265862659626606266162662626636266462665626666266762668626696267062671626726267362674626756267662677626786267962680626816268262683626846268562686626876268862689626906269162692626936269462695626966269762698626996270062701627026270362704627056270662707627086270962710627116271262713627146271562716627176271862719627206272162722627236272462725627266272762728627296273062731627326273362734627356273662737627386273962740627416274262743627446274562746627476274862749627506275162752627536275462755627566275762758627596276062761627626276362764627656276662767627686276962770627716277262773627746277562776627776277862779627806278162782627836278462785627866278762788627896279062791627926279362794627956279662797627986279962800628016280262803628046280562806628076280862809628106281162812628136281462815628166281762818628196282062821628226282362824628256282662827628286282962830628316283262833628346283562836628376283862839628406284162842628436284462845628466284762848628496285062851628526285362854628556285662857628586285962860628616286262863628646286562866628676286862869628706287162872628736287462875628766287762878628796288062881628826288362884628856288662887628886288962890628916289262893628946289562896628976289862899629006290162902629036290462905629066290762908629096291062911629126291362914629156291662917629186291962920629216292262923629246292562926629276292862929629306293162932629336293462935629366293762938629396294062941629426294362944629456294662947629486294962950629516295262953629546295562956629576295862959629606296162962629636296462965629666296762968629696297062971629726297362974629756297662977629786297962980629816298262983629846298562986629876298862989629906299162992629936299462995629966299762998629996300063001630026300363004630056300663007630086300963010630116301263013630146301563016630176301863019630206302163022630236302463025630266302763028630296303063031630326303363034630356303663037630386303963040630416304263043630446304563046630476304863049630506305163052630536305463055630566305763058630596306063061630626306363064630656306663067630686306963070630716307263073630746307563076630776307863079630806308163082630836308463085630866308763088630896309063091630926309363094630956309663097630986309963100631016310263103631046310563106631076310863109631106311163112631136311463115631166311763118631196312063121631226312363124631256312663127631286312963130631316313263133631346313563136631376313863139631406314163142631436314463145631466314763148631496315063151631526315363154631556315663157631586315963160631616316263163631646316563166631676316863169631706317163172631736317463175631766317763178631796318063181631826318363184631856318663187631886318963190631916319263193631946319563196631976319863199632006320163202632036320463205632066320763208632096321063211632126321363214632156321663217632186321963220632216322263223632246322563226632276322863229632306323163232632336323463235632366323763238632396324063241632426324363244632456324663247632486324963250632516325263253632546325563256632576325863259632606326163262632636326463265632666326763268632696327063271632726327363274632756327663277632786327963280632816328263283632846328563286632876328863289632906329163292632936329463295632966329763298632996330063301633026330363304633056330663307633086330963310633116331263313633146331563316633176331863319633206332163322633236332463325633266332763328633296333063331633326333363334633356333663337633386333963340633416334263343633446334563346633476334863349633506335163352633536335463355633566335763358633596336063361633626336363364633656336663367633686336963370633716337263373633746337563376633776337863379633806338163382633836338463385633866338763388633896339063391633926339363394633956339663397633986339963400634016340263403634046340563406634076340863409634106341163412634136341463415634166341763418634196342063421634226342363424634256342663427634286342963430634316343263433634346343563436634376343863439634406344163442634436344463445634466344763448634496345063451634526345363454634556345663457634586345963460634616346263463634646346563466634676346863469634706347163472634736347463475634766347763478634796348063481634826348363484634856348663487634886348963490634916349263493634946349563496634976349863499635006350163502635036350463505635066350763508635096351063511635126351363514635156351663517635186351963520635216352263523635246352563526635276352863529635306353163532635336353463535635366353763538635396354063541635426354363544635456354663547635486354963550635516355263553635546355563556635576355863559635606356163562635636356463565635666356763568635696357063571635726357363574635756357663577635786357963580635816358263583635846358563586635876358863589635906359163592635936359463595635966359763598635996360063601636026360363604636056360663607636086360963610636116361263613636146361563616636176361863619636206362163622636236362463625636266362763628636296363063631636326363363634636356363663637636386363963640636416364263643636446364563646636476364863649636506365163652636536365463655636566365763658636596366063661636626366363664636656366663667636686366963670636716367263673636746367563676636776367863679636806368163682636836368463685636866368763688636896369063691636926369363694636956369663697636986369963700637016370263703637046370563706637076370863709637106371163712637136371463715637166371763718637196372063721637226372363724637256372663727637286372963730637316373263733637346373563736637376373863739637406374163742637436374463745637466374763748637496375063751637526375363754637556375663757637586375963760637616376263763637646376563766637676376863769637706377163772637736377463775637766377763778637796378063781637826378363784637856378663787637886378963790637916379263793637946379563796637976379863799638006380163802638036380463805638066380763808638096381063811638126381363814638156381663817638186381963820638216382263823638246382563826638276382863829638306383163832638336383463835638366383763838638396384063841638426384363844638456384663847638486384963850638516385263853638546385563856638576385863859638606386163862638636386463865638666386763868638696387063871638726387363874638756387663877638786387963880638816388263883638846388563886638876388863889638906389163892638936389463895638966389763898638996390063901639026390363904639056390663907639086390963910639116391263913639146391563916639176391863919639206392163922639236392463925639266392763928639296393063931639326393363934639356393663937639386393963940639416394263943639446394563946639476394863949639506395163952639536395463955639566395763958639596396063961639626396363964639656396663967639686396963970639716397263973639746397563976639776397863979639806398163982639836398463985639866398763988639896399063991639926399363994639956399663997639986399964000640016400264003640046400564006640076400864009640106401164012640136401464015640166401764018640196402064021640226402364024640256402664027640286402964030640316403264033640346403564036640376403864039640406404164042640436404464045640466404764048640496405064051640526405364054640556405664057640586405964060640616406264063640646406564066640676406864069640706407164072640736407464075640766407764078640796408064081640826408364084640856408664087640886408964090640916409264093640946409564096640976409864099641006410164102641036410464105641066410764108641096411064111641126411364114641156411664117641186411964120641216412264123641246412564126641276412864129641306413164132641336413464135641366413764138641396414064141641426414364144641456414664147641486414964150641516415264153641546415564156641576415864159641606416164162641636416464165641666416764168641696417064171641726417364174641756417664177641786417964180641816418264183641846418564186641876418864189641906419164192641936419464195641966419764198641996420064201642026420364204642056420664207642086420964210642116421264213642146421564216642176421864219642206422164222642236422464225642266422764228642296423064231642326423364234642356423664237642386423964240642416424264243642446424564246642476424864249642506425164252642536425464255642566425764258642596426064261642626426364264642656426664267642686426964270642716427264273642746427564276642776427864279642806428164282642836428464285642866428764288642896429064291642926429364294642956429664297642986429964300643016430264303643046430564306643076430864309643106431164312643136431464315643166431764318643196432064321643226432364324643256432664327643286432964330643316433264333643346433564336643376433864339643406434164342643436434464345643466434764348643496435064351643526435364354643556435664357643586435964360643616436264363643646436564366643676436864369643706437164372643736437464375643766437764378643796438064381643826438364384643856438664387643886438964390643916439264393643946439564396643976439864399644006440164402644036440464405644066440764408644096441064411644126441364414644156441664417644186441964420644216442264423644246442564426644276442864429644306443164432644336443464435644366443764438644396444064441644426444364444644456444664447644486444964450644516445264453644546445564456644576445864459644606446164462644636446464465644666446764468644696447064471644726447364474644756447664477644786447964480644816448264483644846448564486644876448864489644906449164492644936449464495644966449764498644996450064501645026450364504645056450664507645086450964510645116451264513645146451564516645176451864519645206452164522645236452464525645266452764528645296453064531645326453364534645356453664537645386453964540645416454264543645446454564546645476454864549645506455164552645536455464555645566455764558645596456064561645626456364564645656456664567645686456964570645716457264573645746457564576645776457864579645806458164582645836458464585645866458764588645896459064591645926459364594645956459664597645986459964600646016460264603646046460564606646076460864609646106461164612646136461464615646166461764618646196462064621646226462364624646256462664627646286462964630646316463264633646346463564636646376463864639646406464164642646436464464645646466464764648646496465064651646526465364654646556465664657646586465964660646616466264663646646466564666646676466864669646706467164672646736467464675646766467764678646796468064681646826468364684646856468664687646886468964690646916469264693646946469564696646976469864699647006470164702647036470464705647066470764708647096471064711647126471364714647156471664717647186471964720647216472264723647246472564726647276472864729647306473164732647336473464735647366473764738647396474064741647426474364744647456474664747647486474964750647516475264753647546475564756647576475864759647606476164762647636476464765647666476764768647696477064771647726477364774647756477664777647786477964780647816478264783647846478564786647876478864789647906479164792647936479464795647966479764798647996480064801648026480364804648056480664807648086480964810648116481264813648146481564816648176481864819648206482164822648236482464825648266482764828648296483064831648326483364834648356483664837648386483964840648416484264843648446484564846648476484864849648506485164852648536485464855648566485764858648596486064861648626486364864648656486664867648686486964870648716487264873648746487564876648776487864879648806488164882648836488464885648866488764888648896489064891648926489364894648956489664897648986489964900649016490264903649046490564906649076490864909649106491164912649136491464915649166491764918649196492064921649226492364924649256492664927649286492964930649316493264933649346493564936649376493864939649406494164942649436494464945649466494764948649496495064951649526495364954649556495664957649586495964960649616496264963649646496564966649676496864969649706497164972649736497464975649766497764978649796498064981649826498364984649856498664987649886498964990649916499264993649946499564996649976499864999650006500165002650036500465005650066500765008650096501065011650126501365014650156501665017650186501965020650216502265023650246502565026650276502865029650306503165032650336503465035650366503765038650396504065041650426504365044650456504665047650486504965050650516505265053650546505565056650576505865059650606506165062650636506465065650666506765068650696507065071650726507365074650756507665077650786507965080650816508265083650846508565086650876508865089650906509165092650936509465095650966509765098650996510065101651026510365104651056510665107651086510965110651116511265113651146511565116651176511865119651206512165122651236512465125651266512765128651296513065131651326513365134651356513665137651386513965140651416514265143651446514565146651476514865149651506515165152651536515465155651566515765158651596516065161651626516365164651656516665167651686516965170651716517265173651746517565176651776517865179651806518165182651836518465185651866518765188651896519065191651926519365194651956519665197651986519965200652016520265203652046520565206652076520865209652106521165212652136521465215652166521765218652196522065221652226522365224652256522665227652286522965230652316523265233652346523565236652376523865239652406524165242652436524465245652466524765248652496525065251652526525365254652556525665257652586525965260652616526265263652646526565266652676526865269652706527165272652736527465275652766527765278652796528065281652826528365284652856528665287652886528965290652916529265293652946529565296652976529865299653006530165302653036530465305653066530765308653096531065311653126531365314653156531665317653186531965320653216532265323653246532565326653276532865329653306533165332653336533465335653366533765338653396534065341653426534365344653456534665347653486534965350653516535265353653546535565356653576535865359653606536165362653636536465365653666536765368653696537065371653726537365374653756537665377653786537965380653816538265383653846538565386653876538865389653906539165392653936539465395653966539765398653996540065401654026540365404654056540665407654086540965410654116541265413654146541565416654176541865419654206542165422654236542465425654266542765428654296543065431654326543365434654356543665437654386543965440654416544265443654446544565446654476544865449654506545165452654536545465455654566545765458654596546065461654626546365464654656546665467654686546965470654716547265473654746547565476654776547865479654806548165482654836548465485654866548765488654896549065491654926549365494654956549665497654986549965500655016550265503655046550565506655076550865509655106551165512655136551465515655166551765518655196552065521655226552365524655256552665527655286552965530655316553265533655346553565536655376553865539655406554165542655436554465545655466554765548655496555065551655526555365554655556555665557655586555965560655616556265563655646556565566655676556865569655706557165572655736557465575655766557765578655796558065581655826558365584655856558665587655886558965590655916559265593655946559565596655976559865599656006560165602656036560465605656066560765608656096561065611656126561365614656156561665617656186561965620656216562265623656246562565626656276562865629656306563165632656336563465635656366563765638656396564065641656426564365644656456564665647656486564965650656516565265653656546565565656656576565865659656606566165662656636566465665656666566765668656696567065671656726567365674656756567665677656786567965680656816568265683656846568565686656876568865689656906569165692656936569465695656966569765698656996570065701657026570365704657056570665707657086570965710657116571265713657146571565716657176571865719657206572165722657236572465725657266572765728657296573065731657326573365734657356573665737657386573965740657416574265743657446574565746657476574865749657506575165752657536575465755657566575765758657596576065761657626576365764657656576665767657686576965770657716577265773657746577565776657776577865779657806578165782657836578465785657866578765788657896579065791657926579365794657956579665797657986579965800658016580265803658046580565806658076580865809658106581165812658136581465815658166581765818658196582065821658226582365824658256582665827658286582965830658316583265833658346583565836658376583865839658406584165842658436584465845658466584765848658496585065851658526585365854658556585665857658586585965860658616586265863658646586565866658676586865869658706587165872658736587465875658766587765878658796588065881658826588365884658856588665887658886588965890658916589265893658946589565896658976589865899659006590165902659036590465905659066590765908659096591065911659126591365914659156591665917659186591965920659216592265923659246592565926659276592865929659306593165932659336593465935659366593765938659396594065941659426594365944659456594665947659486594965950659516595265953659546595565956659576595865959659606596165962659636596465965659666596765968659696597065971659726597365974659756597665977659786597965980659816598265983659846598565986659876598865989659906599165992659936599465995659966599765998659996600066001660026600366004660056600666007660086600966010660116601266013660146601566016660176601866019660206602166022660236602466025660266602766028660296603066031660326603366034660356603666037660386603966040660416604266043660446604566046660476604866049660506605166052660536605466055660566605766058660596606066061660626606366064660656606666067660686606966070660716607266073660746607566076660776607866079660806608166082660836608466085660866608766088660896609066091660926609366094660956609666097660986609966100661016610266103661046610566106661076610866109661106611166112661136611466115661166611766118661196612066121661226612366124661256612666127661286612966130661316613266133661346613566136661376613866139661406614166142661436614466145661466614766148661496615066151661526615366154661556615666157661586615966160661616616266163661646616566166661676616866169661706617166172661736617466175661766617766178661796618066181661826618366184661856618666187661886618966190661916619266193661946619566196661976619866199662006620166202662036620466205662066620766208662096621066211662126621366214662156621666217662186621966220662216622266223662246622566226662276622866229662306623166232662336623466235662366623766238662396624066241662426624366244662456624666247662486624966250662516625266253662546625566256662576625866259662606626166262662636626466265662666626766268662696627066271662726627366274662756627666277662786627966280662816628266283662846628566286662876628866289662906629166292662936629466295662966629766298662996630066301663026630366304663056630666307663086630966310663116631266313663146631566316663176631866319663206632166322663236632466325663266632766328663296633066331663326633366334663356633666337663386633966340663416634266343663446634566346663476634866349663506635166352663536635466355663566635766358663596636066361663626636366364663656636666367663686636966370663716637266373663746637566376663776637866379663806638166382663836638466385663866638766388663896639066391663926639366394663956639666397663986639966400664016640266403664046640566406664076640866409664106641166412664136641466415664166641766418664196642066421664226642366424664256642666427664286642966430664316643266433664346643566436664376643866439664406644166442664436644466445664466644766448664496645066451664526645366454664556645666457664586645966460664616646266463664646646566466664676646866469664706647166472664736647466475664766647766478664796648066481664826648366484664856648666487664886648966490664916649266493664946649566496664976649866499665006650166502665036650466505665066650766508665096651066511665126651366514665156651666517665186651966520665216652266523665246652566526665276652866529665306653166532665336653466535665366653766538665396654066541665426654366544665456654666547665486654966550665516655266553665546655566556665576655866559665606656166562665636656466565665666656766568665696657066571665726657366574665756657666577665786657966580665816658266583665846658566586665876658866589665906659166592665936659466595665966659766598665996660066601666026660366604666056660666607666086660966610666116661266613666146661566616666176661866619666206662166622666236662466625666266662766628666296663066631666326663366634666356663666637666386663966640666416664266643666446664566646666476664866649666506665166652666536665466655666566665766658666596666066661666626666366664666656666666667666686666966670666716667266673666746667566676666776667866679666806668166682666836668466685666866668766688666896669066691666926669366694666956669666697666986669966700667016670266703667046670566706667076670866709667106671166712667136671466715667166671766718667196672066721667226672366724667256672666727667286672966730667316673266733667346673566736667376673866739667406674166742667436674466745667466674766748667496675066751667526675366754667556675666757667586675966760667616676266763667646676566766667676676866769667706677166772667736677466775667766677766778667796678066781667826678366784667856678666787667886678966790667916679266793667946679566796667976679866799668006680166802668036680466805668066680766808668096681066811668126681366814668156681666817668186681966820668216682266823668246682566826668276682866829668306683166832668336683466835668366683766838668396684066841668426684366844668456684666847668486684966850668516685266853668546685566856668576685866859668606686166862668636686466865668666686766868668696687066871668726687366874668756687666877668786687966880668816688266883668846688566886668876688866889668906689166892668936689466895668966689766898668996690066901669026690366904669056690666907669086690966910669116691266913669146691566916669176691866919669206692166922669236692466925669266692766928669296693066931669326693366934669356693666937669386693966940669416694266943669446694566946669476694866949669506695166952669536695466955669566695766958669596696066961669626696366964669656696666967669686696966970669716697266973669746697566976669776697866979669806698166982669836698466985669866698766988669896699066991669926699366994669956699666997669986699967000670016700267003670046700567006670076700867009670106701167012670136701467015670166701767018670196702067021670226702367024670256702667027670286702967030670316703267033670346703567036670376703867039670406704167042670436704467045670466704767048670496705067051670526705367054670556705667057670586705967060670616706267063670646706567066670676706867069670706707167072670736707467075670766707767078670796708067081670826708367084670856708667087670886708967090670916709267093670946709567096670976709867099671006710167102671036710467105671066710767108671096711067111671126711367114671156711667117671186711967120671216712267123671246712567126671276712867129671306713167132671336713467135671366713767138671396714067141671426714367144671456714667147671486714967150671516715267153671546715567156671576715867159671606716167162671636716467165671666716767168671696717067171671726717367174671756717667177671786717967180671816718267183671846718567186671876718867189671906719167192671936719467195671966719767198671996720067201672026720367204672056720667207672086720967210672116721267213672146721567216672176721867219672206722167222672236722467225672266722767228672296723067231672326723367234672356723667237672386723967240672416724267243672446724567246672476724867249672506725167252672536725467255672566725767258672596726067261672626726367264672656726667267672686726967270672716727267273672746727567276672776727867279672806728167282672836728467285672866728767288672896729067291672926729367294672956729667297672986729967300673016730267303673046730567306673076730867309673106731167312673136731467315673166731767318673196732067321673226732367324673256732667327673286732967330673316733267333673346733567336673376733867339673406734167342673436734467345673466734767348673496735067351673526735367354673556735667357673586735967360673616736267363673646736567366673676736867369673706737167372673736737467375673766737767378673796738067381673826738367384673856738667387673886738967390673916739267393673946739567396673976739867399674006740167402674036740467405674066740767408674096741067411674126741367414674156741667417674186741967420674216742267423674246742567426674276742867429674306743167432674336743467435674366743767438674396744067441674426744367444674456744667447674486744967450674516745267453674546745567456674576745867459674606746167462674636746467465674666746767468674696747067471674726747367474674756747667477674786747967480674816748267483674846748567486674876748867489674906749167492674936749467495674966749767498674996750067501675026750367504675056750667507675086750967510675116751267513675146751567516675176751867519675206752167522675236752467525675266752767528675296753067531675326753367534675356753667537675386753967540675416754267543675446754567546675476754867549675506755167552675536755467555675566755767558675596756067561675626756367564675656756667567675686756967570675716757267573675746757567576675776757867579675806758167582675836758467585675866758767588675896759067591675926759367594675956759667597675986759967600676016760267603676046760567606676076760867609676106761167612676136761467615676166761767618676196762067621676226762367624676256762667627676286762967630676316763267633676346763567636676376763867639676406764167642676436764467645676466764767648676496765067651676526765367654676556765667657676586765967660676616766267663676646766567666676676766867669676706767167672676736767467675676766767767678676796768067681676826768367684676856768667687676886768967690676916769267693676946769567696676976769867699677006770167702677036770467705677066770767708677096771067711677126771367714677156771667717677186771967720677216772267723677246772567726677276772867729677306773167732677336773467735677366773767738677396774067741677426774367744677456774667747677486774967750677516775267753677546775567756677576775867759677606776167762677636776467765677666776767768677696777067771677726777367774677756777667777677786777967780677816778267783677846778567786677876778867789677906779167792677936779467795677966779767798677996780067801678026780367804678056780667807678086780967810678116781267813678146781567816678176781867819678206782167822678236782467825678266782767828678296783067831678326783367834678356783667837678386783967840678416784267843678446784567846678476784867849678506785167852678536785467855678566785767858678596786067861678626786367864678656786667867678686786967870678716787267873678746787567876678776787867879678806788167882678836788467885678866788767888678896789067891678926789367894678956789667897678986789967900679016790267903679046790567906679076790867909679106791167912679136791467915679166791767918679196792067921679226792367924679256792667927679286792967930679316793267933679346793567936679376793867939679406794167942679436794467945679466794767948679496795067951679526795367954679556795667957679586795967960679616796267963679646796567966679676796867969679706797167972679736797467975679766797767978679796798067981679826798367984679856798667987679886798967990679916799267993679946799567996679976799867999680006800168002680036800468005680066800768008680096801068011680126801368014680156801668017680186801968020680216802268023680246802568026680276802868029680306803168032680336803468035680366803768038680396804068041680426804368044680456804668047680486804968050680516805268053680546805568056680576805868059680606806168062680636806468065680666806768068680696807068071680726807368074680756807668077680786807968080680816808268083680846808568086680876808868089680906809168092680936809468095680966809768098680996810068101681026810368104681056810668107681086810968110681116811268113681146811568116681176811868119681206812168122681236812468125681266812768128681296813068131681326813368134681356813668137681386813968140681416814268143681446814568146681476814868149681506815168152681536815468155681566815768158681596816068161681626816368164681656816668167681686816968170681716817268173681746817568176681776817868179681806818168182681836818468185681866818768188681896819068191681926819368194681956819668197681986819968200682016820268203682046820568206682076820868209682106821168212682136821468215682166821768218682196822068221682226822368224682256822668227682286822968230682316823268233682346823568236682376823868239682406824168242682436824468245682466824768248682496825068251682526825368254682556825668257682586825968260682616826268263682646826568266682676826868269682706827168272682736827468275682766827768278682796828068281682826828368284682856828668287682886828968290682916829268293682946829568296682976829868299683006830168302683036830468305683066830768308683096831068311683126831368314683156831668317683186831968320683216832268323683246832568326683276832868329683306833168332683336833468335683366833768338683396834068341683426834368344683456834668347683486834968350683516835268353683546835568356683576835868359683606836168362683636836468365683666836768368683696837068371683726837368374683756837668377683786837968380683816838268383683846838568386683876838868389683906839168392683936839468395683966839768398683996840068401684026840368404684056840668407684086840968410684116841268413684146841568416684176841868419684206842168422684236842468425684266842768428684296843068431684326843368434684356843668437684386843968440684416844268443684446844568446684476844868449684506845168452684536845468455684566845768458684596846068461684626846368464684656846668467684686846968470684716847268473684746847568476684776847868479684806848168482684836848468485684866848768488684896849068491684926849368494684956849668497684986849968500685016850268503685046850568506685076850868509685106851168512685136851468515685166851768518685196852068521685226852368524685256852668527685286852968530685316853268533685346853568536685376853868539685406854168542685436854468545685466854768548685496855068551685526855368554685556855668557685586855968560685616856268563685646856568566685676856868569685706857168572685736857468575685766857768578685796858068581685826858368584685856858668587685886858968590685916859268593685946859568596685976859868599686006860168602686036860468605686066860768608686096861068611686126861368614686156861668617686186861968620686216862268623686246862568626686276862868629686306863168632686336863468635686366863768638686396864068641686426864368644686456864668647686486864968650686516865268653686546865568656686576865868659686606866168662686636866468665686666866768668686696867068671686726867368674686756867668677686786867968680686816868268683686846868568686686876868868689686906869168692686936869468695686966869768698686996870068701687026870368704687056870668707687086870968710687116871268713687146871568716687176871868719687206872168722687236872468725687266872768728687296873068731687326873368734687356873668737687386873968740687416874268743687446874568746687476874868749687506875168752687536875468755687566875768758687596876068761687626876368764687656876668767687686876968770687716877268773687746877568776687776877868779687806878168782687836878468785687866878768788687896879068791687926879368794687956879668797687986879968800688016880268803688046880568806688076880868809688106881168812688136881468815688166881768818688196882068821688226882368824688256882668827688286882968830688316883268833688346883568836688376883868839688406884168842688436884468845688466884768848688496885068851688526885368854688556885668857688586885968860688616886268863688646886568866688676886868869688706887168872688736887468875688766887768878688796888068881688826888368884688856888668887688886888968890688916889268893688946889568896688976889868899689006890168902689036890468905689066890768908689096891068911689126891368914689156891668917689186891968920689216892268923689246892568926689276892868929689306893168932689336893468935689366893768938689396894068941689426894368944689456894668947689486894968950689516895268953689546895568956689576895868959689606896168962689636896468965689666896768968689696897068971689726897368974689756897668977689786897968980689816898268983689846898568986689876898868989689906899168992689936899468995689966899768998689996900069001690026900369004690056900669007690086900969010690116901269013690146901569016690176901869019690206902169022690236902469025690266902769028690296903069031690326903369034690356903669037690386903969040690416904269043690446904569046690476904869049690506905169052690536905469055690566905769058690596906069061690626906369064690656906669067690686906969070690716907269073690746907569076690776907869079690806908169082690836908469085690866908769088690896909069091690926909369094690956909669097690986909969100691016910269103691046910569106691076910869109691106911169112691136911469115691166911769118691196912069121691226912369124691256912669127691286912969130691316913269133691346913569136691376913869139691406914169142691436914469145691466914769148691496915069151691526915369154691556915669157691586915969160691616916269163691646916569166691676916869169691706917169172691736917469175691766917769178691796918069181691826918369184691856918669187691886918969190691916919269193691946919569196691976919869199692006920169202692036920469205692066920769208692096921069211692126921369214692156921669217692186921969220692216922269223692246922569226692276922869229692306923169232692336923469235692366923769238692396924069241692426924369244692456924669247692486924969250692516925269253692546925569256692576925869259692606926169262692636926469265692666926769268692696927069271692726927369274692756927669277692786927969280692816928269283692846928569286692876928869289692906929169292692936929469295692966929769298692996930069301693026930369304693056930669307693086930969310693116931269313693146931569316693176931869319693206932169322693236932469325693266932769328693296933069331693326933369334693356933669337693386933969340693416934269343693446934569346693476934869349693506935169352693536935469355693566935769358693596936069361693626936369364693656936669367693686936969370693716937269373693746937569376693776937869379693806938169382693836938469385693866938769388693896939069391693926939369394693956939669397693986939969400694016940269403694046940569406694076940869409694106941169412694136941469415694166941769418694196942069421694226942369424694256942669427694286942969430694316943269433694346943569436694376943869439694406944169442694436944469445694466944769448694496945069451694526945369454694556945669457694586945969460694616946269463694646946569466694676946869469694706947169472694736947469475694766947769478694796948069481694826948369484694856948669487694886948969490694916949269493694946949569496694976949869499695006950169502695036950469505695066950769508695096951069511695126951369514695156951669517695186951969520695216952269523695246952569526695276952869529695306953169532695336953469535695366953769538695396954069541695426954369544695456954669547695486954969550695516955269553695546955569556695576955869559695606956169562695636956469565695666956769568695696957069571695726957369574695756957669577695786957969580695816958269583695846958569586695876958869589695906959169592695936959469595695966959769598695996960069601696026960369604696056960669607696086960969610696116961269613696146961569616696176961869619696206962169622696236962469625696266962769628696296963069631696326963369634696356963669637696386963969640696416964269643696446964569646696476964869649696506965169652696536965469655696566965769658696596966069661696626966369664696656966669667696686966969670696716967269673696746967569676696776967869679696806968169682696836968469685696866968769688696896969069691696926969369694696956969669697696986969969700697016970269703697046970569706697076970869709697106971169712697136971469715697166971769718697196972069721697226972369724697256972669727697286972969730697316973269733697346973569736697376973869739697406974169742697436974469745697466974769748697496975069751697526975369754697556975669757697586975969760697616976269763697646976569766697676976869769697706977169772697736977469775697766977769778697796978069781697826978369784697856978669787697886978969790697916979269793697946979569796697976979869799698006980169802698036980469805698066980769808698096981069811698126981369814698156981669817698186981969820698216982269823698246982569826698276982869829698306983169832698336983469835698366983769838698396984069841698426984369844698456984669847698486984969850698516985269853698546985569856698576985869859698606986169862698636986469865698666986769868698696987069871698726987369874698756987669877698786987969880698816988269883698846988569886698876988869889698906989169892698936989469895698966989769898698996990069901699026990369904699056990669907699086990969910699116991269913699146991569916699176991869919699206992169922699236992469925699266992769928699296993069931699326993369934699356993669937699386993969940699416994269943699446994569946699476994869949699506995169952699536995469955699566995769958699596996069961699626996369964699656996669967699686996969970699716997269973699746997569976699776997869979699806998169982699836998469985699866998769988699896999069991699926999369994699956999669997699986999970000700017000270003700047000570006700077000870009700107001170012700137001470015700167001770018700197002070021700227002370024700257002670027700287002970030700317003270033700347003570036700377003870039700407004170042700437004470045700467004770048700497005070051700527005370054700557005670057700587005970060700617006270063700647006570066700677006870069700707007170072700737007470075700767007770078700797008070081700827008370084700857008670087700887008970090700917009270093700947009570096700977009870099701007010170102701037010470105701067010770108701097011070111701127011370114701157011670117701187011970120701217012270123701247012570126701277012870129701307013170132701337013470135701367013770138701397014070141701427014370144701457014670147701487014970150701517015270153701547015570156701577015870159701607016170162701637016470165701667016770168701697017070171701727017370174701757017670177701787017970180701817018270183701847018570186701877018870189701907019170192701937019470195701967019770198701997020070201702027020370204702057020670207702087020970210702117021270213702147021570216702177021870219702207022170222702237022470225702267022770228702297023070231702327023370234702357023670237702387023970240702417024270243702447024570246702477024870249702507025170252702537025470255702567025770258702597026070261702627026370264702657026670267702687026970270702717027270273702747027570276702777027870279702807028170282702837028470285702867028770288702897029070291702927029370294702957029670297702987029970300703017030270303703047030570306703077030870309703107031170312703137031470315703167031770318703197032070321703227032370324703257032670327703287032970330703317033270333703347033570336703377033870339703407034170342703437034470345703467034770348703497035070351703527035370354703557035670357703587035970360703617036270363703647036570366703677036870369703707037170372703737037470375703767037770378703797038070381703827038370384703857038670387703887038970390703917039270393703947039570396703977039870399704007040170402704037040470405704067040770408704097041070411704127041370414704157041670417704187041970420704217042270423704247042570426704277042870429704307043170432704337043470435704367043770438704397044070441704427044370444704457044670447704487044970450704517045270453704547045570456704577045870459704607046170462704637046470465704667046770468704697047070471704727047370474704757047670477704787047970480704817048270483704847048570486704877048870489704907049170492704937049470495704967049770498704997050070501705027050370504705057050670507705087050970510705117051270513705147051570516705177051870519705207052170522705237052470525705267052770528705297053070531705327053370534705357053670537705387053970540705417054270543705447054570546705477054870549705507055170552705537055470555705567055770558705597056070561705627056370564705657056670567705687056970570705717057270573705747057570576705777057870579705807058170582705837058470585705867058770588705897059070591705927059370594705957059670597705987059970600706017060270603706047060570606706077060870609706107061170612706137061470615706167061770618706197062070621706227062370624706257062670627706287062970630706317063270633706347063570636706377063870639706407064170642706437064470645706467064770648706497065070651706527065370654706557065670657706587065970660706617066270663706647066570666706677066870669706707067170672706737067470675706767067770678706797068070681706827068370684706857068670687706887068970690706917069270693706947069570696706977069870699707007070170702707037070470705707067070770708707097071070711707127071370714707157071670717707187071970720707217072270723707247072570726707277072870729707307073170732707337073470735707367073770738707397074070741707427074370744707457074670747707487074970750707517075270753707547075570756707577075870759707607076170762707637076470765707667076770768707697077070771707727077370774707757077670777707787077970780707817078270783707847078570786707877078870789707907079170792707937079470795707967079770798707997080070801708027080370804708057080670807708087080970810708117081270813708147081570816708177081870819708207082170822708237082470825708267082770828708297083070831708327083370834708357083670837708387083970840708417084270843708447084570846708477084870849708507085170852708537085470855708567085770858708597086070861708627086370864708657086670867708687086970870708717087270873708747087570876708777087870879708807088170882708837088470885708867088770888708897089070891708927089370894708957089670897708987089970900709017090270903709047090570906709077090870909709107091170912709137091470915709167091770918709197092070921709227092370924709257092670927709287092970930709317093270933709347093570936709377093870939709407094170942709437094470945709467094770948709497095070951709527095370954709557095670957709587095970960709617096270963709647096570966709677096870969709707097170972709737097470975709767097770978709797098070981709827098370984709857098670987709887098970990709917099270993709947099570996709977099870999710007100171002710037100471005710067100771008710097101071011710127101371014710157101671017710187101971020710217102271023710247102571026710277102871029710307103171032710337103471035710367103771038710397104071041710427104371044710457104671047710487104971050710517105271053710547105571056710577105871059710607106171062710637106471065710667106771068710697107071071710727107371074710757107671077710787107971080710817108271083710847108571086710877108871089710907109171092710937109471095710967109771098710997110071101711027110371104711057110671107711087110971110711117111271113711147111571116711177111871119711207112171122711237112471125711267112771128711297113071131711327113371134711357113671137711387113971140711417114271143711447114571146711477114871149711507115171152711537115471155711567115771158711597116071161711627116371164711657116671167711687116971170711717117271173711747117571176711777117871179711807118171182711837118471185711867118771188711897119071191711927119371194711957119671197711987119971200712017120271203712047120571206712077120871209712107121171212712137121471215712167121771218712197122071221712227122371224712257122671227712287122971230712317123271233712347123571236712377123871239712407124171242712437124471245712467124771248712497125071251712527125371254712557125671257712587125971260712617126271263712647126571266712677126871269712707127171272712737127471275712767127771278712797128071281712827128371284712857128671287712887128971290712917129271293712947129571296712977129871299713007130171302713037130471305713067130771308713097131071311713127131371314713157131671317713187131971320713217132271323713247132571326713277132871329713307133171332713337133471335713367133771338713397134071341713427134371344713457134671347713487134971350713517135271353713547135571356713577135871359713607136171362713637136471365713667136771368713697137071371713727137371374713757137671377713787137971380713817138271383713847138571386713877138871389713907139171392713937139471395713967139771398713997140071401714027140371404714057140671407714087140971410714117141271413714147141571416714177141871419714207142171422714237142471425714267142771428714297143071431714327143371434714357143671437714387143971440714417144271443714447144571446714477144871449714507145171452714537145471455714567145771458714597146071461714627146371464714657146671467714687146971470714717147271473714747147571476714777147871479714807148171482714837148471485714867148771488714897149071491714927149371494714957149671497714987149971500715017150271503715047150571506715077150871509715107151171512715137151471515715167151771518715197152071521715227152371524715257152671527715287152971530715317153271533715347153571536715377153871539715407154171542715437154471545715467154771548715497155071551715527155371554715557155671557715587155971560715617156271563715647156571566715677156871569715707157171572715737157471575715767157771578715797158071581715827158371584715857158671587715887158971590715917159271593715947159571596715977159871599716007160171602716037160471605716067160771608716097161071611716127161371614716157161671617716187161971620716217162271623716247162571626716277162871629716307163171632716337163471635716367163771638716397164071641716427164371644716457164671647716487164971650716517165271653716547165571656716577165871659716607166171662716637166471665716667166771668716697167071671716727167371674716757167671677716787167971680716817168271683716847168571686716877168871689716907169171692716937169471695716967169771698716997170071701717027170371704717057170671707717087170971710717117171271713717147171571716717177171871719717207172171722717237172471725717267172771728717297173071731717327173371734717357173671737717387173971740717417174271743717447174571746717477174871749717507175171752717537175471755717567175771758717597176071761717627176371764717657176671767717687176971770717717177271773717747177571776717777177871779717807178171782717837178471785717867178771788717897179071791717927179371794717957179671797717987179971800718017180271803718047180571806718077180871809718107181171812718137181471815718167181771818718197182071821718227182371824718257182671827718287182971830718317183271833718347183571836718377183871839718407184171842718437184471845718467184771848718497185071851718527185371854718557185671857718587185971860718617186271863718647186571866718677186871869718707187171872718737187471875718767187771878718797188071881718827188371884718857188671887718887188971890718917189271893718947189571896718977189871899719007190171902719037190471905719067190771908719097191071911719127191371914719157191671917719187191971920719217192271923719247192571926719277192871929719307193171932719337193471935719367193771938719397194071941719427194371944719457194671947719487194971950719517195271953719547195571956719577195871959719607196171962719637196471965719667196771968719697197071971719727197371974719757197671977719787197971980719817198271983719847198571986719877198871989719907199171992719937199471995719967199771998719997200072001720027200372004720057200672007720087200972010720117201272013720147201572016720177201872019720207202172022720237202472025720267202772028720297203072031720327203372034720357203672037720387203972040720417204272043720447204572046720477204872049720507205172052720537205472055720567205772058720597206072061720627206372064720657206672067720687206972070720717207272073720747207572076720777207872079720807208172082720837208472085720867208772088720897209072091720927209372094720957209672097720987209972100721017210272103721047210572106721077210872109721107211172112721137211472115721167211772118721197212072121721227212372124721257212672127721287212972130721317213272133721347213572136721377213872139721407214172142721437214472145721467214772148721497215072151721527215372154721557215672157721587215972160721617216272163721647216572166721677216872169721707217172172721737217472175721767217772178721797218072181721827218372184721857218672187721887218972190721917219272193721947219572196721977219872199722007220172202722037220472205722067220772208722097221072211722127221372214722157221672217722187221972220722217222272223722247222572226722277222872229722307223172232722337223472235722367223772238722397224072241722427224372244722457224672247722487224972250722517225272253722547225572256722577225872259722607226172262722637226472265722667226772268722697227072271722727227372274722757227672277722787227972280722817228272283722847228572286722877228872289722907229172292722937229472295722967229772298722997230072301723027230372304723057230672307723087230972310723117231272313723147231572316723177231872319723207232172322723237232472325723267232772328723297233072331723327233372334723357233672337723387233972340723417234272343723447234572346723477234872349723507235172352723537235472355723567235772358723597236072361723627236372364723657236672367723687236972370723717237272373723747237572376723777237872379723807238172382723837238472385723867238772388723897239072391723927239372394723957239672397723987239972400724017240272403724047240572406724077240872409724107241172412724137241472415724167241772418724197242072421724227242372424724257242672427724287242972430724317243272433724347243572436724377243872439724407244172442724437244472445724467244772448724497245072451724527245372454724557245672457724587245972460724617246272463724647246572466724677246872469724707247172472724737247472475724767247772478724797248072481724827248372484724857248672487724887248972490724917249272493724947249572496724977249872499725007250172502725037250472505725067250772508725097251072511725127251372514725157251672517725187251972520725217252272523725247252572526725277252872529725307253172532725337253472535725367253772538725397254072541725427254372544725457254672547725487254972550725517255272553725547255572556725577255872559725607256172562725637256472565725667256772568725697257072571725727257372574725757257672577725787257972580725817258272583725847258572586725877258872589725907259172592725937259472595725967259772598725997260072601726027260372604726057260672607726087260972610726117261272613726147261572616726177261872619726207262172622726237262472625726267262772628726297263072631726327263372634726357263672637726387263972640726417264272643726447264572646726477264872649726507265172652726537265472655726567265772658726597266072661726627266372664726657266672667726687266972670726717267272673726747267572676726777267872679726807268172682726837268472685726867268772688726897269072691726927269372694726957269672697726987269972700727017270272703727047270572706727077270872709727107271172712727137271472715727167271772718727197272072721727227272372724727257272672727727287272972730727317273272733727347273572736727377273872739727407274172742727437274472745727467274772748727497275072751727527275372754727557275672757727587275972760727617276272763727647276572766727677276872769727707277172772727737277472775727767277772778727797278072781727827278372784727857278672787727887278972790727917279272793727947279572796727977279872799728007280172802728037280472805728067280772808728097281072811728127281372814728157281672817728187281972820728217282272823728247282572826728277282872829728307283172832728337283472835728367283772838728397284072841728427284372844728457284672847728487284972850728517285272853728547285572856728577285872859728607286172862728637286472865728667286772868728697287072871728727287372874728757287672877728787287972880728817288272883728847288572886728877288872889728907289172892728937289472895728967289772898728997290072901729027290372904729057290672907729087290972910729117291272913729147291572916729177291872919729207292172922729237292472925729267292772928729297293072931729327293372934729357293672937729387293972940729417294272943729447294572946729477294872949729507295172952729537295472955729567295772958729597296072961729627296372964729657296672967729687296972970729717297272973729747297572976729777297872979729807298172982729837298472985729867298772988729897299072991729927299372994729957299672997729987299973000730017300273003730047300573006730077300873009730107301173012730137301473015730167301773018730197302073021730227302373024730257302673027730287302973030730317303273033730347303573036730377303873039730407304173042730437304473045730467304773048730497305073051730527305373054730557305673057730587305973060730617306273063730647306573066730677306873069730707307173072730737307473075730767307773078730797308073081730827308373084730857308673087730887308973090730917309273093730947309573096730977309873099731007310173102731037310473105731067310773108731097311073111731127311373114731157311673117731187311973120731217312273123731247312573126731277312873129731307313173132731337313473135731367313773138731397314073141731427314373144731457314673147731487314973150731517315273153731547315573156731577315873159731607316173162731637316473165731667316773168731697317073171731727317373174731757317673177731787317973180731817318273183731847318573186731877318873189731907319173192731937319473195731967319773198731997320073201732027320373204732057320673207732087320973210732117321273213732147321573216732177321873219732207322173222732237322473225732267322773228732297323073231732327323373234732357323673237732387323973240732417324273243732447324573246732477324873249732507325173252732537325473255732567325773258732597326073261732627326373264732657326673267732687326973270732717327273273732747327573276732777327873279732807328173282732837328473285732867328773288732897329073291732927329373294732957329673297732987329973300733017330273303733047330573306733077330873309733107331173312733137331473315733167331773318733197332073321733227332373324733257332673327733287332973330733317333273333733347333573336733377333873339733407334173342733437334473345733467334773348733497335073351733527335373354733557335673357733587335973360733617336273363733647336573366733677336873369733707337173372733737337473375733767337773378733797338073381733827338373384733857338673387733887338973390733917339273393733947339573396733977339873399734007340173402734037340473405734067340773408734097341073411734127341373414734157341673417734187341973420734217342273423734247342573426734277342873429734307343173432734337343473435734367343773438734397344073441734427344373444734457344673447734487344973450734517345273453734547345573456734577345873459734607346173462734637346473465734667346773468734697347073471734727347373474734757347673477734787347973480734817348273483734847348573486734877348873489734907349173492734937349473495734967349773498734997350073501735027350373504735057350673507735087350973510735117351273513735147351573516735177351873519735207352173522735237352473525735267352773528735297353073531735327353373534735357353673537735387353973540735417354273543735447354573546735477354873549735507355173552735537355473555735567355773558735597356073561735627356373564735657356673567735687356973570735717357273573735747357573576735777357873579735807358173582735837358473585735867358773588735897359073591735927359373594735957359673597735987359973600736017360273603736047360573606736077360873609736107361173612736137361473615736167361773618736197362073621736227362373624736257362673627736287362973630736317363273633736347363573636736377363873639736407364173642736437364473645736467364773648736497365073651736527365373654736557365673657736587365973660736617366273663736647366573666736677366873669736707367173672736737367473675736767367773678736797368073681736827368373684736857368673687736887368973690736917369273693736947369573696736977369873699737007370173702737037370473705737067370773708737097371073711737127371373714737157371673717737187371973720737217372273723737247372573726737277372873729737307373173732737337373473735737367373773738737397374073741737427374373744737457374673747737487374973750737517375273753737547375573756737577375873759737607376173762737637376473765737667376773768737697377073771737727377373774737757377673777737787377973780737817378273783737847378573786737877378873789737907379173792737937379473795737967379773798737997380073801738027380373804738057380673807738087380973810738117381273813738147381573816738177381873819738207382173822738237382473825738267382773828738297383073831738327383373834738357383673837738387383973840738417384273843738447384573846738477384873849738507385173852738537385473855738567385773858738597386073861738627386373864738657386673867738687386973870738717387273873738747387573876738777387873879738807388173882738837388473885738867388773888738897389073891738927389373894738957389673897738987389973900739017390273903739047390573906739077390873909739107391173912739137391473915739167391773918739197392073921739227392373924739257392673927739287392973930739317393273933739347393573936739377393873939739407394173942739437394473945739467394773948739497395073951739527395373954739557395673957739587395973960739617396273963739647396573966739677396873969739707397173972739737397473975739767397773978739797398073981739827398373984739857398673987739887398973990739917399273993739947399573996739977399873999740007400174002740037400474005740067400774008740097401074011740127401374014740157401674017740187401974020740217402274023740247402574026740277402874029740307403174032740337403474035740367403774038740397404074041740427404374044740457404674047740487404974050740517405274053740547405574056740577405874059740607406174062740637406474065740667406774068740697407074071740727407374074740757407674077740787407974080740817408274083740847408574086740877408874089740907409174092740937409474095740967409774098740997410074101741027410374104741057410674107741087410974110741117411274113741147411574116741177411874119741207412174122741237412474125741267412774128741297413074131741327413374134741357413674137741387413974140741417414274143741447414574146741477414874149741507415174152741537415474155741567415774158741597416074161741627416374164741657416674167741687416974170741717417274173741747417574176741777417874179741807418174182741837418474185741867418774188741897419074191741927419374194741957419674197741987419974200742017420274203742047420574206742077420874209742107421174212742137421474215742167421774218742197422074221742227422374224742257422674227742287422974230742317423274233742347423574236742377423874239742407424174242742437424474245742467424774248742497425074251742527425374254742557425674257742587425974260742617426274263742647426574266742677426874269742707427174272742737427474275742767427774278742797428074281742827428374284742857428674287742887428974290742917429274293742947429574296742977429874299743007430174302743037430474305743067430774308743097431074311743127431374314743157431674317743187431974320743217432274323743247432574326743277432874329743307433174332743337433474335743367433774338743397434074341743427434374344743457434674347743487434974350743517435274353743547435574356743577435874359743607436174362743637436474365743667436774368743697437074371743727437374374743757437674377743787437974380743817438274383743847438574386743877438874389743907439174392743937439474395743967439774398743997440074401744027440374404744057440674407744087440974410744117441274413744147441574416744177441874419744207442174422744237442474425744267442774428744297443074431744327443374434744357443674437744387443974440744417444274443744447444574446744477444874449744507445174452744537445474455744567445774458744597446074461744627446374464744657446674467744687446974470744717447274473744747447574476744777447874479744807448174482744837448474485744867448774488744897449074491744927449374494744957449674497744987449974500745017450274503745047450574506745077450874509745107451174512745137451474515745167451774518745197452074521745227452374524745257452674527745287452974530745317453274533745347453574536745377453874539745407454174542745437454474545745467454774548745497455074551745527455374554745557455674557745587455974560745617456274563745647456574566745677456874569745707457174572745737457474575745767457774578745797458074581745827458374584745857458674587745887458974590745917459274593745947459574596745977459874599746007460174602746037460474605746067460774608746097461074611746127461374614746157461674617746187461974620746217462274623746247462574626746277462874629746307463174632746337463474635746367463774638746397464074641746427464374644746457464674647746487464974650746517465274653746547465574656746577465874659746607466174662746637466474665746667466774668746697467074671746727467374674746757467674677746787467974680746817468274683746847468574686746877468874689746907469174692746937469474695746967469774698746997470074701747027470374704747057470674707747087470974710747117471274713747147471574716747177471874719747207472174722747237472474725747267472774728747297473074731747327473374734747357473674737747387473974740747417474274743747447474574746747477474874749747507475174752747537475474755747567475774758747597476074761747627476374764747657476674767747687476974770747717477274773747747477574776747777477874779747807478174782747837478474785747867478774788747897479074791747927479374794747957479674797747987479974800748017480274803748047480574806748077480874809748107481174812748137481474815748167481774818748197482074821748227482374824748257482674827748287482974830748317483274833748347483574836748377483874839748407484174842748437484474845748467484774848748497485074851748527485374854748557485674857748587485974860748617486274863748647486574866748677486874869748707487174872748737487474875748767487774878748797488074881748827488374884748857488674887748887488974890748917489274893748947489574896748977489874899749007490174902749037490474905749067490774908749097491074911749127491374914749157491674917749187491974920749217492274923749247492574926749277492874929749307493174932749337493474935749367493774938749397494074941749427494374944749457494674947749487494974950749517495274953749547495574956749577495874959749607496174962749637496474965749667496774968749697497074971749727497374974749757497674977749787497974980749817498274983749847498574986749877498874989749907499174992749937499474995749967499774998749997500075001750027500375004750057500675007750087500975010750117501275013750147501575016750177501875019750207502175022750237502475025750267502775028750297503075031750327503375034750357503675037750387503975040750417504275043750447504575046750477504875049750507505175052750537505475055750567505775058750597506075061750627506375064750657506675067750687506975070750717507275073750747507575076750777507875079750807508175082750837508475085750867508775088750897509075091750927509375094750957509675097750987509975100751017510275103751047510575106751077510875109751107511175112751137511475115751167511775118751197512075121751227512375124751257512675127751287512975130751317513275133751347513575136751377513875139751407514175142751437514475145751467514775148751497515075151751527515375154751557515675157751587515975160751617516275163751647516575166751677516875169751707517175172751737517475175751767517775178751797518075181751827518375184751857518675187751887518975190751917519275193751947519575196751977519875199752007520175202752037520475205752067520775208752097521075211752127521375214752157521675217752187521975220752217522275223752247522575226752277522875229752307523175232752337523475235752367523775238752397524075241752427524375244752457524675247752487524975250752517525275253752547525575256752577525875259752607526175262752637526475265752667526775268752697527075271752727527375274752757527675277752787527975280752817528275283752847528575286752877528875289752907529175292752937529475295752967529775298752997530075301753027530375304753057530675307753087530975310753117531275313753147531575316753177531875319753207532175322753237532475325753267532775328753297533075331753327533375334753357533675337753387533975340753417534275343753447534575346753477534875349753507535175352753537535475355753567535775358753597536075361753627536375364753657536675367753687536975370753717537275373753747537575376753777537875379753807538175382753837538475385753867538775388753897539075391753927539375394753957539675397753987539975400754017540275403754047540575406754077540875409754107541175412754137541475415754167541775418754197542075421754227542375424754257542675427754287542975430754317543275433754347543575436754377543875439754407544175442754437544475445754467544775448754497545075451754527545375454754557545675457754587545975460754617546275463754647546575466754677546875469754707547175472754737547475475754767547775478754797548075481754827548375484754857548675487754887548975490754917549275493754947549575496754977549875499755007550175502755037550475505755067550775508755097551075511755127551375514755157551675517755187551975520755217552275523755247552575526755277552875529755307553175532755337553475535755367553775538755397554075541755427554375544755457554675547755487554975550755517555275553755547555575556755577555875559755607556175562755637556475565755667556775568755697557075571755727557375574755757557675577755787557975580755817558275583755847558575586755877558875589755907559175592755937559475595755967559775598755997560075601756027560375604756057560675607756087560975610756117561275613756147561575616756177561875619756207562175622756237562475625756267562775628756297563075631756327563375634756357563675637756387563975640756417564275643756447564575646756477564875649756507565175652756537565475655756567565775658756597566075661756627566375664756657566675667756687566975670756717567275673756747567575676756777567875679756807568175682756837568475685756867568775688756897569075691756927569375694756957569675697756987569975700757017570275703757047570575706757077570875709757107571175712757137571475715757167571775718757197572075721757227572375724757257572675727757287572975730757317573275733757347573575736757377573875739757407574175742757437574475745757467574775748757497575075751757527575375754757557575675757757587575975760757617576275763757647576575766757677576875769757707577175772757737577475775757767577775778757797578075781757827578375784757857578675787757887578975790757917579275793757947579575796757977579875799758007580175802758037580475805758067580775808758097581075811758127581375814758157581675817758187581975820758217582275823758247582575826758277582875829758307583175832758337583475835758367583775838758397584075841758427584375844758457584675847758487584975850758517585275853758547585575856758577585875859758607586175862758637586475865758667586775868758697587075871758727587375874758757587675877758787587975880758817588275883758847588575886758877588875889758907589175892758937589475895758967589775898758997590075901759027590375904759057590675907759087590975910759117591275913759147591575916759177591875919759207592175922759237592475925759267592775928759297593075931759327593375934759357593675937759387593975940759417594275943759447594575946759477594875949759507595175952759537595475955759567595775958759597596075961759627596375964759657596675967759687596975970759717597275973
  1. /* api.c API unit tests
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /* For AES-CBC, input lengths can optionally be validated to be a
  22. * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
  23. * also available via the configure option --enable-aescbc-length-checks.
  24. */
  25. /*----------------------------------------------------------------------------*
  26. | Includes
  27. *----------------------------------------------------------------------------*/
  28. #ifdef HAVE_CONFIG_H
  29. #include <config.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/settings.h>
  32. #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
  33. #ifndef FOURK_BUF
  34. #define FOURK_BUF 4096
  35. #endif
  36. #ifndef TWOK_BUF
  37. #define TWOK_BUF 2048
  38. #endif
  39. #ifndef ONEK_BUF
  40. #define ONEK_BUF 1024
  41. #endif
  42. #if defined(WOLFSSL_STATIC_MEMORY)
  43. #include <wolfssl/wolfcrypt/memory.h>
  44. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  45. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  46. defined(SESSION_CERTS)
  47. #ifdef OPENSSL_EXTRA
  48. #define TEST_TLS_STATIC_MEMSZ (400000)
  49. #else
  50. #define TEST_TLS_STATIC_MEMSZ (320000)
  51. #endif
  52. #else
  53. #define TEST_TLS_STATIC_MEMSZ (80000)
  54. #endif
  55. #endif
  56. #endif /* WOLFSSL_STATIC_MEMORY */
  57. #ifndef HEAP_HINT
  58. #define HEAP_HINT NULL
  59. #endif /* WOLFSSL_STAIC_MEMORY */
  60. #ifdef WOLFSSL_ASNC_CRYPT
  61. #include <wolfssl/wolfcrypt/async.h>
  62. #endif
  63. #ifdef HAVE_ECC
  64. #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
  65. #ifndef ECC_ASN963_MAX_BUF_SZ
  66. #define ECC_ASN963_MAX_BUF_SZ 133
  67. #endif
  68. #ifndef ECC_PRIV_KEY_BUF
  69. #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
  70. #endif
  71. /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
  72. /* logic to choose right key ECC size */
  73. #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
  74. #define KEY14 14
  75. #else
  76. #define KEY14 32
  77. #endif
  78. #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
  79. #define KEY16 16
  80. #else
  81. #define KEY16 32
  82. #endif
  83. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  84. #define KEY20 20
  85. #else
  86. #define KEY20 32
  87. #endif
  88. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  89. #define KEY24 24
  90. #else
  91. #define KEY24 32
  92. #endif
  93. #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
  94. #define KEY28 28
  95. #else
  96. #define KEY28 32
  97. #endif
  98. #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
  99. #define KEY30 30
  100. #else
  101. #define KEY30 32
  102. #endif
  103. #define KEY32 32
  104. #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
  105. #define KEY40 40
  106. #else
  107. #define KEY40 32
  108. #endif
  109. #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
  110. #define KEY48 48
  111. #else
  112. #define KEY48 32
  113. #endif
  114. #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
  115. #define KEY64 64
  116. #else
  117. #define KEY64 32
  118. #endif
  119. #if !defined(HAVE_COMP_KEY)
  120. #if !defined(NOCOMP)
  121. #define NOCOMP 0
  122. #endif
  123. #else
  124. #if !defined(COMP)
  125. #define COMP 1
  126. #endif
  127. #endif
  128. #if !defined(DER_SZ)
  129. #define DER_SZ(ks) ((ks) * 2 + 1)
  130. #endif
  131. #ifdef WOLFSSL_SM2
  132. #include <wolfssl/wolfcrypt/sm2.h>
  133. #endif
  134. #endif
  135. #ifndef NO_ASN
  136. #include <wolfssl/wolfcrypt/asn_public.h>
  137. #endif
  138. #include <wolfssl/error-ssl.h>
  139. #include <stdlib.h>
  140. #include <wolfssl/ssl.h> /* compatibility layer */
  141. #include <wolfssl/test.h>
  142. #include <tests/unit.h>
  143. #include "examples/server/server.h"
  144. /* for testing compatibility layer callbacks */
  145. #ifndef NO_MD5
  146. #include <wolfssl/wolfcrypt/md5.h>
  147. #endif
  148. #ifndef NO_SHA
  149. #include <wolfssl/wolfcrypt/sha.h>
  150. #endif
  151. #ifndef NO_SHA256
  152. #include <wolfssl/wolfcrypt/sha256.h>
  153. #endif
  154. #ifdef WOLFSSL_SHA512
  155. #include <wolfssl/wolfcrypt/sha512.h>
  156. #endif
  157. #ifdef WOLFSSL_SHA384
  158. #include <wolfssl/wolfcrypt/sha512.h>
  159. #endif
  160. #ifdef WOLFSSL_SHA3
  161. #include <wolfssl/wolfcrypt/sha3.h>
  162. #ifndef HEAP_HINT
  163. #define HEAP_HINT NULL
  164. #endif
  165. #endif
  166. #ifdef WOLFSSL_SM3
  167. #include <wolfssl/wolfcrypt/sm3.h>
  168. #endif
  169. #ifndef NO_AES
  170. #include <wolfssl/wolfcrypt/aes.h>
  171. #ifdef HAVE_AES_DECRYPT
  172. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  173. #endif
  174. #endif
  175. #ifdef WOLFSSL_SM4
  176. #include <wolfssl/wolfcrypt/sm4.h>
  177. #endif
  178. #ifdef WOLFSSL_RIPEMD
  179. #include <wolfssl/wolfcrypt/ripemd.h>
  180. #endif
  181. #ifndef NO_DES3
  182. #include <wolfssl/wolfcrypt/des3.h>
  183. #include <wolfssl/wolfcrypt/wc_encrypt.h>
  184. #endif
  185. #ifdef WC_RC2
  186. #include <wolfssl/wolfcrypt/rc2.h>
  187. #endif
  188. #ifndef NO_HMAC
  189. #include <wolfssl/wolfcrypt/hmac.h>
  190. #endif
  191. #ifdef HAVE_CHACHA
  192. #include <wolfssl/wolfcrypt/chacha.h>
  193. #endif
  194. #ifdef HAVE_POLY1305
  195. #include <wolfssl/wolfcrypt/poly1305.h>
  196. #endif
  197. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  198. #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
  199. #endif
  200. #ifdef HAVE_CAMELLIA
  201. #include <wolfssl/wolfcrypt/camellia.h>
  202. #endif
  203. #ifndef NO_RC4
  204. #include <wolfssl/wolfcrypt/arc4.h>
  205. #endif
  206. #ifdef HAVE_BLAKE2
  207. #include <wolfssl/wolfcrypt/blake2.h>
  208. #endif
  209. #include <wolfssl/wolfcrypt/hash.h>
  210. #ifndef NO_RSA
  211. #include <wolfssl/wolfcrypt/rsa.h>
  212. #define FOURK_BUF 4096
  213. #define GEN_BUF 294
  214. #endif
  215. #ifndef NO_SIG_WRAPPER
  216. #include <wolfssl/wolfcrypt/signature.h>
  217. #endif
  218. #ifdef HAVE_AESCCM
  219. #include <wolfssl/wolfcrypt/aes.h>
  220. #endif
  221. #ifdef HAVE_PKCS7
  222. #include <wolfssl/wolfcrypt/pkcs7.h>
  223. #include <wolfssl/wolfcrypt/asn.h>
  224. #ifdef HAVE_LIBZ
  225. #include <wolfssl/wolfcrypt/compress.h>
  226. #endif
  227. #endif
  228. #ifdef WOLFSSL_SMALL_CERT_VERIFY
  229. #include <wolfssl/wolfcrypt/asn.h>
  230. #endif
  231. #ifndef NO_DSA
  232. #include <wolfssl/wolfcrypt/dsa.h>
  233. #ifndef ONEK_BUF
  234. #define ONEK_BUF 1024
  235. #endif
  236. #ifndef TWOK_BUF
  237. #define TWOK_BUF 2048
  238. #endif
  239. #ifndef FOURK_BUF
  240. #define FOURK_BUF 4096
  241. #endif
  242. #ifndef DSA_SIG_SIZE
  243. #define DSA_SIG_SIZE 40
  244. #endif
  245. #ifndef MAX_DSA_PARAM_SIZE
  246. #define MAX_DSA_PARAM_SIZE 256
  247. #endif
  248. #endif
  249. #ifdef WOLFSSL_CMAC
  250. #include <wolfssl/wolfcrypt/cmac.h>
  251. #endif
  252. #ifdef HAVE_ED25519
  253. #include <wolfssl/wolfcrypt/ed25519.h>
  254. #endif
  255. #ifdef HAVE_CURVE25519
  256. #include <wolfssl/wolfcrypt/curve25519.h>
  257. #endif
  258. #ifdef HAVE_ED448
  259. #include <wolfssl/wolfcrypt/ed448.h>
  260. #endif
  261. #ifdef HAVE_CURVE448
  262. #include <wolfssl/wolfcrypt/curve448.h>
  263. #endif
  264. #ifdef HAVE_DILITHIUM
  265. #include <wolfssl/wolfcrypt/dilithium.h>
  266. #endif
  267. #ifdef HAVE_PKCS12
  268. #include <wolfssl/wolfcrypt/pkcs12.h>
  269. #endif
  270. #include <wolfssl/wolfcrypt/logging.h>
  271. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
  272. #include <wolfssl/openssl/ssl.h>
  273. #ifndef NO_ASN
  274. /* for ASN_COMMON_NAME DN_tags enum */
  275. #include <wolfssl/wolfcrypt/asn.h>
  276. #endif
  277. #ifdef HAVE_OCSP
  278. #include <wolfssl/openssl/ocsp.h>
  279. #endif
  280. #endif
  281. #ifdef OPENSSL_EXTRA
  282. #include <wolfssl/openssl/cmac.h>
  283. #include <wolfssl/openssl/x509v3.h>
  284. #include <wolfssl/openssl/asn1.h>
  285. #include <wolfssl/openssl/crypto.h>
  286. #include <wolfssl/openssl/pkcs12.h>
  287. #include <wolfssl/openssl/evp.h>
  288. #include <wolfssl/openssl/dh.h>
  289. #include <wolfssl/openssl/bn.h>
  290. #include <wolfssl/openssl/buffer.h>
  291. #include <wolfssl/openssl/pem.h>
  292. #include <wolfssl/openssl/ec.h>
  293. #include <wolfssl/openssl/ecdh.h>
  294. #include <wolfssl/openssl/engine.h>
  295. #include <wolfssl/openssl/hmac.h>
  296. #include <wolfssl/openssl/objects.h>
  297. #include <wolfssl/openssl/rand.h>
  298. #include <wolfssl/openssl/modes.h>
  299. #include <wolfssl/openssl/fips_rand.h>
  300. #include <wolfssl/openssl/kdf.h>
  301. #ifdef OPENSSL_ALL
  302. #include <wolfssl/openssl/txt_db.h>
  303. #include <wolfssl/openssl/lhash.h>
  304. #endif
  305. #ifndef NO_AES
  306. #include <wolfssl/openssl/aes.h>
  307. #endif
  308. #ifndef NO_DES3
  309. #include <wolfssl/openssl/des.h>
  310. #endif
  311. #ifndef NO_RC4
  312. #include <wolfssl/openssl/rc4.h>
  313. #endif
  314. #ifdef HAVE_ECC
  315. #include <wolfssl/openssl/ecdsa.h>
  316. #endif
  317. #ifdef HAVE_PKCS7
  318. #include <wolfssl/openssl/pkcs7.h>
  319. #endif
  320. #ifdef HAVE_CURVE25519
  321. #include <wolfssl/openssl/ec25519.h>
  322. #endif
  323. #ifdef HAVE_ED25519
  324. #include <wolfssl/openssl/ed25519.h>
  325. #endif
  326. #ifdef HAVE_CURVE448
  327. #include <wolfssl/openssl/ec448.h>
  328. #endif
  329. #ifdef HAVE_ED448
  330. #include <wolfssl/openssl/ed448.h>
  331. #endif
  332. #endif /* OPENSSL_EXTRA */
  333. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  334. && !defined(NO_SHA256) && !defined(RC_NO_RNG)
  335. #include <wolfssl/wolfcrypt/srp.h>
  336. #endif
  337. #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
  338. defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
  339. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
  340. defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
  341. defined(HAVE_ECH) || defined(HAVE_EX_DATA) || !defined(NO_SESSION_CACHE) \
  342. || !defined(WOLFSSL_NO_TLS12) || defined(WOLFSSL_TLS13)
  343. /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
  344. * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
  345. * number tracking */
  346. #include "wolfssl/internal.h"
  347. #endif
  348. /* force enable test buffers */
  349. #ifndef USE_CERT_BUFFERS_2048
  350. #define USE_CERT_BUFFERS_2048
  351. #endif
  352. #ifndef USE_CERT_BUFFERS_256
  353. #define USE_CERT_BUFFERS_256
  354. #endif
  355. #include <wolfssl/certs_test.h>
  356. #include "tests/utils.h"
  357. /* include misc.c here regardless of NO_INLINE, because misc.c implementations
  358. * have default (hidden) visibility, and in the absence of visibility, it's
  359. * benign to mask out the library implementation.
  360. */
  361. #define WOLFSSL_MISC_INCLUDED
  362. #include <wolfcrypt/src/misc.c>
  363. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  364. /* FIPS build has replaced ecc.h. */
  365. #define wc_ecc_key_get_priv(key) (&((key)->k))
  366. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  367. #endif
  368. typedef struct testVector {
  369. const char* input;
  370. const char* output;
  371. size_t inLen;
  372. size_t outLen;
  373. } testVector;
  374. #if defined(HAVE_PKCS7)
  375. typedef struct {
  376. const byte* content;
  377. word32 contentSz;
  378. int contentOID;
  379. int encryptOID;
  380. int keyWrapOID;
  381. int keyAgreeOID;
  382. byte* cert;
  383. size_t certSz;
  384. byte* privateKey;
  385. word32 privateKeySz;
  386. } pkcs7EnvelopedVector;
  387. #ifndef NO_PKCS7_ENCRYPTED_DATA
  388. typedef struct {
  389. const byte* content;
  390. word32 contentSz;
  391. int contentOID;
  392. int encryptOID;
  393. byte* encryptionKey;
  394. word32 encryptionKeySz;
  395. } pkcs7EncryptedVector;
  396. #endif
  397. #endif /* HAVE_PKCS7 */
  398. typedef int (*ctx_cb)(WOLFSSL_CTX* ctx);
  399. typedef int (*ssl_cb)(WOLFSSL* ssl);
  400. typedef int (*test_cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  401. typedef int (*hs_cb)(WOLFSSL_CTX **ctx, WOLFSSL **ssl);
  402. typedef struct test_ssl_cbf {
  403. method_provider method;
  404. ctx_cb ctx_ready;
  405. ssl_cb ssl_ready;
  406. ssl_cb on_result;
  407. ctx_cb on_ctx_cleanup;
  408. ssl_cb on_cleanup;
  409. hs_cb on_handshake;
  410. WOLFSSL_CTX* ctx;
  411. const char* caPemFile;
  412. const char* certPemFile;
  413. const char* keyPemFile;
  414. const char* crlPemFile;
  415. #ifdef WOLFSSL_STATIC_MEMORY
  416. byte* mem;
  417. word32 memSz;
  418. wolfSSL_method_func method_ex;
  419. #endif
  420. int devId;
  421. int return_code;
  422. int last_err;
  423. unsigned char isSharedCtx:1;
  424. unsigned char loadToSSL:1;
  425. unsigned char ticNoInit:1;
  426. unsigned char doUdp:1;
  427. } test_ssl_cbf;
  428. #define TEST_SSL_MEMIO_BUF_SZ (64 * 1024)
  429. typedef struct test_ssl_memio_ctx {
  430. WOLFSSL_CTX* s_ctx;
  431. WOLFSSL_CTX* c_ctx;
  432. WOLFSSL* s_ssl;
  433. WOLFSSL* c_ssl;
  434. const char* c_ciphers;
  435. const char* s_ciphers;
  436. char* c_msg;
  437. int c_msglen;
  438. char* s_msg;
  439. int s_msglen;
  440. test_ssl_cbf s_cb;
  441. test_ssl_cbf c_cb;
  442. byte c_buff[TEST_SSL_MEMIO_BUF_SZ];
  443. int c_len;
  444. byte s_buff[TEST_SSL_MEMIO_BUF_SZ];
  445. int s_len;
  446. } test_ssl_memio_ctx;
  447. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  448. test_ssl_cbf* server_cb, test_cbType client_on_handshake);
  449. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  450. const char* currentTestName;
  451. char tmpDirName[16];
  452. int tmpDirNameSet = 0;
  453. #endif
  454. /*----------------------------------------------------------------------------*
  455. | Constants
  456. *----------------------------------------------------------------------------*/
  457. /* Test result constants and macros. */
  458. /* Test succeeded. */
  459. #define TEST_SUCCESS (1)
  460. /* Test failed. */
  461. #define TEST_FAIL (0)
  462. /* Test skipped - not run. */
  463. #define TEST_SKIPPED (-7777)
  464. /* Returns the result based on whether check is true.
  465. *
  466. * @param [in] check Condition for success.
  467. * @return When condition is true: TEST_SUCCESS.
  468. * @return When condition is false: TEST_FAIL.
  469. */
  470. #ifdef DEBUG_WOLFSSL_VERBOSE
  471. #define XSTRINGIFY(s) STRINGIFY(s)
  472. #define STRINGIFY(s) #s
  473. #define TEST_RES_CHECK(check) ({ \
  474. int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
  475. if (_ret == TEST_FAIL) { \
  476. fprintf(stderr, " check \"%s\" at %d ", \
  477. XSTRINGIFY(check), __LINE__); \
  478. } \
  479. _ret; })
  480. #else
  481. #define TEST_RES_CHECK(check) \
  482. ((check) ? TEST_SUCCESS : TEST_FAIL)
  483. #endif /* DEBUG_WOLFSSL_VERBOSE */
  484. #define TEST_STRING "Everyone gets Friday off."
  485. #define TEST_STRING_SZ 25
  486. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  487. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  488. #define TEST_RSA_BITS 1024
  489. #else
  490. #define TEST_RSA_BITS 2048
  491. #endif
  492. #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
  493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  494. (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
  495. static const char* bogusFile =
  496. #ifdef _WIN32
  497. "NUL"
  498. #else
  499. "/dev/null"
  500. #endif
  501. ;
  502. #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
  503. enum {
  504. TESTING_RSA = 1,
  505. TESTING_ECC = 2
  506. };
  507. #ifdef WOLFSSL_QNX_CAAM
  508. #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
  509. static int testDevId = WOLFSSL_CAAM_DEVID;
  510. #else
  511. static int testDevId = INVALID_DEVID;
  512. #endif
  513. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  514. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  515. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  516. #define HAVE_IO_TESTS_DEPENDENCIES
  517. #endif
  518. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  519. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  520. #define HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  521. #endif
  522. /*----------------------------------------------------------------------------*
  523. | BIO with fixed read/write size
  524. *----------------------------------------------------------------------------*/
  525. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  526. static int wolfssl_bio_s_fixed_mem_write(WOLFSSL_BIO* bio, const char* data,
  527. int len)
  528. {
  529. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  530. len = 0;
  531. }
  532. else {
  533. if (bio->wrSz - bio->wrIdx < len) {
  534. len = bio->wrSz - bio->wrIdx;
  535. }
  536. XMEMCPY((char*)bio->ptr + bio->wrIdx, data, len);
  537. bio->wrIdx += len;
  538. }
  539. return len;
  540. }
  541. static int wolfssl_bio_s_fixed_mem_read(WOLFSSL_BIO* bio, char* data, int len)
  542. {
  543. if ((bio == NULL) || (bio->ptr == NULL) || (data == NULL)) {
  544. len = 0;
  545. }
  546. else {
  547. if (bio->wrSz - bio->rdIdx < len) {
  548. len = bio->wrSz - bio->rdIdx;
  549. }
  550. XMEMCPY(data, (char*)bio->ptr + bio->rdIdx, len);
  551. bio->rdIdx += len;
  552. }
  553. return len;
  554. }
  555. static WOLFSSL_BIO_METHOD* wolfSSL_BIO_s_fixed_mem(void)
  556. {
  557. static WOLFSSL_BIO_METHOD meth;
  558. meth.type = WOLFSSL_BIO_BIO;
  559. XMEMCPY(meth.name, "Fixed Memory Size", 18);
  560. meth.writeCb = wolfssl_bio_s_fixed_mem_write;
  561. meth.readCb = wolfssl_bio_s_fixed_mem_read;
  562. return &meth;
  563. }
  564. #endif
  565. /*----------------------------------------------------------------------------*
  566. | Setup
  567. *----------------------------------------------------------------------------*/
  568. static int test_wolfSSL_Init(void)
  569. {
  570. EXPECT_DECLS;
  571. ExpectIntEQ(wolfSSL_Init(), WOLFSSL_SUCCESS);
  572. return EXPECT_RESULT();
  573. }
  574. static int test_wolfSSL_Cleanup(void)
  575. {
  576. EXPECT_DECLS;
  577. ExpectIntEQ(wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
  578. return EXPECT_RESULT();
  579. }
  580. /* Initialize the wolfCrypt state.
  581. * POST: 0 success.
  582. */
  583. static int test_wolfCrypt_Init(void)
  584. {
  585. EXPECT_DECLS;
  586. ExpectIntEQ(wolfCrypt_Init(), 0);
  587. return EXPECT_RESULT();
  588. } /* END test_wolfCrypt_Init */
  589. static int test_wolfCrypt_Cleanup(void)
  590. {
  591. EXPECT_DECLS;
  592. ExpectIntEQ(wolfCrypt_Cleanup(), 0);
  593. return EXPECT_RESULT();
  594. }
  595. #ifdef WOLFSSL_STATIC_MEMORY
  596. #define TEST_LSM_STATIC_SIZE 440000
  597. /* Create new bucket list, using the default list, adding
  598. * one dang large buffer size. */
  599. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  600. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  601. #define TEST_LSM_DIST WOLFMEM_DIST,1
  602. #endif
  603. static int test_wc_LoadStaticMemory_ex(void)
  604. {
  605. EXPECT_DECLS;
  606. #ifdef WOLFSSL_STATIC_MEMORY
  607. byte staticMemory[TEST_LSM_STATIC_SIZE];
  608. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  609. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  610. WOLFSSL_HEAP_HINT* heap;
  611. /* For this test, the size and dist lists will be the ones configured
  612. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  613. * so these lists are 10 long. For most tests, the value of
  614. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  615. * to that, to make sure the list size is larger than
  616. * WOLFMEM_MAX_BUCKETS. */
  617. /* Pass in zero everything. */
  618. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  619. BAD_FUNC_ARG);
  620. /* Set the heap pointer to NULL. */
  621. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  622. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  623. staticMemory, (word32)sizeof(staticMemory),
  624. 0, 1),
  625. BAD_FUNC_ARG);
  626. /* Set other pointer values to NULL one at a time. */
  627. heap = NULL;
  628. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  629. WOLFMEM_DEF_BUCKETS, NULL, distList,
  630. staticMemory, (word32)sizeof(staticMemory),
  631. 0, 1),
  632. BAD_FUNC_ARG);
  633. heap = NULL;
  634. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  635. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  636. staticMemory, (word32)sizeof(staticMemory),
  637. 0, 1),
  638. BAD_FUNC_ARG);
  639. heap = NULL;
  640. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  641. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  642. NULL, (word32)sizeof(staticMemory),
  643. 0, 1),
  644. BAD_FUNC_ARG);
  645. /* Set the size of the static buffer to 0. */
  646. heap = NULL;
  647. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  648. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  649. staticMemory, 0,
  650. 0, 1),
  651. BUFFER_E);
  652. /* Set the size of the static buffer to one less than minimum allowed. */
  653. heap = NULL;
  654. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  655. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  656. staticMemory,
  657. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  658. 0, 1),
  659. BUFFER_E);
  660. /* Set the size of the static buffer to exactly the minimum size. */
  661. heap = NULL;
  662. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  663. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  664. staticMemory,
  665. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  666. 0, 1),
  667. 0);
  668. wc_UnloadStaticMemory(heap);
  669. /* Use more buckets than able. Success case. */
  670. heap = NULL;
  671. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  672. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  673. staticMemory, (word32)sizeof(staticMemory),
  674. 0, 1),
  675. 0);
  676. wc_UnloadStaticMemory(heap);
  677. /* Success case. */
  678. heap = NULL;
  679. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  680. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  681. staticMemory, (word32)sizeof(staticMemory),
  682. 0, 1),
  683. 0);
  684. wc_UnloadStaticMemory(heap);
  685. #endif /* WOLFSSL_STATIC_MEMORY */
  686. return EXPECT_RESULT();
  687. }
  688. /*----------------------------------------------------------------------------*
  689. | Platform dependent function test
  690. *----------------------------------------------------------------------------*/
  691. static int test_fileAccess(void)
  692. {
  693. EXPECT_DECLS;
  694. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  695. const char *fname[] = {
  696. svrCertFile, svrKeyFile, caCertFile,
  697. eccCertFile, eccKeyFile, eccRsaCertFile,
  698. cliCertFile, cliCertDerFile, cliKeyFile,
  699. dhParamFile,
  700. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  701. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  702. NULL
  703. };
  704. const char derfile[] = "./certs/server-cert.der";
  705. XFILE f = XBADFILE;
  706. size_t sz;
  707. byte *buff = NULL;
  708. int i;
  709. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  710. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  711. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  712. XFCLOSE(f);
  713. }
  714. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  715. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  716. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  717. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  718. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  719. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  720. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  721. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  722. XFCLOSE(f);
  723. #endif
  724. return EXPECT_RESULT();
  725. }
  726. /*----------------------------------------------------------------------------*
  727. | Method Allocators
  728. *----------------------------------------------------------------------------*/
  729. static int test_wolfSSL_Method_Allocators(void)
  730. {
  731. EXPECT_DECLS;
  732. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  733. do { \
  734. WOLFSSL_METHOD *method = NULL; \
  735. condition(method = allocator()); \
  736. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  737. } while (0)
  738. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  739. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  740. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  741. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  742. #ifndef NO_OLD_TLS
  743. #ifdef WOLFSSL_ALLOW_SSLV3
  744. #ifndef NO_WOLFSSL_SERVER
  745. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  746. #endif
  747. #ifndef NO_WOLFSSL_CLIENT
  748. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  749. #endif
  750. #endif
  751. #ifdef WOLFSSL_ALLOW_TLSV10
  752. #ifndef NO_WOLFSSL_SERVER
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  754. #endif
  755. #ifndef NO_WOLFSSL_CLIENT
  756. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  757. #endif
  758. #endif
  759. #ifndef NO_WOLFSSL_SERVER
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  761. #endif
  762. #ifndef NO_WOLFSSL_CLIENT
  763. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  764. #endif
  765. #endif /* !NO_OLD_TLS */
  766. #ifndef WOLFSSL_NO_TLS12
  767. #ifndef NO_WOLFSSL_SERVER
  768. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  769. #endif
  770. #ifndef NO_WOLFSSL_CLIENT
  771. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  772. #endif
  773. #endif /* !WOLFSSL_NO_TLS12 */
  774. #ifdef WOLFSSL_TLS13
  775. #ifndef NO_WOLFSSL_SERVER
  776. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  777. #endif
  778. #ifndef NO_WOLFSSL_CLIENT
  779. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  780. #endif
  781. #endif /* WOLFSSL_TLS13 */
  782. #ifndef NO_WOLFSSL_SERVER
  783. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  784. #endif
  785. #ifndef NO_WOLFSSL_CLIENT
  786. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  787. #endif
  788. #ifdef WOLFSSL_DTLS
  789. #ifndef NO_OLD_TLS
  790. #ifndef NO_WOLFSSL_SERVER
  791. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  792. #endif
  793. #ifndef NO_WOLFSSL_CLIENT
  794. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  795. #endif
  796. #endif
  797. #ifndef WOLFSSL_NO_TLS12
  798. #ifndef NO_WOLFSSL_SERVER
  799. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  800. #endif
  801. #ifndef NO_WOLFSSL_CLIENT
  802. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  803. #endif
  804. #endif
  805. #endif /* WOLFSSL_DTLS */
  806. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  807. /* Stubs */
  808. #ifndef NO_WOLFSSL_SERVER
  809. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  810. #endif
  811. #ifndef NO_WOLFSSL_CLIENT
  812. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  813. #endif
  814. #endif
  815. /* Test Either Method (client or server) */
  816. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  817. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  818. #ifndef NO_OLD_TLS
  819. #ifdef WOLFSSL_ALLOW_TLSV10
  820. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  821. #endif
  822. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  823. #endif /* !NO_OLD_TLS */
  824. #ifndef WOLFSSL_NO_TLS12
  825. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  826. #endif /* !WOLFSSL_NO_TLS12 */
  827. #ifdef WOLFSSL_TLS13
  828. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  829. #endif /* WOLFSSL_TLS13 */
  830. #ifdef WOLFSSL_DTLS
  831. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  832. #ifndef NO_OLD_TLS
  833. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  834. #endif /* !NO_OLD_TLS */
  835. #ifndef WOLFSSL_NO_TLS12
  836. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  837. #endif /* !WOLFSSL_NO_TLS12 */
  838. #endif /* WOLFSSL_DTLS */
  839. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  840. return EXPECT_RESULT();
  841. }
  842. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  843. /*----------------------------------------------------------------------------*
  844. | Dual algorithm Certificate Tests
  845. *----------------------------------------------------------------------------*/
  846. #define LARGE_TEMP_SZ 4096
  847. /* To better understand this, please see the X9.146 example in wolfssl-examples
  848. * repo. */
  849. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  850. char *sapkiFile, char *altPrivFile)
  851. {
  852. EXPECT_DECLS;
  853. FILE* file = NULL;
  854. Cert newCert;
  855. DecodedCert preTBS;
  856. byte caKeyBuf[LARGE_TEMP_SZ];
  857. word32 caKeySz = LARGE_TEMP_SZ;
  858. byte sapkiBuf[LARGE_TEMP_SZ];
  859. word32 sapkiSz = LARGE_TEMP_SZ;
  860. byte altPrivBuf[LARGE_TEMP_SZ];
  861. word32 altPrivSz = LARGE_TEMP_SZ;
  862. byte altSigAlgBuf[LARGE_TEMP_SZ];
  863. word32 altSigAlgSz = LARGE_TEMP_SZ;
  864. byte scratchBuf[LARGE_TEMP_SZ];
  865. word32 scratchSz = LARGE_TEMP_SZ;
  866. byte preTbsBuf[LARGE_TEMP_SZ];
  867. word32 preTbsSz = LARGE_TEMP_SZ;
  868. byte altSigValBuf[LARGE_TEMP_SZ];
  869. word32 altSigValSz = LARGE_TEMP_SZ;
  870. byte *outBuf = NULL;
  871. word32 outSz = LARGE_TEMP_SZ;
  872. WC_RNG rng;
  873. RsaKey caKey;
  874. ecc_key altCaKey;
  875. word32 idx = 0;
  876. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  877. DYNAMIC_TYPE_TMP_BUFFER));
  878. ExpectIntEQ(wc_InitRng(&rng), 0);
  879. XMEMSET(caKeyBuf, 0, caKeySz);
  880. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  881. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  882. if (file) {
  883. fclose(file);
  884. file = NULL;
  885. }
  886. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  887. idx = 0;
  888. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  889. 0);
  890. XMEMSET(sapkiBuf, 0, sapkiSz);
  891. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  892. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  893. if (file) {
  894. fclose(file);
  895. file = NULL;
  896. }
  897. XMEMSET(altPrivBuf, 0, altPrivSz);
  898. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  899. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  900. if (file) {
  901. fclose(file);
  902. file = NULL;
  903. }
  904. wc_ecc_init(&altCaKey);
  905. idx = 0;
  906. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  907. (word32)altPrivSz), 0);
  908. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  909. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  910. oidSigType, 0), 0);
  911. wc_InitCert(&newCert);
  912. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  913. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  914. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  915. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  916. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  917. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  918. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  919. newCert.sigType = CTC_SHA256wRSA;
  920. newCert.isCA = 1;
  921. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  922. (const byte *)"This is NOT a critical extension", 32), 0);
  923. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  924. sapkiSz), 0);
  925. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  926. altSigAlgSz), 0);
  927. XMEMSET(scratchBuf, 0, scratchSz);
  928. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  929. &caKey, &rng), 0);
  930. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  931. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  932. XMEMSET(preTbsBuf, 0, preTbsSz);
  933. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  934. XMEMSET(altSigValBuf, 0, altSigValSz);
  935. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  936. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  937. &rng), 0);
  938. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  939. altSigValSz), 0);
  940. /* Finally, generate the new certificate. */
  941. XMEMSET(outBuf, 0, outSz);
  942. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  943. 0);
  944. *out = outBuf;
  945. wc_FreeRsaKey(&caKey);
  946. wc_FreeRng(&rng);
  947. wc_FreeDecodedCert(&preTBS);
  948. return outSz;
  949. }
  950. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  951. char *sapkiFile, char *altPrivFile,
  952. char *serverKeyFile,
  953. byte *caCertBuf, int caCertSz)
  954. {
  955. EXPECT_DECLS;
  956. FILE* file = NULL;
  957. Cert newCert;
  958. DecodedCert preTBS;
  959. byte serverKeyBuf[LARGE_TEMP_SZ];
  960. word32 serverKeySz = LARGE_TEMP_SZ;
  961. byte caKeyBuf[LARGE_TEMP_SZ];
  962. word32 caKeySz = LARGE_TEMP_SZ;
  963. byte sapkiBuf[LARGE_TEMP_SZ];
  964. word32 sapkiSz = LARGE_TEMP_SZ;
  965. byte altPrivBuf[LARGE_TEMP_SZ];
  966. word32 altPrivSz = LARGE_TEMP_SZ;
  967. byte altSigAlgBuf[LARGE_TEMP_SZ];
  968. word32 altSigAlgSz = LARGE_TEMP_SZ;
  969. byte scratchBuf[LARGE_TEMP_SZ];
  970. word32 scratchSz = LARGE_TEMP_SZ;
  971. byte preTbsBuf[LARGE_TEMP_SZ];
  972. word32 preTbsSz = LARGE_TEMP_SZ;
  973. byte altSigValBuf[LARGE_TEMP_SZ];
  974. word32 altSigValSz = LARGE_TEMP_SZ;
  975. byte *outBuf = NULL;
  976. word32 outSz = LARGE_TEMP_SZ;
  977. WC_RNG rng;
  978. RsaKey caKey;
  979. RsaKey serverKey;
  980. ecc_key altCaKey;
  981. word32 idx = 0;
  982. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  983. DYNAMIC_TYPE_TMP_BUFFER));
  984. ExpectIntEQ(wc_InitRng(&rng), 0);
  985. XMEMSET(serverKeyBuf, 0, serverKeySz);
  986. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  987. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  988. 0);
  989. if (file) {
  990. fclose(file);
  991. file = NULL;
  992. }
  993. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  994. idx = 0;
  995. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  996. (word32)serverKeySz), 0);
  997. XMEMSET(caKeyBuf, 0, caKeySz);
  998. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  999. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  1000. if (file) {
  1001. fclose(file);
  1002. file = NULL;
  1003. }
  1004. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1005. idx = 0;
  1006. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1007. (word32)caKeySz), 0);
  1008. XMEMSET(sapkiBuf, 0, sapkiSz);
  1009. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1010. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1011. if (file) {
  1012. fclose(file);
  1013. file = NULL;
  1014. }
  1015. XMEMSET(altPrivBuf, 0, altPrivSz);
  1016. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1017. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1018. if (file) {
  1019. fclose(file);
  1020. file = NULL;
  1021. }
  1022. wc_ecc_init(&altCaKey);
  1023. idx = 0;
  1024. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1025. (word32)altPrivSz), 0);
  1026. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1027. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1028. oidSigType, 0), 0);
  1029. wc_InitCert(&newCert);
  1030. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1031. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1032. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1033. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1034. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1035. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1036. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1037. newCert.sigType = CTC_SHA256wRSA;
  1038. newCert.isCA = 0;
  1039. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1040. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1041. (const byte *)"This is NOT a critical extension", 32), 0);
  1042. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1043. sapkiSz), 0);
  1044. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1045. altSigAlgSz), 0);
  1046. XMEMSET(scratchBuf, 0, scratchSz);
  1047. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1048. &rng), 0);
  1049. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1050. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1051. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1052. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1053. XMEMSET(preTbsBuf, 0, preTbsSz);
  1054. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1055. XMEMSET(altSigValBuf, 0, altSigValSz);
  1056. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1057. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1058. &rng), 0);
  1059. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1060. altSigValBuf, altSigValSz), 0);
  1061. /* Finally, generate the new certificate. */
  1062. XMEMSET(outBuf, 0, outSz);
  1063. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1064. 0);
  1065. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1066. outSz, &caKey, NULL, &rng), 0);
  1067. *out = outBuf;
  1068. wc_FreeRsaKey(&caKey);
  1069. wc_FreeRsaKey(&serverKey);
  1070. wc_FreeRng(&rng);
  1071. wc_FreeDecodedCert(&preTBS);
  1072. return outSz;
  1073. }
  1074. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1075. byte *serverCert, word32 serverCertSz,
  1076. byte *serverKey, word32 serverKeySz,
  1077. int negative_test)
  1078. {
  1079. EXPECT_DECLS;
  1080. WOLFSSL_CTX *ctx_c = NULL;
  1081. WOLFSSL_CTX *ctx_s = NULL;
  1082. WOLFSSL *ssl_c = NULL;
  1083. WOLFSSL *ssl_s = NULL;
  1084. struct test_memio_ctx test_ctx;
  1085. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1086. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1087. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1088. caCert, caCertSz, serverCert, serverCertSz,
  1089. serverKey, serverKeySz), 0);
  1090. if (negative_test) {
  1091. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1092. }
  1093. else {
  1094. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1095. }
  1096. wolfSSL_free(ssl_c);
  1097. wolfSSL_free(ssl_s);
  1098. wolfSSL_CTX_free(ctx_c);
  1099. wolfSSL_CTX_free(ctx_s);
  1100. return EXPECT_RESULT();
  1101. }
  1102. static int extCount = 0;
  1103. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1104. const unsigned char* der, word32 derSz)
  1105. {
  1106. (void) oid;
  1107. (void) oidSz;
  1108. (void) crit;
  1109. (void) der;
  1110. (void) derSz;
  1111. extCount ++;
  1112. /* Accept all extensions. This is only a test. Normally we would be much more
  1113. * careful about critical extensions. */
  1114. return 1;
  1115. }
  1116. static int test_dual_alg_support(void)
  1117. {
  1118. EXPECT_DECLS;
  1119. /* Root CA and server keys will be the same. This is only appropriate for
  1120. * testing. */
  1121. char keyFile[] = "./certs/ca-key.der";
  1122. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1123. char altPrivFile[] = "./certs/ecc-key.der";
  1124. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1125. byte *serverKey = NULL;
  1126. size_t serverKeySz = 0;
  1127. byte *root = NULL;
  1128. int rootSz = 0;
  1129. byte *server = NULL;
  1130. int serverSz = 0;
  1131. WOLFSSL_CERT_MANAGER* cm = NULL;
  1132. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1133. /* Base normal case. */
  1134. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1135. ExpectNotNull(root);
  1136. ExpectIntGT(rootSz, 0);
  1137. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1138. altPrivFile, keyFile, root, rootSz);
  1139. ExpectNotNull(server);
  1140. ExpectIntGT(serverSz, 0);
  1141. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1142. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1143. TEST_SUCCESS);
  1144. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1145. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1146. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1147. * the alternative signature and then set negative_test to true for the
  1148. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1149. * because the signature won't verify. The exception is if
  1150. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1151. * and this is no longer a negative test. */
  1152. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1153. ExpectNotNull(root);
  1154. ExpectIntGT(rootSz, 0);
  1155. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1156. wrongPrivFile, keyFile, root, rootSz);
  1157. ExpectNotNull(server);
  1158. ExpectIntGT(serverSz, 0);
  1159. #ifdef WOLFSSL_TRUST_PEER_CERT
  1160. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1161. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1162. TEST_SUCCESS);
  1163. #else
  1164. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1165. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1166. TEST_SUCCESS);
  1167. #endif
  1168. /* Lets see if CertManager can find the new extensions */
  1169. extCount = 0;
  1170. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1171. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1172. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1173. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1174. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1175. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1176. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1177. * because they are for the dual alg extensions. */
  1178. ExpectIntEQ(extCount, 1);
  1179. wolfSSL_CertManagerFree(cm);
  1180. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1181. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1182. free(serverKey);
  1183. return EXPECT_RESULT();
  1184. }
  1185. #else
  1186. static int test_dual_alg_support(void)
  1187. {
  1188. return TEST_SKIPPED;
  1189. }
  1190. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1191. /*----------------------------------------------------------------------------*
  1192. | Context
  1193. *----------------------------------------------------------------------------*/
  1194. #ifndef NO_WOLFSSL_SERVER
  1195. static int test_wolfSSL_CTX_new(void)
  1196. {
  1197. EXPECT_DECLS;
  1198. WOLFSSL_CTX *ctx;
  1199. WOLFSSL_METHOD* method;
  1200. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1201. ExpectNotNull(method = wolfSSLv23_server_method());
  1202. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1203. wolfSSL_CTX_free(ctx);
  1204. return EXPECT_RESULT();
  1205. }
  1206. #endif
  1207. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1208. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1209. static int test_for_double_Free(void)
  1210. {
  1211. EXPECT_DECLS;
  1212. WOLFSSL_CTX* ctx = NULL;
  1213. WOLFSSL* ssl = NULL;
  1214. int skipTest = 0;
  1215. const char* testCertFile;
  1216. const char* testKeyFile;
  1217. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1218. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1219. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1220. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1221. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1222. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1223. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1224. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1225. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1226. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1227. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1228. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1229. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1230. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1231. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1232. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1233. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1234. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1235. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1236. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1237. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1238. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1239. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1240. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1241. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1242. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1243. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1244. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1245. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1246. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1247. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1248. #ifdef OPENSSL_EXTRA
  1249. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1250. "!SRP:!kRSA:!aNULL:!eNULL";
  1251. #endif
  1252. #ifndef NO_RSA
  1253. testCertFile = svrCertFile;
  1254. testKeyFile = svrKeyFile;
  1255. #elif defined(HAVE_ECC)
  1256. testCertFile = eccCertFile;
  1257. testKeyFile = eccKeyFile;
  1258. #else
  1259. skipTest = 1;
  1260. #endif
  1261. if (skipTest != 1) {
  1262. #ifndef NO_WOLFSSL_SERVER
  1263. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1264. #else
  1265. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1266. #endif
  1267. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1268. WOLFSSL_FILETYPE_PEM));
  1269. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1270. WOLFSSL_FILETYPE_PEM));
  1271. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1272. /* First test freeing SSL, then CTX */
  1273. wolfSSL_free(ssl);
  1274. ssl = NULL;
  1275. wolfSSL_CTX_free(ctx);
  1276. ctx = NULL;
  1277. #ifndef NO_WOLFSSL_CLIENT
  1278. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1279. #else
  1280. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1281. #endif
  1282. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1283. WOLFSSL_FILETYPE_PEM));
  1284. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1285. WOLFSSL_FILETYPE_PEM));
  1286. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1287. /* Next test freeing CTX then SSL */
  1288. wolfSSL_CTX_free(ctx);
  1289. ctx = NULL;
  1290. wolfSSL_free(ssl);
  1291. ssl = NULL;
  1292. #ifndef NO_WOLFSSL_SERVER
  1293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1294. #else
  1295. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1296. #endif
  1297. /* Test setting ciphers at ctx level */
  1298. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1299. WOLFSSL_FILETYPE_PEM));
  1300. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1301. WOLFSSL_FILETYPE_PEM));
  1302. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1303. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1304. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1305. /* only update TLSv13 suites */
  1306. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1307. #endif
  1308. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1309. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1310. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1311. /* only update pre-TLSv13 suites */
  1312. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1313. "ECDHE-RSA-AES128-GCM-SHA256"));
  1314. #endif
  1315. #ifdef OPENSSL_EXTRA
  1316. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1317. #endif
  1318. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1319. wolfSSL_CTX_free(ctx);
  1320. ctx = NULL;
  1321. wolfSSL_free(ssl);
  1322. ssl = NULL;
  1323. #ifndef NO_WOLFSSL_CLIENT
  1324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1325. #else
  1326. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1327. #endif
  1328. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1329. WOLFSSL_FILETYPE_PEM));
  1330. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1331. WOLFSSL_FILETYPE_PEM));
  1332. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1333. /* test setting ciphers at SSL level */
  1334. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1335. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1336. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1337. /* only update TLSv13 suites */
  1338. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1339. #endif
  1340. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1341. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1342. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1343. /* only update pre-TLSv13 suites */
  1344. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1345. #endif
  1346. wolfSSL_CTX_free(ctx);
  1347. ctx = NULL;
  1348. wolfSSL_free(ssl);
  1349. ssl = NULL;
  1350. }
  1351. return EXPECT_RESULT();
  1352. }
  1353. #endif
  1354. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1355. {
  1356. EXPECT_DECLS;
  1357. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1358. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1359. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1360. const char* testCertFile;
  1361. const char* testKeyFile;
  1362. WOLFSSL_CTX* ctx = NULL;
  1363. WOLFSSL* ssl = NULL;
  1364. const byte cipherList[] =
  1365. {
  1366. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1367. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1368. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1369. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1370. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1371. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1372. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1373. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1374. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1375. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1376. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1377. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1378. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1379. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1380. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1381. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1382. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1383. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1384. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1385. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1386. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1387. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1388. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1389. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1390. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1391. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1392. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1393. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1394. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1395. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1396. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1397. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1398. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1399. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1400. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1401. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1402. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1403. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1404. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1405. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1406. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1407. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1408. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1409. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1410. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1411. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1412. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1413. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1414. /* SHA256 */
  1415. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1416. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1417. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1418. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1419. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1420. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1421. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1422. /* SHA384 */
  1423. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1424. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1425. /* AES-GCM */
  1426. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1427. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1428. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1429. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1430. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1431. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1432. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1433. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1434. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1435. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1436. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1437. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1438. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1439. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1440. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1441. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1442. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1443. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1444. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1445. * also, in some of the other AES-CCM suites
  1446. * there will be second byte number conflicts
  1447. * with non-ECC AES-GCM */
  1448. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1449. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1450. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1451. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1452. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1453. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1454. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1455. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1456. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1457. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1458. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1459. /* Camellia */
  1460. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1461. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1462. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1463. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1464. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1465. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1466. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1467. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1468. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1469. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1470. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1471. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1472. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1473. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1474. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1475. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1476. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1477. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1478. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1479. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1480. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1481. /* TLS v1.3 cipher suites */
  1482. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1483. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1484. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1485. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1486. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1487. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1488. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1489. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1490. };
  1491. #ifndef NO_RSA
  1492. testCertFile = svrCertFile;
  1493. testKeyFile = svrKeyFile;
  1494. #elif defined(HAVE_ECC)
  1495. testCertFile = eccCertFile;
  1496. testKeyFile = eccKeyFile;
  1497. #endif
  1498. #ifndef NO_WOLFSSL_SERVER
  1499. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1500. #else
  1501. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1502. #endif
  1503. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1504. sizeof(cipherList)));
  1505. wolfSSL_CTX_free(ctx);
  1506. ctx = NULL;
  1507. #ifndef NO_WOLFSSL_SERVER
  1508. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1509. #else
  1510. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1511. #endif
  1512. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1513. WOLFSSL_FILETYPE_PEM));
  1514. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1515. WOLFSSL_FILETYPE_PEM));
  1516. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1517. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1518. sizeof(cipherList)));
  1519. wolfSSL_free(ssl);
  1520. wolfSSL_CTX_free(ctx);
  1521. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1522. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1523. return EXPECT_RESULT();
  1524. }
  1525. static int test_wolfSSL_CTX_use_certificate_file(void)
  1526. {
  1527. EXPECT_DECLS;
  1528. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1529. WOLFSSL_CTX *ctx = NULL;
  1530. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1531. /* invalid context */
  1532. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1533. WOLFSSL_FILETYPE_PEM));
  1534. /* invalid cert file */
  1535. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1536. WOLFSSL_FILETYPE_PEM));
  1537. /* invalid cert type */
  1538. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1539. #ifdef NO_RSA
  1540. /* rsa needed */
  1541. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1542. WOLFSSL_FILETYPE_PEM));
  1543. #else
  1544. /* success */
  1545. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1546. WOLFSSL_FILETYPE_PEM));
  1547. #endif
  1548. wolfSSL_CTX_free(ctx);
  1549. #endif
  1550. return EXPECT_RESULT();
  1551. }
  1552. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1553. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1554. {
  1555. EXPECT_DECLS;
  1556. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1557. WOLFSSL_CTX* ctx = NULL;
  1558. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1559. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1560. server_cert_der_2048), WOLFSSL_SUCCESS);
  1561. wolfSSL_CTX_free(ctx);
  1562. #endif
  1563. return EXPECT_RESULT();
  1564. }
  1565. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1566. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1567. * context using buffer.
  1568. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1569. * --enable-testcert flag.
  1570. */
  1571. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1572. {
  1573. EXPECT_DECLS;
  1574. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1575. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1576. WOLFSSL_CTX* ctx = NULL;
  1577. int ret;
  1578. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1579. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1580. server_cert_der_2048, sizeof_server_cert_der_2048,
  1581. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1582. wolfSSL_CTX_free(ctx);
  1583. #endif
  1584. return EXPECT_RESULT();
  1585. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1586. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1587. {
  1588. EXPECT_DECLS;
  1589. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1590. WOLFSSL_CTX *ctx = NULL;
  1591. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1592. /* invalid context */
  1593. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1594. WOLFSSL_FILETYPE_PEM));
  1595. /* invalid key file */
  1596. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1597. WOLFSSL_FILETYPE_PEM));
  1598. /* invalid key type */
  1599. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1600. /* success */
  1601. #ifdef NO_RSA
  1602. /* rsa needed */
  1603. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1604. WOLFSSL_FILETYPE_PEM));
  1605. #else
  1606. /* success */
  1607. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1608. WOLFSSL_FILETYPE_PEM));
  1609. #endif
  1610. wolfSSL_CTX_free(ctx);
  1611. #endif
  1612. return EXPECT_RESULT();
  1613. }
  1614. /* test both file and buffer versions along with unloading trusted peer certs */
  1615. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1616. {
  1617. EXPECT_DECLS;
  1618. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1619. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1620. WOLFSSL_CTX *ctx = NULL;
  1621. WOLFSSL* ssl = NULL;
  1622. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1623. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1624. #if !defined(NO_FILESYSTEM)
  1625. /* invalid file */
  1626. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1627. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1628. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1629. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1630. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1631. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1632. /* success */
  1633. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1634. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1635. /* unload cert */
  1636. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1637. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1638. /* invalid file */
  1639. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1640. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1641. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1642. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1643. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1644. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1645. /* success */
  1646. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1647. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1648. #ifdef WOLFSSL_LOCAL_X509_STORE
  1649. /* unload cert */
  1650. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1651. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1652. #endif
  1653. #endif
  1654. /* Test of loading certs from buffers */
  1655. /* invalid buffer */
  1656. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1657. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1658. /* success */
  1659. #ifdef USE_CERT_BUFFERS_1024
  1660. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1661. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1662. #endif
  1663. #ifdef USE_CERT_BUFFERS_2048
  1664. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1665. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1666. #endif
  1667. /* unload cert */
  1668. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1669. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1670. wolfSSL_free(ssl);
  1671. wolfSSL_CTX_free(ctx);
  1672. #endif
  1673. return EXPECT_RESULT();
  1674. }
  1675. static int test_wolfSSL_CTX_load_verify_locations(void)
  1676. {
  1677. EXPECT_DECLS;
  1678. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1679. WOLFSSL_CTX *ctx = NULL;
  1680. #ifndef NO_RSA
  1681. WOLFSSL_CERT_MANAGER* cm = NULL;
  1682. #ifdef PERSIST_CERT_CACHE
  1683. int cacheSz = 0;
  1684. unsigned char* cache = NULL;
  1685. int used = 0;
  1686. #ifndef NO_FILESYSTEM
  1687. const char* cacheFile = "./tests/cert_cache.tmp";
  1688. #endif
  1689. int i;
  1690. int t;
  1691. int* p;
  1692. #endif
  1693. #endif
  1694. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1695. const char* load_certs_path = "./certs/external";
  1696. const char* load_no_certs_path = "./examples";
  1697. const char* load_expired_path = "./certs/test/expired";
  1698. #endif
  1699. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1700. /* invalid arguments */
  1701. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1702. WOLFSSL_FAILURE);
  1703. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1704. WOLFSSL_FAILURE);
  1705. /* invalid ca file */
  1706. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1707. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1708. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1709. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1710. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1711. /* invalid path */
  1712. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1713. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1714. #endif
  1715. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1716. /* test ignoring the invalid path */
  1717. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1718. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1719. #endif
  1720. /* load ca cert */
  1721. #ifdef NO_RSA
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1723. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1724. #else /* Skip the following test without RSA certs. */
  1725. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1726. WOLFSSL_SUCCESS);
  1727. #ifdef PERSIST_CERT_CACHE
  1728. /* Get cert cache size */
  1729. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1730. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1731. DYNAMIC_TYPE_TMP_BUFFER));
  1732. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1733. BAD_FUNC_ARG);
  1734. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1735. BAD_FUNC_ARG);
  1736. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1737. BAD_FUNC_ARG);
  1738. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1739. BAD_FUNC_ARG);
  1740. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1741. BAD_FUNC_ARG);
  1742. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1743. BAD_FUNC_ARG);
  1744. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1745. BAD_FUNC_ARG);
  1746. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1747. BAD_FUNC_ARG);
  1748. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1749. BAD_FUNC_ARG);
  1750. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1751. BUFFER_E);
  1752. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1753. ExpectIntEQ(cacheSz, used);
  1754. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1755. BAD_FUNC_ARG);
  1756. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1757. BAD_FUNC_ARG);
  1758. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1759. BAD_FUNC_ARG);
  1760. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1761. BAD_FUNC_ARG);
  1762. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1763. BAD_FUNC_ARG);
  1764. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1765. BAD_FUNC_ARG);
  1766. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1767. BAD_FUNC_ARG);
  1768. /* Smaller than header. */
  1769. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1770. for (i = 1; i < cacheSz; i++) {
  1771. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1772. BUFFER_E);
  1773. }
  1774. if (EXPECT_SUCCESS()) {
  1775. /* Modify header for bad results! */
  1776. p = (int*)cache;
  1777. /* version */
  1778. t = p[0]; p[0] = 0xff;
  1779. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1780. CACHE_MATCH_ERROR);
  1781. p[0] = t; p++;
  1782. /* rows */
  1783. t = p[0]; p[0] = 0xff;
  1784. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1785. CACHE_MATCH_ERROR);
  1786. p[0] = t; p++;
  1787. /* columns[0] */
  1788. t = p[0]; p[0] = -1;
  1789. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1790. PARSE_ERROR);
  1791. p[0] = t; p += CA_TABLE_SIZE;
  1792. /* signerSz*/
  1793. t = p[0]; p[0] = 0xff;
  1794. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1795. CACHE_MATCH_ERROR);
  1796. p[0] = t;
  1797. }
  1798. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1799. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1800. #ifndef NO_FILESYSTEM
  1801. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1802. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1803. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1804. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1805. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1806. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1807. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1808. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1809. WOLFSSL_BAD_FILE);
  1810. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1811. /* File contents is not a cache. */
  1812. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1813. CACHE_MATCH_ERROR);
  1814. #endif
  1815. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1816. #endif
  1817. /* Test unloading CA's */
  1818. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1819. #ifdef PERSIST_CERT_CACHE
  1820. /* Verify no certs (result is less than cacheSz) */
  1821. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1822. #endif
  1823. /* load ca cert again */
  1824. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1825. WOLFSSL_SUCCESS);
  1826. /* Test getting CERT_MANAGER */
  1827. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1828. /* Test unloading CA's using CM */
  1829. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1830. #ifdef PERSIST_CERT_CACHE
  1831. /* Verify no certs (result is less than cacheSz) */
  1832. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1833. #endif
  1834. #endif
  1835. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1836. /* Test loading CA certificates using a path */
  1837. #ifdef NO_RSA
  1838. /* failure here okay since certs in external directory are RSA */
  1839. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1840. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1841. #else
  1842. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1843. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1844. #endif
  1845. /* Test loading path with no files */
  1846. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1847. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1848. /* Test loading expired CA certificates */
  1849. #ifdef NO_RSA
  1850. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1851. load_expired_path,
  1852. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1853. WOLFSSL_SUCCESS);
  1854. #else
  1855. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1856. load_expired_path,
  1857. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1858. WOLFSSL_SUCCESS);
  1859. #endif
  1860. /* Test loading CA certificates and ignoring all errors */
  1861. #ifdef NO_RSA
  1862. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1863. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1864. #else
  1865. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1866. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1867. #endif
  1868. #endif
  1869. wolfSSL_CTX_free(ctx);
  1870. #endif
  1871. return EXPECT_RESULT();
  1872. }
  1873. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1874. {
  1875. int res = TEST_SKIPPED;
  1876. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1877. (!defined(NO_RSA) || defined(HAVE_ECC))
  1878. WOLFSSL_CTX* ctx;
  1879. byte dirValid = 0;
  1880. int ret = 0;
  1881. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1882. if (ctx == NULL) {
  1883. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1884. ret = -1;
  1885. }
  1886. if (ret == 0) {
  1887. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1888. dirValid = 1;
  1889. #else
  1890. word32 numDirs;
  1891. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1892. if (caDirs == NULL || numDirs == 0) {
  1893. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1894. ret = -1;
  1895. }
  1896. else {
  1897. ReadDirCtx dirCtx;
  1898. word32 i;
  1899. for (i = 0; i < numDirs; ++i) {
  1900. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1901. /* Directory isn't empty. */
  1902. dirValid = 1;
  1903. wc_ReadDirClose(&dirCtx);
  1904. break;
  1905. }
  1906. }
  1907. }
  1908. #endif
  1909. }
  1910. /*
  1911. * If the directory isn't empty, we should be able to load CA
  1912. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1913. * usable.
  1914. */
  1915. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1916. WOLFSSL_SUCCESS) {
  1917. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1918. ret = -1;
  1919. }
  1920. #ifdef OPENSSL_EXTRA
  1921. if (ret == 0 &&
  1922. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1923. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1924. ret = -1;
  1925. }
  1926. #endif /* OPENSSL_EXTRA */
  1927. wolfSSL_CTX_free(ctx);
  1928. res = TEST_RES_CHECK(ret == 0);
  1929. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1930. return res;
  1931. }
  1932. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1933. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1934. int file_type)
  1935. {
  1936. int ret;
  1937. WOLFSSL_CERT_MANAGER* cm;
  1938. cm = wolfSSL_CertManagerNew();
  1939. if (cm == NULL) {
  1940. fprintf(stderr, "test_cm_load_ca failed\n");
  1941. return -1;
  1942. }
  1943. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1944. wolfSSL_CertManagerFree(cm);
  1945. return ret;
  1946. }
  1947. static int test_cm_load_ca_file(const char* ca_cert_file)
  1948. {
  1949. int ret = 0;
  1950. byte* cert_buf = NULL;
  1951. size_t cert_sz = 0;
  1952. #if defined(WOLFSSL_PEM_TO_DER)
  1953. DerBuffer* pDer = NULL;
  1954. #endif
  1955. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1956. if (ret == 0) {
  1957. /* normal test */
  1958. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1959. if (ret == WOLFSSL_SUCCESS) {
  1960. /* test including null terminator in length */
  1961. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1962. if (tmp == NULL) {
  1963. ret = MEMORY_E;
  1964. }
  1965. else {
  1966. cert_buf = tmp;
  1967. cert_buf[cert_sz] = '\0';
  1968. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1969. WOLFSSL_FILETYPE_PEM);
  1970. }
  1971. }
  1972. #if defined(WOLFSSL_PEM_TO_DER)
  1973. if (ret == WOLFSSL_SUCCESS) {
  1974. /* test loading DER */
  1975. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1976. if (ret == 0 && pDer != NULL) {
  1977. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1978. WOLFSSL_FILETYPE_ASN1);
  1979. wc_FreeDer(&pDer);
  1980. }
  1981. }
  1982. #endif
  1983. }
  1984. free(cert_buf);
  1985. return ret;
  1986. }
  1987. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1988. int file_type, word32 flags)
  1989. {
  1990. int ret;
  1991. WOLFSSL_CERT_MANAGER* cm;
  1992. cm = wolfSSL_CertManagerNew();
  1993. if (cm == NULL) {
  1994. fprintf(stderr, "test_cm_load_ca failed\n");
  1995. return -1;
  1996. }
  1997. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1998. 0, flags);
  1999. wolfSSL_CertManagerFree(cm);
  2000. return ret;
  2001. }
  2002. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2003. {
  2004. int ret = 0;
  2005. byte* cert_buf = NULL;
  2006. size_t cert_sz = 0;
  2007. #if defined(WOLFSSL_PEM_TO_DER)
  2008. DerBuffer* pDer = NULL;
  2009. #endif
  2010. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2011. if (ret == 0) {
  2012. /* normal test */
  2013. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2014. WOLFSSL_FILETYPE_PEM, flags);
  2015. if (ret == WOLFSSL_SUCCESS) {
  2016. /* test including null terminator in length */
  2017. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2018. if (tmp == NULL) {
  2019. ret = MEMORY_E;
  2020. }
  2021. else {
  2022. cert_buf = tmp;
  2023. cert_buf[cert_sz] = '\0';
  2024. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2025. WOLFSSL_FILETYPE_PEM, flags);
  2026. }
  2027. }
  2028. #if defined(WOLFSSL_PEM_TO_DER)
  2029. if (ret == WOLFSSL_SUCCESS) {
  2030. /* test loading DER */
  2031. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2032. if (ret == 0 && pDer != NULL) {
  2033. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2034. WOLFSSL_FILETYPE_ASN1, flags);
  2035. wc_FreeDer(&pDer);
  2036. }
  2037. }
  2038. #endif
  2039. }
  2040. free(cert_buf);
  2041. return ret;
  2042. }
  2043. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2044. static int test_wolfSSL_CertManagerAPI(void)
  2045. {
  2046. EXPECT_DECLS;
  2047. #ifndef NO_CERTS
  2048. WOLFSSL_CERT_MANAGER* cm = NULL;
  2049. unsigned char c;
  2050. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2051. wolfSSL_CertManagerFree(NULL);
  2052. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2053. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2054. #ifdef WOLFSSL_TRUST_PEER_CERT
  2055. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2056. #endif
  2057. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2058. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2059. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2060. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2061. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2062. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2063. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2064. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2065. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2066. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2067. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2068. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2069. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2070. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2071. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2072. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2073. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2074. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2075. WOLFSSL_BAD_FILETYPE);
  2076. #endif
  2077. #if !defined(NO_FILESYSTEM)
  2078. {
  2079. const char* ca_cert = "./certs/ca-cert.pem";
  2080. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2081. const char* ca_cert_der = "./certs/ca-cert.der";
  2082. #endif
  2083. const char* ca_path = "./certs";
  2084. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2085. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2086. BAD_FUNC_ARG);
  2087. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2088. BAD_FUNC_ARG);
  2089. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2090. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2091. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2092. WOLFSSL_BAD_FILETYPE);
  2093. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2094. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2095. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2096. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2097. #endif
  2098. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2099. WOLFSSL_FATAL_ERROR);
  2100. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2101. WOLFSSL_FATAL_ERROR);
  2102. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2103. WOLFSSL_FATAL_ERROR);
  2104. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2105. WOLFSSL_FATAL_ERROR);
  2106. }
  2107. #endif
  2108. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2109. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2110. #elif !defined(HAVE_CRL)
  2111. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2112. #endif
  2113. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2114. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2115. #ifdef HAVE_CRL
  2116. /* Test APIs when CRL is disabled. */
  2117. #ifdef HAVE_CRL_IO
  2118. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2119. #endif
  2120. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2121. sizeof_server_cert_der_2048), 1);
  2122. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2123. #endif
  2124. /* OCSP */
  2125. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2126. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2127. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2128. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2129. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2130. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2131. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2132. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2133. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2134. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2135. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2136. #endif
  2137. #ifdef HAVE_OCSP
  2138. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2139. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2140. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2141. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2142. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2143. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2144. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2145. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2146. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2147. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2148. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2149. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2150. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2151. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2152. BAD_FUNC_ARG);
  2153. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2154. BAD_FUNC_ARG);
  2155. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2156. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2157. BAD_FUNC_ARG);
  2158. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2159. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2160. /* Test APIs when OCSP is disabled. */
  2161. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2162. NULL, NULL, NULL, NULL), 1);
  2163. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2164. #endif
  2165. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2166. if (EXPECT_SUCCESS()) {
  2167. wolfSSL_CertManagerFree(cm);
  2168. }
  2169. wolfSSL_CertManagerFree(cm);
  2170. cm = NULL;
  2171. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2172. #ifdef HAVE_OCSP
  2173. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2174. WOLFSSL_OCSP_CHECKALL), 1);
  2175. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2176. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2177. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2178. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2179. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2180. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2181. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2182. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2183. #endif
  2184. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2185. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2186. #endif
  2187. #ifdef WOLFSSL_TRUST_PEER_CERT
  2188. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2189. #endif
  2190. wolfSSL_CertManagerFree(cm);
  2191. #endif
  2192. return EXPECT_RESULT();
  2193. }
  2194. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2195. {
  2196. EXPECT_DECLS;
  2197. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2198. const char* ca_cert = "./certs/ca-cert.pem";
  2199. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2200. int ret;
  2201. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2202. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2203. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2204. #elif defined(NO_RSA)
  2205. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2206. #else
  2207. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2208. #endif
  2209. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2210. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2211. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2212. #elif defined(NO_RSA)
  2213. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2214. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2215. !defined(NO_ASN_TIME)
  2216. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2217. #else
  2218. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2219. #endif
  2220. #endif
  2221. return EXPECT_RESULT();
  2222. }
  2223. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2224. {
  2225. EXPECT_DECLS;
  2226. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2227. const char* ca_cert = "./certs/ca-cert.pem";
  2228. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2229. int ret;
  2230. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2231. 1);
  2232. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2233. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2234. #elif defined(NO_RSA)
  2235. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2236. #else
  2237. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2238. #endif
  2239. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2240. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2241. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2242. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2243. #elif defined(NO_RSA)
  2244. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2245. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2246. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2247. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2248. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2249. #else
  2250. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2251. #endif
  2252. #endif
  2253. return EXPECT_RESULT();
  2254. }
  2255. static int test_wolfSSL_CertManagerGetCerts(void)
  2256. {
  2257. EXPECT_DECLS;
  2258. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2259. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2260. defined(WOLFSSL_SIGNER_DER_CERT)
  2261. WOLFSSL_CERT_MANAGER* cm = NULL;
  2262. WOLFSSL_STACK* sk = NULL;
  2263. X509* x509 = NULL;
  2264. X509* cert1 = NULL;
  2265. FILE* file1 = NULL;
  2266. #ifdef DEBUG_WOLFSSL_VERBOSE
  2267. WOLFSSL_BIO* bio = NULL;
  2268. #endif
  2269. int i = 0;
  2270. int ret = 0;
  2271. const byte* der = NULL;
  2272. int derSz = 0;
  2273. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2274. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2275. if (file1 != NULL) {
  2276. fclose(file1);
  2277. }
  2278. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2279. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2280. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2281. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2282. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2283. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2284. * and full OpenSSL compatibility */
  2285. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2286. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2287. #else
  2288. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2289. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2290. #endif
  2291. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2292. "./certs/ca-cert.pem", NULL));
  2293. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2294. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2295. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2296. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2297. #ifdef DEBUG_WOLFSSL_VERBOSE
  2298. bio = BIO_new(wolfSSL_BIO_s_file());
  2299. if (bio != NULL) {
  2300. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2301. X509_print(bio, x509);
  2302. BIO_free(bio);
  2303. }
  2304. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2305. }
  2306. wolfSSL_X509_free(cert1);
  2307. sk_X509_pop_free(sk, NULL);
  2308. wolfSSL_CertManagerFree(cm);
  2309. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2310. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2311. defined(WOLFSSL_SIGNER_DER_CERT) */
  2312. return EXPECT_RESULT();
  2313. }
  2314. static int test_wolfSSL_CertManagerSetVerify(void)
  2315. {
  2316. EXPECT_DECLS;
  2317. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2318. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2319. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2320. WOLFSSL_CERT_MANAGER* cm = NULL;
  2321. int tmp = myVerifyAction;
  2322. const char* ca_cert = "./certs/ca-cert.pem";
  2323. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2324. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2325. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2326. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2327. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2328. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2329. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2330. #else
  2331. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2332. WOLFSSL_SUCCESS);
  2333. #endif
  2334. /* Use the test CB that always accepts certs */
  2335. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2336. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2337. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2338. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2339. {
  2340. const char* verifyCert = "./certs/server-cert.der";
  2341. /* Use the test CB that always fails certs */
  2342. myVerifyAction = VERIFY_FORCE_FAIL;
  2343. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2344. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2345. }
  2346. #endif
  2347. wolfSSL_CertManagerFree(cm);
  2348. myVerifyAction = tmp;
  2349. #endif
  2350. return EXPECT_RESULT();
  2351. }
  2352. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2353. defined(DEBUG_UNIT_TEST_CERTS)
  2354. /* Used when debugging name constraint tests. Not static to allow use in
  2355. * multiple locations with complex define guards. */
  2356. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2357. {
  2358. BIO* out = BIO_new_file(fileName, "wb");
  2359. if (out != NULL) {
  2360. PEM_write_bio_X509(out, x509);
  2361. BIO_free(out);
  2362. }
  2363. }
  2364. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2365. {
  2366. BIO* out = BIO_new_file(fileName, "wb");
  2367. if (out != NULL) {
  2368. BIO_write(out, der, derSz);
  2369. BIO_free(out);
  2370. }
  2371. }
  2372. #else
  2373. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2374. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2375. #endif
  2376. static int test_wolfSSL_CertManagerNameConstraint(void)
  2377. {
  2378. EXPECT_DECLS;
  2379. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2380. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2381. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2382. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2383. !defined(NO_SHA256)
  2384. WOLFSSL_CERT_MANAGER* cm = NULL;
  2385. WOLFSSL_EVP_PKEY *priv = NULL;
  2386. WOLFSSL_X509_NAME* name = NULL;
  2387. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2388. const char* server_cert = "./certs/test/server-goodcn.pem";
  2389. int i = 0;
  2390. static const byte extNameConsOid[] = {85, 29, 30};
  2391. RsaKey key;
  2392. WC_RNG rng;
  2393. byte *der = NULL;
  2394. int derSz = 0;
  2395. word32 idx = 0;
  2396. byte *pt;
  2397. WOLFSSL_X509 *x509 = NULL;
  2398. WOLFSSL_X509 *ca = NULL;
  2399. wc_InitRng(&rng);
  2400. /* load in CA private key for signing */
  2401. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2402. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2403. sizeof_server_key_der_2048), 0);
  2404. /* get ca certificate then alter it */
  2405. ExpectNotNull(der =
  2406. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2407. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2408. WOLFSSL_FILETYPE_ASN1));
  2409. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2410. if (EXPECT_SUCCESS() && (der != NULL)) {
  2411. XMEMCPY(der, pt, derSz);
  2412. /* find the name constraint extension and alter it */
  2413. pt = der;
  2414. for (i = 0; i < derSz - 3; i++) {
  2415. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2416. pt += 3;
  2417. break;
  2418. }
  2419. pt++;
  2420. }
  2421. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2422. /* go to the length value and set it to 0 */
  2423. while (i < derSz && *pt != 0x81) {
  2424. pt++;
  2425. i++;
  2426. }
  2427. ExpectIntNE(i, derSz); /* did not place to alter */
  2428. pt++;
  2429. *pt = 0x00;
  2430. }
  2431. /* resign the altered certificate */
  2432. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2433. FOURK_BUF, &key, NULL, &rng)), 0);
  2434. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2435. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2436. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2437. wolfSSL_CertManagerFree(cm);
  2438. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2439. wolfSSL_X509_free(x509);
  2440. wc_FreeRsaKey(&key);
  2441. wc_FreeRng(&rng);
  2442. /* add email alt name to satisfy constraint */
  2443. pt = (byte*)server_key_der_2048;
  2444. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2445. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2446. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2447. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2448. WOLFSSL_FILETYPE_ASN1));
  2449. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2450. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2451. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2452. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2453. /* Good cert test with proper alt email name */
  2454. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2455. WOLFSSL_FILETYPE_PEM));
  2456. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2457. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2458. name = NULL;
  2459. ExpectNotNull(name = X509_NAME_new());
  2460. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2461. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2462. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2463. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2464. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2465. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2466. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2467. X509_NAME_free(name);
  2468. name = NULL;
  2469. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2470. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2471. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2472. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2473. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2474. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2475. wolfSSL_X509_free(x509);
  2476. x509 = NULL;
  2477. /* Cert with bad alt name list */
  2478. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2479. WOLFSSL_FILETYPE_PEM));
  2480. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2481. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2482. name = NULL;
  2483. ExpectNotNull(name = X509_NAME_new());
  2484. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2485. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2486. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2487. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2488. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2489. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2490. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2491. X509_NAME_free(name);
  2492. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2493. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2494. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2495. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2496. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2497. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2498. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2499. wolfSSL_CertManagerFree(cm);
  2500. wolfSSL_X509_free(x509);
  2501. wolfSSL_X509_free(ca);
  2502. wolfSSL_EVP_PKEY_free(priv);
  2503. #endif
  2504. return EXPECT_RESULT();
  2505. }
  2506. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2507. {
  2508. EXPECT_DECLS;
  2509. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2510. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2511. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2512. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2513. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2514. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2515. const char* server_cert = "./certs/server-cert.pem";
  2516. WOLFSSL_CERT_MANAGER* cm = NULL;
  2517. WOLFSSL_X509 *x509 = NULL;
  2518. WOLFSSL_X509 *ca = NULL;
  2519. const unsigned char *der = NULL;
  2520. const unsigned char *pt;
  2521. WOLFSSL_EVP_PKEY *priv = NULL;
  2522. WOLFSSL_X509_NAME* name = NULL;
  2523. int derSz = 0;
  2524. /* C=US*/
  2525. char altName[] = {
  2526. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2527. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2528. };
  2529. /* C=ID */
  2530. char altNameFail[] = {
  2531. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2532. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2533. };
  2534. /* C=US ST=California*/
  2535. char altNameExc[] = {
  2536. 0x30, 0x22,
  2537. 0x31, 0x0B,
  2538. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2539. 0x31, 0x13,
  2540. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2541. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2542. };
  2543. /* load in CA private key for signing */
  2544. pt = ca_key_der_2048;
  2545. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2546. sizeof_ca_key_der_2048));
  2547. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2548. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2549. WOLFSSL_FILETYPE_ASN1));
  2550. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2551. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2552. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2553. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2554. WOLFSSL_FILETYPE_PEM));
  2555. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2556. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2557. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2558. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2559. #else
  2560. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2561. #endif
  2562. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2563. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2564. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2565. /* add in matching DIR alt name and resign */
  2566. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2567. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2568. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2569. #else
  2570. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2571. #endif
  2572. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2573. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2574. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2575. wolfSSL_X509_free(x509);
  2576. x509 = NULL;
  2577. /* check verify fail */
  2578. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2579. WOLFSSL_FILETYPE_PEM));
  2580. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2581. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2582. /* add in miss matching DIR alt name and resign */
  2583. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2584. ASN_DIR_TYPE);
  2585. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2586. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2587. #else
  2588. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2589. #endif
  2590. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2591. #ifndef WOLFSSL_NO_ASN_STRICT
  2592. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2593. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2594. #else
  2595. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2596. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2597. #endif
  2598. /* check that it still fails if one bad altname and one good altname is in
  2599. * the certificate */
  2600. wolfSSL_X509_free(x509);
  2601. x509 = NULL;
  2602. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2603. WOLFSSL_FILETYPE_PEM));
  2604. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2605. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2606. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2607. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2608. ASN_DIR_TYPE);
  2609. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2610. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2611. #else
  2612. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2613. #endif
  2614. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2615. #ifndef WOLFSSL_NO_ASN_STRICT
  2616. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2617. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2618. #else
  2619. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2620. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2621. #endif
  2622. /* check it fails with switching position of bad altname */
  2623. wolfSSL_X509_free(x509);
  2624. x509 = NULL;
  2625. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2626. WOLFSSL_FILETYPE_PEM));
  2627. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2628. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2629. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2630. ASN_DIR_TYPE);
  2631. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2632. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2633. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2634. #else
  2635. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2636. #endif
  2637. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2638. #ifndef WOLFSSL_NO_ASN_STRICT
  2639. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2640. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2641. #else
  2642. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2643. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2644. #endif
  2645. wolfSSL_CertManagerFree(cm);
  2646. wolfSSL_X509_free(x509);
  2647. x509 = NULL;
  2648. wolfSSL_X509_free(ca);
  2649. ca = NULL;
  2650. /* now test with excluded name constraint */
  2651. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2652. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2653. WOLFSSL_FILETYPE_ASN1));
  2654. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2655. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2656. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2657. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2658. WOLFSSL_FILETYPE_PEM));
  2659. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2660. ASN_DIR_TYPE);
  2661. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2662. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2663. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2664. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2665. #else
  2666. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2667. #endif
  2668. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2669. #ifndef WOLFSSL_NO_ASN_STRICT
  2670. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2671. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2672. #else
  2673. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2674. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2675. #endif
  2676. wolfSSL_CertManagerFree(cm);
  2677. wolfSSL_X509_free(x509);
  2678. wolfSSL_X509_free(ca);
  2679. wolfSSL_EVP_PKEY_free(priv);
  2680. #endif
  2681. return EXPECT_RESULT();
  2682. }
  2683. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2684. {
  2685. EXPECT_DECLS;
  2686. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2687. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2688. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2689. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2690. !defined(NO_SHA256)
  2691. WOLFSSL_CERT_MANAGER* cm = NULL;
  2692. WOLFSSL_EVP_PKEY *priv = NULL;
  2693. WOLFSSL_X509_NAME* name = NULL;
  2694. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2695. const char* server_cert = "./certs/test/server-goodcn.pem";
  2696. byte *der = NULL;
  2697. int derSz = 0;
  2698. byte *pt;
  2699. WOLFSSL_X509 *x509 = NULL;
  2700. WOLFSSL_X509 *ca = NULL;
  2701. pt = (byte*)server_key_der_2048;
  2702. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2703. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2704. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2705. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2706. WOLFSSL_FILETYPE_ASN1));
  2707. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2708. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2709. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2710. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2711. /* check satisfying .wolfssl.com constraint passes */
  2712. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2713. WOLFSSL_FILETYPE_PEM));
  2714. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2715. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2716. name = NULL;
  2717. ExpectNotNull(name = X509_NAME_new());
  2718. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2719. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2720. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2721. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2722. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2723. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2724. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2725. X509_NAME_free(name);
  2726. name = NULL;
  2727. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2728. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2729. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2730. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2731. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2732. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2733. wolfSSL_X509_free(x509);
  2734. x509 = NULL;
  2735. /* check satisfying .random.com constraint passes */
  2736. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2737. WOLFSSL_FILETYPE_PEM));
  2738. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2739. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2740. name = NULL;
  2741. ExpectNotNull(name = X509_NAME_new());
  2742. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2743. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2744. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2745. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2746. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2747. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2748. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2749. X509_NAME_free(name);
  2750. name = NULL;
  2751. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2752. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2753. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2754. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2755. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2756. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2757. wolfSSL_X509_free(x509);
  2758. x509 = NULL;
  2759. /* check fail case when neither constraint is matched */
  2760. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2761. WOLFSSL_FILETYPE_PEM));
  2762. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2763. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2764. name = NULL;
  2765. ExpectNotNull(name = X509_NAME_new());
  2766. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2767. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2768. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2769. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2770. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2771. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2772. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2773. X509_NAME_free(name);
  2774. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2775. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2776. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2777. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2778. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2779. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2780. wolfSSL_CertManagerFree(cm);
  2781. wolfSSL_X509_free(x509);
  2782. wolfSSL_X509_free(ca);
  2783. wolfSSL_EVP_PKEY_free(priv);
  2784. #endif
  2785. return EXPECT_RESULT();
  2786. }
  2787. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2788. {
  2789. EXPECT_DECLS;
  2790. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2791. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2792. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2793. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2794. !defined(NO_SHA256)
  2795. WOLFSSL_CERT_MANAGER* cm = NULL;
  2796. WOLFSSL_EVP_PKEY *priv = NULL;
  2797. WOLFSSL_X509_NAME* name = NULL;
  2798. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2799. const char* server_cert = "./certs/test/server-goodcn.pem";
  2800. byte *der = NULL;
  2801. int derSz;
  2802. byte *pt;
  2803. WOLFSSL_X509 *x509 = NULL;
  2804. WOLFSSL_X509 *ca = NULL;
  2805. pt = (byte*)server_key_der_2048;
  2806. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2807. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2808. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2809. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2810. WOLFSSL_FILETYPE_ASN1));
  2811. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2812. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2813. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2814. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2815. /* check satisfying wolfssl.com constraint passes */
  2816. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2817. WOLFSSL_FILETYPE_PEM));
  2818. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2819. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2820. name = NULL;
  2821. ExpectNotNull(name = X509_NAME_new());
  2822. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2823. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2824. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2825. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2826. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2827. X509_NAME_free(name);
  2828. name = NULL;
  2829. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2830. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2831. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2832. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2833. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2834. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2835. wolfSSL_X509_free(x509);
  2836. x509 = NULL;
  2837. /* check satisfying example.com constraint passes */
  2838. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2839. WOLFSSL_FILETYPE_PEM));
  2840. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2841. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2842. name = NULL;
  2843. ExpectNotNull(name = X509_NAME_new());
  2844. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2845. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2846. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2847. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2848. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2849. X509_NAME_free(name);
  2850. name = NULL;
  2851. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2852. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2853. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2854. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2855. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2856. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2857. wolfSSL_X509_free(x509);
  2858. x509 = NULL;
  2859. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2860. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2861. WOLFSSL_FILETYPE_PEM));
  2862. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2863. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2864. name = NULL;
  2865. ExpectNotNull(name = X509_NAME_new());
  2866. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2867. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2868. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2869. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2870. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2871. X509_NAME_free(name);
  2872. name = NULL;
  2873. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2874. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2875. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2876. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2877. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2878. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2879. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2880. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2881. wolfSSL_X509_free(x509);
  2882. x509 = NULL;
  2883. /* check fail when one DNS in the list is bad */
  2884. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2885. WOLFSSL_FILETYPE_PEM));
  2886. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2887. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2888. name = NULL;
  2889. ExpectNotNull(name = X509_NAME_new());
  2890. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2891. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2892. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2893. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2894. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2895. X509_NAME_free(name);
  2896. name = NULL;
  2897. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2898. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2899. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2900. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2901. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2902. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2903. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2904. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2905. wolfSSL_X509_free(x509);
  2906. x509 = NULL;
  2907. /* check fail case when neither constraint is matched */
  2908. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2909. WOLFSSL_FILETYPE_PEM));
  2910. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2911. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2912. name = NULL;
  2913. ExpectNotNull(name = X509_NAME_new());
  2914. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2915. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2916. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2917. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2918. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2919. X509_NAME_free(name);
  2920. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2921. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2922. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2923. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2924. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2925. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2926. wolfSSL_CertManagerFree(cm);
  2927. wolfSSL_X509_free(x509);
  2928. wolfSSL_X509_free(ca);
  2929. wolfSSL_EVP_PKEY_free(priv);
  2930. #endif
  2931. return EXPECT_RESULT();
  2932. }
  2933. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2934. {
  2935. EXPECT_DECLS;
  2936. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2937. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2938. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2939. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2940. !defined(NO_SHA256)
  2941. WOLFSSL_CERT_MANAGER* cm = NULL;
  2942. WOLFSSL_EVP_PKEY *priv = NULL;
  2943. WOLFSSL_X509_NAME* name = NULL;
  2944. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2945. const char* server_cert = "./certs/test/server-goodcn.pem";
  2946. byte *der = NULL;
  2947. int derSz;
  2948. byte *pt;
  2949. WOLFSSL_X509 *x509 = NULL;
  2950. WOLFSSL_X509 *ca = NULL;
  2951. pt = (byte*)server_key_der_2048;
  2952. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2953. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2954. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2955. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2956. WOLFSSL_FILETYPE_ASN1));
  2957. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2958. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2959. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2960. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2961. /* check satisfying wolfssl.com constraint passes */
  2962. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2963. WOLFSSL_FILETYPE_PEM));
  2964. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2965. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2966. name = NULL;
  2967. ExpectNotNull(name = X509_NAME_new());
  2968. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2969. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2970. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2971. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2972. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2973. X509_NAME_free(name);
  2974. name = NULL;
  2975. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2976. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2977. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2978. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2979. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2980. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2981. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2982. wolfSSL_X509_free(x509);
  2983. x509 = NULL;
  2984. /* fail with DNS check because of common name */
  2985. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2986. WOLFSSL_FILETYPE_PEM));
  2987. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2988. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2989. name = NULL;
  2990. ExpectNotNull(name = X509_NAME_new());
  2991. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2992. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2993. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2994. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2995. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2996. X509_NAME_free(name);
  2997. name = NULL;
  2998. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2999. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  3000. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3001. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  3002. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3003. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3004. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3005. wolfSSL_X509_free(x509);
  3006. x509 = NULL;
  3007. /* fail on permitted DNS name constraint */
  3008. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3009. WOLFSSL_FILETYPE_PEM));
  3010. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3011. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3012. name = NULL;
  3013. ExpectNotNull(name = X509_NAME_new());
  3014. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3015. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3016. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3017. X509_NAME_free(name);
  3018. name = NULL;
  3019. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3020. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3021. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3022. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3023. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3024. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3025. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3026. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3027. wolfSSL_X509_free(x509);
  3028. x509 = NULL;
  3029. /* fail on permitted email name constraint */
  3030. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3031. WOLFSSL_FILETYPE_PEM));
  3032. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3033. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3034. name = NULL;
  3035. ExpectNotNull(name = X509_NAME_new());
  3036. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3037. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3038. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3039. X509_NAME_free(name);
  3040. name = NULL;
  3041. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3042. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3043. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3044. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3045. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3046. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3047. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3048. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3049. wolfSSL_X509_free(x509);
  3050. x509 = NULL;
  3051. /* success with empty email name */
  3052. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3053. WOLFSSL_FILETYPE_PEM));
  3054. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3055. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3056. name = NULL;
  3057. ExpectNotNull(name = X509_NAME_new());
  3058. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3059. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3060. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3061. X509_NAME_free(name);
  3062. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3063. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3064. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3065. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3066. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3067. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3068. wolfSSL_X509_free(x509);
  3069. wolfSSL_CertManagerFree(cm);
  3070. wolfSSL_X509_free(ca);
  3071. wolfSSL_EVP_PKEY_free(priv);
  3072. #endif
  3073. return EXPECT_RESULT();
  3074. }
  3075. static int test_wolfSSL_CertManagerCRL(void)
  3076. {
  3077. EXPECT_DECLS;
  3078. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3079. !defined(NO_RSA)
  3080. const char* ca_cert = "./certs/ca-cert.pem";
  3081. const char* crl1 = "./certs/crl/crl.pem";
  3082. const char* crl2 = "./certs/crl/crl2.pem";
  3083. #ifdef WC_RSA_PSS
  3084. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3085. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3086. #endif
  3087. const unsigned char crl_buff[] = {
  3088. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3089. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3090. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3091. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3092. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3093. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3094. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3095. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3096. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3097. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3098. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3099. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3100. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3101. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3102. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3103. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3104. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3105. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3106. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3107. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3108. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3109. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3110. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3111. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3112. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3113. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3114. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3115. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3116. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3117. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3118. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3119. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3120. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3121. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3122. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3123. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3124. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3125. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3126. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3127. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3128. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3129. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3130. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3131. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3132. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3133. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3134. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3135. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3136. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3137. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3138. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3139. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3140. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3141. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3142. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3143. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3144. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3145. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3146. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3147. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3148. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3149. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3150. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3151. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3152. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3153. };
  3154. WOLFSSL_CERT_MANAGER* cm = NULL;
  3155. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3156. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3157. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3158. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3159. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3160. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3161. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3162. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3163. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3164. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3165. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3166. BAD_FUNC_ARG);
  3167. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3168. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3169. BAD_FUNC_ARG);
  3170. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3171. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3172. BAD_FUNC_ARG);
  3173. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3174. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3175. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3176. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3177. #ifdef HAVE_CRL_IO
  3178. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3179. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3180. #endif
  3181. #ifndef NO_FILESYSTEM
  3182. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3183. 0), BAD_FUNC_ARG);
  3184. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3185. 0), BAD_FUNC_ARG);
  3186. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3187. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3188. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3189. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3190. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3191. BAD_FUNC_ARG);
  3192. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3193. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3194. ASN_PARSE_E);
  3195. #endif
  3196. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3197. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3198. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3199. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3200. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3201. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3202. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3203. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3204. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3205. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3206. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3207. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3208. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3209. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3210. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3211. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3212. ExpectIntEQ(WOLFSSL_SUCCESS,
  3213. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3214. ExpectIntEQ(WOLFSSL_SUCCESS,
  3215. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3216. ExpectIntEQ(WOLFSSL_SUCCESS,
  3217. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3218. wolfSSL_CertManagerFreeCRL(cm);
  3219. ExpectIntEQ(WOLFSSL_SUCCESS,
  3220. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3221. ExpectIntEQ(WOLFSSL_SUCCESS,
  3222. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3223. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3224. sizeof_server_cert_der_2048), CRL_MISSING);
  3225. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3226. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3227. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3228. WOLFSSL_FILETYPE_ASN1), 1);
  3229. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3230. /* loading should fail without the CA set */
  3231. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3232. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3233. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3234. ExpectIntEQ(WOLFSSL_SUCCESS,
  3235. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3236. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3237. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3238. #endif
  3239. wolfSSL_CertManagerFree(cm);
  3240. #endif
  3241. return EXPECT_RESULT();
  3242. }
  3243. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3244. {
  3245. EXPECT_DECLS;
  3246. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3247. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3248. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3249. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3250. defined(HAVE_LIGHTY)
  3251. WOLFSSL_CERT_MANAGER* cm = NULL;
  3252. /* Raw OCSP response bytes captured using the following setup:
  3253. * - Run responder with
  3254. * openssl ocsp -port 9999 -ndays 9999
  3255. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3256. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3257. * -rkey certs/ocsp/ocsp-responder-key.pem
  3258. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3259. * - Run client with
  3260. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3261. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3262. * -cert certs/ocsp/server1-cert.pem
  3263. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3264. * - Select the response packet in Wireshark, and export it using
  3265. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3266. * packets only". After importing into the editor, remove the initial
  3267. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3268. */
  3269. static const byte response[] = {
  3270. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3271. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3272. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3273. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3274. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3275. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3276. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3277. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3278. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3279. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3280. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3281. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3282. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3283. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3284. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3285. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3286. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3287. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3288. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3289. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3290. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3291. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3292. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3293. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3294. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3295. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3296. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3297. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3298. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3299. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3300. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3301. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3302. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3303. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3304. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3305. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3306. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3307. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3308. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3309. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3310. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3311. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3312. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3313. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3314. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3315. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3316. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3317. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3318. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3319. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3320. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3321. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3322. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3323. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3324. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3325. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3326. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3327. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3328. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3329. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3330. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3331. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3332. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3333. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3334. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3335. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3336. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3337. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3338. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3339. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3340. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3341. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3342. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3343. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3344. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3345. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3346. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3347. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3348. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3349. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3350. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3351. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3352. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3353. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3354. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3355. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3356. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3357. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3358. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3359. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3360. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3361. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3362. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3363. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3364. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3365. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3366. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3367. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3368. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3369. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3370. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3371. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3372. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3373. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3374. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3375. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3376. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3377. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3378. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3379. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3380. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3381. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3382. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3383. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3384. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3385. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3386. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3387. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3388. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3389. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3390. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3391. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3392. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3393. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3394. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3395. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3396. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3397. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3398. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3399. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3400. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3401. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3402. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3403. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3404. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3405. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3406. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3407. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3408. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3409. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3410. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3411. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3412. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3413. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3414. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3415. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3416. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3417. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3418. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3419. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3420. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3421. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3422. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3423. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3424. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3425. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3426. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3427. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3428. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3429. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3430. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3431. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3432. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3433. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3434. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3435. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3436. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3437. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3438. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3439. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3440. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3441. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3442. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3443. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3444. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3445. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3446. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3447. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3448. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3449. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3450. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3451. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3452. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3453. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3454. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3455. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3456. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3457. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3458. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3459. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3460. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3461. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3462. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3463. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3464. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3465. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3466. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3467. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3468. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3469. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3470. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3471. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3472. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3473. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3474. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3475. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3476. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3477. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3478. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3479. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3480. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3481. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3482. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3483. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3484. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3485. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3486. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3487. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3488. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3489. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3490. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3491. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3492. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3493. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3494. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3495. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3496. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3497. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3498. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3499. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3500. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3501. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3502. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3503. };
  3504. OcspEntry entry[1];
  3505. CertStatus status[1];
  3506. OcspRequest* request = NULL;
  3507. #ifndef NO_FILESYSTEM
  3508. const char* ca_cert = "./certs/ca-cert.pem";
  3509. #endif
  3510. byte serial[] = {0x05};
  3511. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3512. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3513. XMEMSET(entry, 0, sizeof(OcspEntry));
  3514. XMEMSET(status, 0, sizeof(CertStatus));
  3515. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3516. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3517. DYNAMIC_TYPE_OCSP_REQUEST));
  3518. if ((request != NULL) && (request->serial != NULL)) {
  3519. request->serialSz = sizeof(serial);
  3520. XMEMCPY(request->serial, serial, sizeof(serial));
  3521. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3522. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3523. }
  3524. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3525. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3526. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3527. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3528. /* Response should be valid. */
  3529. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3530. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3531. /* Flip a byte in the request serial number, response should be invalid
  3532. * now. */
  3533. if ((request != NULL) && (request->serial != NULL))
  3534. request->serial[0] ^= request->serial[0];
  3535. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3536. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3537. #ifndef NO_FILESYSTEM
  3538. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3539. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3540. ExpectIntEQ(WOLFSSL_SUCCESS,
  3541. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3542. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3543. sizeof(server_cert_der_2048)), 1);
  3544. #endif
  3545. wolfSSL_OCSP_REQUEST_free(request);
  3546. wolfSSL_CertManagerFree(cm);
  3547. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3548. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3549. #endif /* HAVE_OCSP */
  3550. return EXPECT_RESULT();
  3551. }
  3552. static int test_wolfSSL_CheckOCSPResponse(void)
  3553. {
  3554. EXPECT_DECLS;
  3555. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3556. defined(OPENSSL_ALL)
  3557. const char* responseFile = "./certs/ocsp/test-response.der";
  3558. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3559. const char* responseNoInternFile =
  3560. "./certs/ocsp/test-response-nointern.der";
  3561. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3562. OcspResponse* res = NULL;
  3563. byte data[4096];
  3564. const unsigned char* pt;
  3565. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3566. * gcc sanitizer with --enable-heapmath.
  3567. */
  3568. XFILE f = XBADFILE;
  3569. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3570. WOLFSSL_X509_STORE* st = NULL;
  3571. WOLFSSL_X509* issuer = NULL;
  3572. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3573. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3574. if (f != XBADFILE) {
  3575. XFCLOSE(f);
  3576. f = XBADFILE;
  3577. }
  3578. pt = data;
  3579. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3580. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3581. SSL_FILETYPE_PEM));
  3582. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3583. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3584. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3585. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3586. wolfSSL_OCSP_BASICRESP_free(bs);
  3587. bs = NULL;
  3588. wolfSSL_OCSP_RESPONSE_free(res);
  3589. res = NULL;
  3590. wolfSSL_X509_STORE_free(st);
  3591. st = NULL;
  3592. wolfSSL_X509_free(issuer);
  3593. issuer = NULL;
  3594. /* check loading a response with optional certs */
  3595. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3596. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3597. if (f != XBADFILE)
  3598. XFCLOSE(f);
  3599. f = XBADFILE;
  3600. pt = data;
  3601. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3602. wolfSSL_OCSP_RESPONSE_free(res);
  3603. res = NULL;
  3604. /* check loading a response with multiple certs */
  3605. {
  3606. WOLFSSL_CERT_MANAGER* cm = NULL;
  3607. OcspEntry *entry = NULL;
  3608. CertStatus* status = NULL;
  3609. OcspRequest* request = NULL;
  3610. byte serial1[] = {0x01};
  3611. byte serial[] = {0x02};
  3612. byte issuerHash[] = {
  3613. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3614. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3615. 0x37, 0x15, 0x8A, 0x88
  3616. };
  3617. byte issuerKeyHash[] = {
  3618. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3619. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3620. 0x7E, 0x72, 0x15, 0x21
  3621. };
  3622. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3623. DYNAMIC_TYPE_OPENSSL));
  3624. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3625. DYNAMIC_TYPE_OPENSSL));
  3626. if (entry != NULL)
  3627. XMEMSET(entry, 0, sizeof(OcspEntry));
  3628. if (status != NULL)
  3629. XMEMSET(status, 0, sizeof(CertStatus));
  3630. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3631. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3632. DYNAMIC_TYPE_OCSP_REQUEST));
  3633. if (request != NULL && request->serial != NULL) {
  3634. request->serialSz = sizeof(serial);
  3635. XMEMCPY(request->serial, serial, sizeof(serial));
  3636. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3637. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3638. sizeof(issuerKeyHash));
  3639. }
  3640. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3641. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3642. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3643. WOLFSSL_SUCCESS);
  3644. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3645. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3646. if (f != XBADFILE)
  3647. XFCLOSE(f);
  3648. f = XBADFILE;
  3649. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3650. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3651. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3652. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3653. ExpectNotNull(entry->status);
  3654. if (request != NULL && request->serial != NULL)
  3655. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3656. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3657. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3658. /* store both status's in the entry to check that "next" is not
  3659. * overwritten */
  3660. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3661. status->next = entry->status;
  3662. entry->status = status;
  3663. }
  3664. if (request != NULL && request->serial != NULL)
  3665. XMEMCPY(request->serial, serial, sizeof(serial));
  3666. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3667. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3668. ExpectNotNull(entry->status->next);
  3669. /* compare the status found */
  3670. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3671. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3672. status->serialSz), 0);
  3673. if (status != NULL && entry != NULL && entry->status != status) {
  3674. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3675. }
  3676. wolfSSL_OCSP_CERTID_free(entry);
  3677. wolfSSL_OCSP_REQUEST_free(request);
  3678. wolfSSL_CertManagerFree(cm);
  3679. }
  3680. #if defined(WC_RSA_PSS)
  3681. {
  3682. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3683. /* check loading a response with RSA-PSS signature */
  3684. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3685. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3686. if (f != XBADFILE)
  3687. XFCLOSE(f);
  3688. pt = data;
  3689. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3690. /* try to verify the response */
  3691. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3692. SSL_FILETYPE_PEM));
  3693. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3694. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3695. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3696. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3697. WOLFSSL_SUCCESS);
  3698. wolfSSL_OCSP_BASICRESP_free(bs);
  3699. wolfSSL_OCSP_RESPONSE_free(res);
  3700. wolfSSL_X509_STORE_free(st);
  3701. wolfSSL_X509_free(issuer);
  3702. }
  3703. #endif
  3704. #endif /* HAVE_OCSP */
  3705. return EXPECT_RESULT();
  3706. }
  3707. static int test_wolfSSL_FPKI(void)
  3708. {
  3709. EXPECT_DECLS;
  3710. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3711. XFILE f = XBADFILE;
  3712. const char* fpkiCert = "./certs/fpki-cert.der";
  3713. DecodedCert cert;
  3714. byte buf[4096];
  3715. byte* uuid = NULL;
  3716. byte* fascn = NULL;
  3717. word32 fascnSz;
  3718. word32 uuidSz;
  3719. int bytes = 0;
  3720. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3721. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3722. if (f != XBADFILE)
  3723. XFCLOSE(f);
  3724. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3725. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3726. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3727. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3728. DYNAMIC_TYPE_TMP_BUFFER));
  3729. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3730. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3731. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3732. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3733. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3734. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3735. wc_FreeDecodedCert(&cert);
  3736. #endif
  3737. return EXPECT_RESULT();
  3738. }
  3739. /* use RID in confuncture with other names to test parsing of unknown other
  3740. * names */
  3741. static int test_wolfSSL_OtherName(void)
  3742. {
  3743. EXPECT_DECLS;
  3744. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3745. XFILE f = XBADFILE;
  3746. const char* ridCert = "./certs/rid-cert.der";
  3747. DecodedCert cert;
  3748. byte buf[4096];
  3749. int bytes = 0;
  3750. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3751. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3752. if (f != XBADFILE)
  3753. XFCLOSE(f);
  3754. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3755. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3756. wc_FreeDecodedCert(&cert);
  3757. #endif
  3758. return EXPECT_RESULT();
  3759. }
  3760. static int test_wolfSSL_CertRsaPss(void)
  3761. {
  3762. EXPECT_DECLS;
  3763. /* FIPS v2 and below don't support long salts. */
  3764. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3765. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3766. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3767. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3768. XFILE f = XBADFILE;
  3769. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3770. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3771. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3772. RSA_MAX_SIZE >= 3072
  3773. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3774. #endif
  3775. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3776. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3777. #endif
  3778. DecodedCert cert;
  3779. byte buf[4096];
  3780. int bytes = 0;
  3781. WOLFSSL_CERT_MANAGER* cm = NULL;
  3782. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3783. ExpectIntEQ(WOLFSSL_SUCCESS,
  3784. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3785. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3786. ExpectIntEQ(WOLFSSL_SUCCESS,
  3787. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3788. #endif
  3789. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3790. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3791. if (f != XBADFILE) {
  3792. XFCLOSE(f);
  3793. f = XBADFILE;
  3794. }
  3795. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3796. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3797. wc_FreeDecodedCert(&cert);
  3798. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3799. RSA_MAX_SIZE >= 3072
  3800. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3801. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3802. if (f != XBADFILE)
  3803. XFCLOSE(f);
  3804. wc_InitDecodedCert(&cert, buf, (word32)bytes, NULL);
  3805. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3806. wc_FreeDecodedCert(&cert);
  3807. #endif
  3808. wolfSSL_CertManagerFree(cm);
  3809. #endif
  3810. return EXPECT_RESULT();
  3811. }
  3812. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3813. {
  3814. EXPECT_DECLS;
  3815. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3816. !defined(NO_WOLFSSL_CLIENT)
  3817. WOLFSSL_CTX* ctx = NULL;
  3818. const char* ca_cert = "./certs/ca-cert.pem";
  3819. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3820. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3821. /* test good CA */
  3822. ExpectTrue(WOLFSSL_SUCCESS ==
  3823. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3824. WOLFSSL_LOAD_FLAG_NONE));
  3825. /* test expired CA */
  3826. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3827. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3828. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3829. #else
  3830. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3831. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3832. #endif
  3833. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3834. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3835. wolfSSL_CTX_free(ctx);
  3836. #endif
  3837. return EXPECT_RESULT();
  3838. }
  3839. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3840. {
  3841. EXPECT_DECLS;
  3842. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3843. defined(USE_CERT_BUFFERS_2048)
  3844. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3845. WOLFSSL_CTX* ctx;
  3846. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3847. byte ca_expired_cert[TWOK_BUF];
  3848. word32 sizeof_ca_expired_cert = 0;
  3849. XFILE fp = XBADFILE;
  3850. #ifndef NO_WOLFSSL_CLIENT
  3851. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3852. #else
  3853. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3854. #endif
  3855. ExpectNotNull(ctx);
  3856. /* test good CA */
  3857. ExpectTrue(WOLFSSL_SUCCESS ==
  3858. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3859. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3860. WOLFSSL_LOAD_FLAG_NONE));
  3861. /* load expired CA */
  3862. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3863. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3864. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3865. sizeof(ca_expired_cert), fp), 0);
  3866. if (fp != XBADFILE)
  3867. XFCLOSE(fp);
  3868. /* test expired CA failure */
  3869. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3870. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3871. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3872. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3873. #else
  3874. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3875. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3876. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3877. #endif
  3878. /* test expired CA success */
  3879. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3880. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3881. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3882. wolfSSL_CTX_free(ctx);
  3883. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3884. #endif
  3885. return EXPECT_RESULT();
  3886. }
  3887. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3888. {
  3889. EXPECT_DECLS;
  3890. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3891. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3892. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3893. WOLFSSL_CTX* ctx = NULL;
  3894. #ifndef NO_WOLFSSL_CLIENT
  3895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3896. #else
  3897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3898. #endif
  3899. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3900. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3901. WOLFSSL_FILETYPE_ASN1));
  3902. wolfSSL_CTX_free(ctx);
  3903. #endif
  3904. return EXPECT_RESULT();
  3905. }
  3906. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3907. {
  3908. EXPECT_DECLS;
  3909. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3910. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3911. WOLFSSL_CTX* ctx;
  3912. WOLFSSL* ssl = NULL;
  3913. const char *certChain[] = {
  3914. "./certs/intermediate/client-int-cert.pem",
  3915. "./certs/intermediate/ca-int2-cert.pem",
  3916. "./certs/intermediate/ca-int-cert.pem",
  3917. "./certs/ca-cert.pem",
  3918. NULL
  3919. };
  3920. const char** cert;
  3921. WOLFSSL_X509* x509 = NULL;
  3922. WOLF_STACK_OF(X509)* chain = NULL;
  3923. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3924. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3925. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3926. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3927. WOLFSSL_FILETYPE_PEM));
  3928. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3929. X509_free(x509);
  3930. x509 = NULL;
  3931. }
  3932. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3933. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3934. WOLFSSL_FILETYPE_PEM));
  3935. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3936. X509_free(x509);
  3937. x509 = NULL;
  3938. }
  3939. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3940. ExpectIntEQ(sk_X509_num(chain), 3);
  3941. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3942. ExpectIntEQ(sk_X509_num(chain), 3);
  3943. SSL_free(ssl);
  3944. SSL_CTX_free(ctx);
  3945. #endif
  3946. return EXPECT_RESULT();
  3947. }
  3948. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3949. {
  3950. EXPECT_DECLS;
  3951. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3952. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3953. const char* server_chain_der = "./certs/server-cert-chain.der";
  3954. const char* client_single_pem = "./certs/client-cert.pem";
  3955. WOLFSSL_CTX* ctx;
  3956. (void)server_chain_der;
  3957. (void)client_single_pem;
  3958. (void)ctx;
  3959. #ifndef NO_WOLFSSL_CLIENT
  3960. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3961. #else
  3962. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3963. #endif
  3964. ExpectNotNull(ctx);
  3965. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3966. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3967. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3968. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3969. wolfSSL_CTX_free(ctx);
  3970. #endif
  3971. return EXPECT_RESULT();
  3972. }
  3973. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3974. {
  3975. EXPECT_DECLS;
  3976. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3977. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3978. WOLFSSL_CTX *ctx = NULL;
  3979. (void)ctx;
  3980. #ifndef NO_WOLFSSL_CLIENT
  3981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3982. #else
  3983. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3984. #endif
  3985. /* invalid context */
  3986. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3987. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3988. /* invalid dhParamFile file */
  3989. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3990. NULL, WOLFSSL_FILETYPE_PEM));
  3991. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3992. bogusFile, WOLFSSL_FILETYPE_PEM));
  3993. /* success */
  3994. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3995. WOLFSSL_FILETYPE_PEM));
  3996. wolfSSL_CTX_free(ctx);
  3997. #endif
  3998. return EXPECT_RESULT();
  3999. }
  4000. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  4001. {
  4002. EXPECT_DECLS;
  4003. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4004. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4005. WOLFSSL_CTX *ctx = NULL;
  4006. #ifndef NO_WOLFSSL_CLIENT
  4007. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4008. #else
  4009. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4010. #endif
  4011. /* invalid context */
  4012. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4013. dh_key_der_2048, sizeof_dh_key_der_2048,
  4014. WOLFSSL_FILETYPE_ASN1));
  4015. /* invalid dhParamFile file */
  4016. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4017. 0, WOLFSSL_FILETYPE_ASN1));
  4018. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4019. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4020. WOLFSSL_FILETYPE_ASN1));
  4021. /* success */
  4022. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4023. dh_key_der_2048, sizeof_dh_key_der_2048,
  4024. WOLFSSL_FILETYPE_ASN1));
  4025. wolfSSL_CTX_free(ctx);
  4026. #endif
  4027. return EXPECT_RESULT();
  4028. }
  4029. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4030. {
  4031. EXPECT_DECLS;
  4032. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4033. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4034. WOLFSSL_CTX *ctx;
  4035. (void)ctx;
  4036. #ifndef NO_WOLFSSL_CLIENT
  4037. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4038. #else
  4039. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4040. #endif
  4041. ExpectNotNull(ctx);
  4042. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4043. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4044. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4045. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4046. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4047. dh_key_der_2048, sizeof_dh_key_der_2048,
  4048. WOLFSSL_FILETYPE_ASN1));
  4049. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4050. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4051. dh_key_der_2048, sizeof_dh_key_der_2048,
  4052. WOLFSSL_FILETYPE_ASN1));
  4053. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4054. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4055. dh_key_der_2048, sizeof_dh_key_der_2048,
  4056. WOLFSSL_FILETYPE_ASN1));
  4057. wolfSSL_CTX_free(ctx);
  4058. #endif
  4059. return EXPECT_RESULT();
  4060. }
  4061. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4062. {
  4063. EXPECT_DECLS;
  4064. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4065. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4066. WOLFSSL_CTX* ctx = NULL;
  4067. const char* derCert = "./certs/server-cert.der";
  4068. const char* nullPath = NULL;
  4069. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4070. const char* emptyPath = "";
  4071. /* der load Case 1 ctx NULL */
  4072. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4073. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4074. #ifndef NO_WOLFSSL_CLIENT
  4075. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4076. #else
  4077. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4078. #endif
  4079. /* Case 2 filePath NULL */
  4080. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4081. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4082. /* Case 3 invalid format */
  4083. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4084. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4085. /* Case 4 filePath not valid */
  4086. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4087. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4088. /* Case 5 filePath empty */
  4089. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4090. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4091. #ifndef NO_RSA
  4092. /* Case 6 success case */
  4093. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4094. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4095. #endif
  4096. wolfSSL_CTX_free(ctx);
  4097. #endif
  4098. return EXPECT_RESULT();
  4099. }
  4100. static int test_wolfSSL_CTX_enable_disable(void)
  4101. {
  4102. EXPECT_DECLS;
  4103. #ifndef NO_CERTS
  4104. WOLFSSL_CTX* ctx = NULL;
  4105. #ifdef HAVE_CRL
  4106. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4107. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4108. #endif
  4109. #ifdef HAVE_OCSP
  4110. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4111. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4112. #endif
  4113. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4114. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4115. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4116. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4117. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4118. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4119. #endif
  4120. #ifndef NO_WOLFSSL_CLIENT
  4121. #ifdef HAVE_EXTENDED_MASTER
  4122. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4123. #endif
  4124. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4125. #ifdef HAVE_EXTENDED_MASTER
  4126. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4127. #endif
  4128. #elif !defined(NO_WOLFSSL_SERVER)
  4129. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4130. #endif
  4131. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4132. #ifdef HAVE_CRL
  4133. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4134. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4135. #endif
  4136. #ifdef HAVE_OCSP
  4137. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4138. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4139. WOLFSSL_SUCCESS);
  4140. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4141. WOLFSSL_SUCCESS);
  4142. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4143. WOLFSSL_SUCCESS);
  4144. #endif
  4145. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4146. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4147. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4148. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4149. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4150. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4151. #endif
  4152. wolfSSL_CTX_free(ctx);
  4153. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4154. #endif /* NO_CERTS */
  4155. return EXPECT_RESULT();
  4156. }
  4157. static int test_wolfSSL_CTX_ticket_API(void)
  4158. {
  4159. EXPECT_DECLS;
  4160. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4161. WOLFSSL_CTX* ctx = NULL;
  4162. void *userCtx = (void*)"this is my ctx";
  4163. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4164. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4165. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4166. wolfSSL_CTX_free(ctx);
  4167. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4168. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4169. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4170. return EXPECT_RESULT();
  4171. }
  4172. static int test_wolfSSL_set_minmax_proto_version(void)
  4173. {
  4174. EXPECT_DECLS;
  4175. #ifdef OPENSSL_EXTRA
  4176. WOLFSSL_CTX *ctx = NULL;
  4177. WOLFSSL *ssl = NULL;
  4178. (void)ssl;
  4179. #ifndef NO_WOLFSSL_CLIENT
  4180. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4181. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4182. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4183. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4184. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4185. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4186. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4187. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4188. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4189. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4190. wolfSSL_free(ssl);
  4191. wolfSSL_CTX_free(ctx);
  4192. ctx = NULL;
  4193. #endif
  4194. #ifndef NO_WOLFSSL_SERVER
  4195. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4196. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4197. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4198. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4199. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4200. wolfSSL_CTX_free(ctx);
  4201. #endif
  4202. #endif
  4203. return EXPECT_RESULT();
  4204. }
  4205. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4206. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4207. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4208. {
  4209. EXPECT_DECLS;
  4210. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4211. return EXPECT_RESULT();
  4212. }
  4213. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4214. {
  4215. EXPECT_DECLS;
  4216. /* Set TLS 1.2 */
  4217. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4218. WOLFSSL_SUCCESS);
  4219. return EXPECT_RESULT();
  4220. }
  4221. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4222. * what was set at ctx creation. */
  4223. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4224. {
  4225. EXPECT_DECLS;
  4226. test_ssl_cbf client_cbs;
  4227. test_ssl_cbf server_cbs;
  4228. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4229. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4230. client_cbs.method = wolfTLS_client_method;
  4231. server_cbs.method = wolfTLS_server_method;
  4232. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4233. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4234. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4235. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4236. &server_cbs, NULL), TEST_SUCCESS);
  4237. return EXPECT_RESULT();
  4238. }
  4239. #else
  4240. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4241. {
  4242. return TEST_SKIPPED;
  4243. }
  4244. #endif
  4245. /*----------------------------------------------------------------------------*
  4246. | SSL
  4247. *----------------------------------------------------------------------------*/
  4248. static int test_server_wolfSSL_new(void)
  4249. {
  4250. EXPECT_DECLS;
  4251. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4252. !defined(NO_WOLFSSL_SERVER)
  4253. WOLFSSL_CTX *ctx = NULL;
  4254. WOLFSSL_CTX *ctx_nocert = NULL;
  4255. WOLFSSL *ssl = NULL;
  4256. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4257. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4258. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4259. WOLFSSL_FILETYPE_PEM));
  4260. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4261. WOLFSSL_FILETYPE_PEM));
  4262. /* invalid context */
  4263. ExpectNull(ssl = wolfSSL_new(NULL));
  4264. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4265. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4266. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4267. #endif
  4268. /* success */
  4269. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4270. wolfSSL_free(ssl);
  4271. wolfSSL_CTX_free(ctx);
  4272. wolfSSL_CTX_free(ctx_nocert);
  4273. #endif
  4274. return EXPECT_RESULT();
  4275. }
  4276. static int test_client_wolfSSL_new(void)
  4277. {
  4278. EXPECT_DECLS;
  4279. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4280. !defined(NO_WOLFSSL_CLIENT)
  4281. WOLFSSL_CTX *ctx = NULL;
  4282. WOLFSSL_CTX *ctx_nocert = NULL;
  4283. WOLFSSL *ssl = NULL;
  4284. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4285. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4286. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4287. /* invalid context */
  4288. ExpectNull(ssl = wolfSSL_new(NULL));
  4289. /* success */
  4290. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4291. wolfSSL_free(ssl);
  4292. ssl = NULL;
  4293. /* success */
  4294. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4295. wolfSSL_free(ssl);
  4296. wolfSSL_CTX_free(ctx);
  4297. wolfSSL_CTX_free(ctx_nocert);
  4298. #endif
  4299. return EXPECT_RESULT();
  4300. }
  4301. static int test_wolfSSL_SetTmpDH_file(void)
  4302. {
  4303. EXPECT_DECLS;
  4304. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4305. !defined(NO_WOLFSSL_SERVER)
  4306. WOLFSSL_CTX *ctx = NULL;
  4307. WOLFSSL *ssl = NULL;
  4308. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4309. #ifndef NO_RSA
  4310. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4311. WOLFSSL_FILETYPE_PEM));
  4312. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4313. WOLFSSL_FILETYPE_PEM));
  4314. #elif defined(HAVE_ECC)
  4315. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4316. WOLFSSL_FILETYPE_PEM));
  4317. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4318. WOLFSSL_FILETYPE_PEM));
  4319. #elif defined(HAVE_ED25519)
  4320. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4321. WOLFSSL_FILETYPE_PEM));
  4322. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4323. WOLFSSL_FILETYPE_PEM));
  4324. #elif defined(HAVE_ED448)
  4325. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4326. WOLFSSL_FILETYPE_PEM));
  4327. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4328. WOLFSSL_FILETYPE_PEM));
  4329. #endif
  4330. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4331. /* invalid ssl */
  4332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4333. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4334. /* invalid dhParamFile file */
  4335. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4336. NULL, WOLFSSL_FILETYPE_PEM));
  4337. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4338. bogusFile, WOLFSSL_FILETYPE_PEM));
  4339. /* success */
  4340. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4341. WOLFSSL_FILETYPE_PEM));
  4342. wolfSSL_free(ssl);
  4343. wolfSSL_CTX_free(ctx);
  4344. #endif
  4345. return EXPECT_RESULT();
  4346. }
  4347. static int test_wolfSSL_SetTmpDH_buffer(void)
  4348. {
  4349. EXPECT_DECLS;
  4350. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4351. WOLFSSL_CTX *ctx = NULL;
  4352. WOLFSSL *ssl = NULL;
  4353. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4354. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4355. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4356. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4357. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4358. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4359. /* invalid ssl */
  4360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4361. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4362. /* invalid dhParamFile file */
  4363. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4364. 0, WOLFSSL_FILETYPE_ASN1));
  4365. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4366. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4367. /* success */
  4368. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4369. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4370. wolfSSL_free(ssl);
  4371. wolfSSL_CTX_free(ctx);
  4372. #endif
  4373. return EXPECT_RESULT();
  4374. }
  4375. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4376. {
  4377. EXPECT_DECLS;
  4378. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4379. WOLFSSL_CTX *ctx = NULL;
  4380. WOLFSSL_CTX *ctx2 = NULL;
  4381. WOLFSSL *ssl = NULL;
  4382. WOLFSSL *ssl2 = NULL;
  4383. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4384. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4385. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4386. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4387. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4388. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4389. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4390. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4391. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4392. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4393. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4394. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4395. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4396. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4397. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4398. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4399. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4400. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4401. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4402. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4403. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4404. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4405. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4406. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4407. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4408. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4409. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4410. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4411. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4412. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4413. wolfSSL_free(ssl2);
  4414. wolfSSL_CTX_free(ctx2);
  4415. wolfSSL_free(ssl);
  4416. wolfSSL_CTX_free(ctx);
  4417. #endif
  4418. return EXPECT_RESULT();
  4419. }
  4420. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4421. * allowed.
  4422. * POST: return 1 on success.
  4423. */
  4424. static int test_wolfSSL_SetMinVersion(void)
  4425. {
  4426. int res = TEST_SKIPPED;
  4427. #ifndef NO_WOLFSSL_CLIENT
  4428. int failFlag = WOLFSSL_SUCCESS;
  4429. WOLFSSL_CTX* ctx = NULL;
  4430. WOLFSSL* ssl = NULL;
  4431. int itr;
  4432. #ifndef NO_OLD_TLS
  4433. const int versions[] = {
  4434. #ifdef WOLFSSL_ALLOW_TLSV10
  4435. WOLFSSL_TLSV1,
  4436. #endif
  4437. WOLFSSL_TLSV1_1,
  4438. WOLFSSL_TLSV1_2};
  4439. #elif !defined(WOLFSSL_NO_TLS12)
  4440. const int versions[] = { WOLFSSL_TLSV1_2 };
  4441. #else
  4442. const int versions[] = { WOLFSSL_TLSV1_3 };
  4443. #endif
  4444. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4445. ssl = wolfSSL_new(ctx);
  4446. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4447. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4448. failFlag = WOLFSSL_FAILURE;
  4449. }
  4450. }
  4451. wolfSSL_free(ssl);
  4452. wolfSSL_CTX_free(ctx);
  4453. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4454. #endif
  4455. return res;
  4456. } /* END test_wolfSSL_SetMinVersion */
  4457. #ifdef OPENSSL_EXTRA
  4458. static int test_EC25519(void)
  4459. {
  4460. EXPECT_DECLS;
  4461. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4462. byte priv[CURVE25519_KEYSIZE];
  4463. unsigned int privSz = CURVE25519_KEYSIZE;
  4464. byte pub[CURVE25519_KEYSIZE];
  4465. unsigned int pubSz = CURVE25519_KEYSIZE;
  4466. byte priv2[CURVE25519_KEYSIZE];
  4467. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4468. byte pub2[CURVE25519_KEYSIZE];
  4469. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4470. byte shared[CURVE25519_KEYSIZE];
  4471. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4472. byte shared2[CURVE25519_KEYSIZE];
  4473. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4474. /* Bad parameter testing of key generation. */
  4475. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4476. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4477. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4478. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4479. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4480. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4481. /* Bad length */
  4482. privSz = 1;
  4483. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4484. privSz = CURVE25519_KEYSIZE;
  4485. pubSz = 1;
  4486. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4487. pubSz = CURVE25519_KEYSIZE;
  4488. /* Good case of generating key. */
  4489. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4490. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4491. 1);
  4492. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4493. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4494. /* Bad parameter testing of shared key. */
  4495. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4496. NULL, pubSz), 0);
  4497. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4498. NULL, pubSz), 0);
  4499. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4500. pub, pubSz), 0);
  4501. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4502. pub, pubSz), 0);
  4503. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4504. NULL, pubSz), 0);
  4505. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4506. pub, pubSz), 0);
  4507. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4508. pub, pubSz), 0);
  4509. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4510. pub, pubSz), 0);
  4511. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4512. NULL, pubSz), 0);
  4513. /* Bad length. */
  4514. sharedSz = 1;
  4515. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4516. pub, pubSz), 0);
  4517. sharedSz = CURVE25519_KEYSIZE;
  4518. privSz = 1;
  4519. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4520. pub, pubSz), 0);
  4521. privSz = CURVE25519_KEYSIZE;
  4522. pubSz = 1;
  4523. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4524. pub, pubSz), 0);
  4525. pubSz = CURVE25519_KEYSIZE;
  4526. /* Good case of shared key. */
  4527. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4528. pub2, pub2Sz), 1);
  4529. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4530. pub, pubSz), 1);
  4531. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4532. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4533. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4534. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4535. return EXPECT_RESULT();
  4536. }
  4537. static int test_ED25519(void)
  4538. {
  4539. EXPECT_DECLS;
  4540. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4541. defined(WOLFSSL_KEY_GEN)
  4542. byte priv[ED25519_PRV_KEY_SIZE];
  4543. unsigned int privSz = (unsigned int)sizeof(priv);
  4544. byte pub[ED25519_PUB_KEY_SIZE];
  4545. unsigned int pubSz = (unsigned int)sizeof(pub);
  4546. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4547. const char* msg = TEST_STRING;
  4548. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4549. byte sig[ED25519_SIG_SIZE];
  4550. unsigned int sigSz = (unsigned int)sizeof(sig);
  4551. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4552. /* Bad parameter testing of key generation. */
  4553. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4554. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4555. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4556. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4557. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4558. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4559. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4560. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4561. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4562. /* Bad length. */
  4563. privSz = 1;
  4564. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4565. privSz = ED25519_PRV_KEY_SIZE;
  4566. pubSz = 1;
  4567. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4568. pubSz = ED25519_PUB_KEY_SIZE;
  4569. /* Good case of generating key. */
  4570. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4571. 1);
  4572. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4573. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4574. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4575. /* Bad parameter testing of signing. */
  4576. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4577. NULL), 0);
  4578. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4579. NULL), 0);
  4580. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4581. NULL), 0);
  4582. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4583. NULL), 0);
  4584. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4585. &sigSz), 0);
  4586. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4587. &sigSz), 0);
  4588. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4589. &sigSz), 0);
  4590. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4591. &sigSz), 0);
  4592. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4593. NULL), 0);
  4594. /* Bad length. */
  4595. privSz = 1;
  4596. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4597. &sigSz), 0);
  4598. privSz = ED25519_PRV_KEY_SIZE;
  4599. sigSz = 1;
  4600. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4601. &sigSz), 0);
  4602. sigSz = ED25519_SIG_SIZE;
  4603. /* Good case of signing. */
  4604. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4605. &sigSz), 1);
  4606. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4607. #ifdef HAVE_ED25519_VERIFY
  4608. /* Bad parameter testing of verification. */
  4609. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4610. sigSz), 0);
  4611. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4612. sigSz), 0);
  4613. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4614. sigSz), 0);
  4615. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4616. sigSz), 0);
  4617. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4618. sigSz), 0);
  4619. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4620. sigSz), 0);
  4621. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4622. sigSz), 0);
  4623. /* Bad length. */
  4624. pubSz = 1;
  4625. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4626. sigSz), 0);
  4627. pubSz = ED25519_PUB_KEY_SIZE;
  4628. sigSz = 1;
  4629. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4630. sigSz), 0);
  4631. sigSz = ED25519_SIG_SIZE;
  4632. /* Good case of verification. */
  4633. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4634. sigSz), 1);
  4635. /* Bad signature. */
  4636. if (EXPECT_SUCCESS()) {
  4637. sig[1] ^= 0x80;
  4638. }
  4639. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4640. sigSz), 0);
  4641. #endif /* HAVE_ED25519_VERIFY */
  4642. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4643. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4644. return EXPECT_RESULT();
  4645. }
  4646. static int test_EC448(void)
  4647. {
  4648. EXPECT_DECLS;
  4649. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4650. byte priv[CURVE448_KEY_SIZE];
  4651. unsigned int privSz = CURVE448_KEY_SIZE;
  4652. byte pub[CURVE448_KEY_SIZE];
  4653. unsigned int pubSz = CURVE448_KEY_SIZE;
  4654. byte priv2[CURVE448_KEY_SIZE];
  4655. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4656. byte pub2[CURVE448_KEY_SIZE];
  4657. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4658. byte shared[CURVE448_KEY_SIZE];
  4659. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4660. byte shared2[CURVE448_KEY_SIZE];
  4661. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4662. /* Bad parameter testing of key generation. */
  4663. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4664. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4665. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4666. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4667. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4668. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4669. /* Bad length. */
  4670. privSz = 1;
  4671. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4672. privSz = CURVE448_KEY_SIZE;
  4673. pubSz = 1;
  4674. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4675. pubSz = CURVE448_KEY_SIZE;
  4676. /* Good case of generating key. */
  4677. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4678. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4679. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4680. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4681. /* Bad parameter testing of shared key. */
  4682. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4683. NULL, pubSz), 0);
  4684. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4685. NULL, pubSz), 0);
  4686. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4687. pub, pubSz), 0);
  4688. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4689. pub, pubSz), 0);
  4690. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4691. NULL, pubSz), 0);
  4692. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4693. pub, pubSz), 0);
  4694. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4695. pub, pubSz), 0);
  4696. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4697. pub, pubSz), 0);
  4698. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4699. NULL, pubSz), 0);
  4700. /* Bad length. */
  4701. sharedSz = 1;
  4702. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4703. pub, pubSz), 0);
  4704. sharedSz = CURVE448_KEY_SIZE;
  4705. privSz = 1;
  4706. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4707. pub, pubSz), 0);
  4708. privSz = CURVE448_KEY_SIZE;
  4709. pubSz = 1;
  4710. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4711. pub, pubSz), 0);
  4712. pubSz = CURVE448_KEY_SIZE;
  4713. /* Good case of shared key. */
  4714. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4715. pub2, pub2Sz), 1);
  4716. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4717. pub, pubSz), 1);
  4718. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4719. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4720. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4721. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4722. return EXPECT_RESULT();
  4723. }
  4724. static int test_ED448(void)
  4725. {
  4726. EXPECT_DECLS;
  4727. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4728. defined(WOLFSSL_KEY_GEN)
  4729. byte priv[ED448_PRV_KEY_SIZE];
  4730. unsigned int privSz = (unsigned int)sizeof(priv);
  4731. byte pub[ED448_PUB_KEY_SIZE];
  4732. unsigned int pubSz = (unsigned int)sizeof(pub);
  4733. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4734. const char* msg = TEST_STRING;
  4735. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4736. byte sig[ED448_SIG_SIZE];
  4737. unsigned int sigSz = (unsigned int)sizeof(sig);
  4738. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4739. /* Bad parameter testing of key generation. */
  4740. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4741. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4742. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4743. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4744. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4745. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4746. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4747. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4748. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4749. /* Bad length. */
  4750. privSz = 1;
  4751. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4752. privSz = ED448_PRV_KEY_SIZE;
  4753. pubSz = 1;
  4754. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4755. pubSz = ED448_PUB_KEY_SIZE;
  4756. /* Good case of generating key. */
  4757. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4758. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4759. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4760. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4761. /* Bad parameter testing of signing. */
  4762. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4763. NULL), 0);
  4764. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4765. NULL), 0);
  4766. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4767. NULL), 0);
  4768. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4769. NULL), 0);
  4770. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4771. &sigSz), 0);
  4772. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4773. &sigSz), 0);
  4774. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4775. &sigSz), 0);
  4776. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4777. &sigSz), 0);
  4778. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4779. NULL), 0);
  4780. /* Bad length. */
  4781. privSz = 1;
  4782. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4783. &sigSz), 0);
  4784. privSz = ED448_PRV_KEY_SIZE;
  4785. sigSz = 1;
  4786. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4787. &sigSz), 0);
  4788. sigSz = ED448_SIG_SIZE;
  4789. /* Good case of signing. */
  4790. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4791. &sigSz), 1);
  4792. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4793. #ifdef HAVE_ED448_VERIFY
  4794. /* Bad parameter testing of verification. */
  4795. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4796. sigSz), 0);
  4797. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4798. sigSz), 0);
  4799. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4800. sigSz), 0);
  4801. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4802. sigSz), 0);
  4803. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4804. sigSz), 0);
  4805. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4806. sigSz), 0);
  4807. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4808. sigSz), 0);
  4809. /* Bad length. */
  4810. pubSz = 1;
  4811. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4812. sigSz), 0);
  4813. pubSz = ED448_PUB_KEY_SIZE;
  4814. sigSz = 1;
  4815. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4816. sigSz), 0);
  4817. sigSz = ED448_SIG_SIZE;
  4818. /* Good case of verification. */
  4819. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4820. sigSz), 1);
  4821. /* Bad signature. */
  4822. if (EXPECT_SUCCESS()) {
  4823. sig[1] ^= 0x80;
  4824. }
  4825. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4826. sigSz), 0);
  4827. #endif /* HAVE_ED448_VERIFY */
  4828. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4829. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4830. return EXPECT_RESULT();
  4831. }
  4832. #endif /* OPENSSL_EXTRA */
  4833. #include <wolfssl/openssl/pem.h>
  4834. /*----------------------------------------------------------------------------*
  4835. | EVP
  4836. *----------------------------------------------------------------------------*/
  4837. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4838. {
  4839. EXPECT_DECLS;
  4840. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4841. WOLFSSL_BIO* rbio = NULL;
  4842. WOLFSSL_BIO* wbio = NULL;
  4843. WOLFSSL_EVP_PKEY* pkey = NULL;
  4844. char line[256] = { 0 };
  4845. char line1[256] = { 0 };
  4846. int i = 0;
  4847. /* test error cases */
  4848. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4849. /*
  4850. * test RSA public key print
  4851. * in this test, pass '3' for indent
  4852. */
  4853. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4854. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4855. sizeof_client_keypub_der_1024));
  4856. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4857. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4858. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4859. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4860. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4861. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. strcpy(line1, " Modulus:\n");
  4864. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4865. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4866. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4867. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4868. /* skip to the end of modulus element*/
  4869. for (i = 0; i < 8 ;i++) {
  4870. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4871. }
  4872. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4873. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4874. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4875. /* should reach EOF */
  4876. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4877. EVP_PKEY_free(pkey);
  4878. pkey = NULL;
  4879. BIO_free(rbio);
  4880. BIO_free(wbio);
  4881. rbio = NULL;
  4882. wbio = NULL;
  4883. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4884. /*
  4885. * test DSA public key print
  4886. */
  4887. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4888. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4889. sizeof_dsa_pub_key_der_2048));
  4890. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4891. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4892. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4893. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4894. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4895. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4896. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4897. strcpy(line1, "pub:\n");
  4898. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4899. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4900. strcpy(line1,
  4901. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4902. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4903. /* skip to the end of pub element*/
  4904. for (i = 0; i < 17 ;i++) {
  4905. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4906. }
  4907. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4908. strcpy(line1, "P:\n");
  4909. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4910. /* skip to the end of P element*/
  4911. for (i = 0; i < 18 ;i++) {
  4912. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4913. }
  4914. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4915. strcpy(line1, "Q:\n");
  4916. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4917. /* skip to the end of Q element*/
  4918. for (i = 0; i < 3 ;i++) {
  4919. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4920. }
  4921. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4922. strcpy(line1, "G:\n");
  4923. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4924. /* skip to the end of G element*/
  4925. for (i = 0; i < 18 ;i++) {
  4926. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4927. }
  4928. /* should reach EOF */
  4929. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4930. EVP_PKEY_free(pkey);
  4931. pkey = NULL;
  4932. BIO_free(rbio);
  4933. BIO_free(wbio);
  4934. rbio = NULL;
  4935. wbio = NULL;
  4936. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4937. /*
  4938. * test ECC public key print
  4939. */
  4940. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4941. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4942. sizeof_ecc_clikeypub_der_256));
  4943. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4944. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4945. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4946. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4947. strcpy(line1, "Public-Key: (256 bit)\n");
  4948. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4949. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. strcpy(line1, "pub:\n");
  4951. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4952. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4953. strcpy(line1,
  4954. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4955. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4956. /* skip to the end of pub element*/
  4957. for (i = 0; i < 4 ;i++) {
  4958. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4959. }
  4960. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4961. strcpy(line1, "ASN1 OID: prime256v1\n");
  4962. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4963. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4964. strcpy(line1, "NIST CURVE: P-256\n");
  4965. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4966. /* should reach EOF */
  4967. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4968. EVP_PKEY_free(pkey);
  4969. pkey = NULL;
  4970. BIO_free(rbio);
  4971. BIO_free(wbio);
  4972. rbio = NULL;
  4973. wbio = NULL;
  4974. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4975. /*
  4976. * test DH public key print
  4977. */
  4978. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4979. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4980. sizeof_dh_pub_key_der_2048));
  4981. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4982. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4983. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4984. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4985. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4986. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4987. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4988. strcpy(line1, "public-key:\n");
  4989. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4990. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4991. strcpy(line1,
  4992. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4993. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4994. /* skip to the end of public-key element*/
  4995. for (i = 0; i < 17 ;i++) {
  4996. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4997. }
  4998. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4999. strcpy(line1, "prime:\n");
  5000. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5001. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5002. strcpy(line1,
  5003. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5004. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5005. /* skip to the end of prime element*/
  5006. for (i = 0; i < 17 ;i++) {
  5007. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5008. }
  5009. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5010. strcpy(line1, "generator: 2 (0x02)\n");
  5011. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5012. /* should reach EOF */
  5013. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5014. EVP_PKEY_free(pkey);
  5015. pkey = NULL;
  5016. BIO_free(rbio);
  5017. BIO_free(wbio);
  5018. rbio = NULL;
  5019. wbio = NULL;
  5020. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5021. /* to prevent "unused variable" warning */
  5022. (void)pkey;
  5023. (void)wbio;
  5024. (void)rbio;
  5025. (void)line;
  5026. (void)line1;
  5027. (void)i;
  5028. #endif /* OPENSSL_EXTRA */
  5029. return EXPECT_RESULT();
  5030. }
  5031. /* Test functions for base64 encode/decode */
  5032. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5033. {
  5034. EXPECT_DECLS;
  5035. #if defined(OPENSSL_EXTRA) && \
  5036. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5037. EVP_ENCODE_CTX* ctx = NULL;
  5038. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5039. ExpectIntEQ(ctx->remaining,0);
  5040. ExpectIntEQ(ctx->data[0],0);
  5041. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5042. EVP_ENCODE_CTX_free(ctx);
  5043. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5044. return EXPECT_RESULT();
  5045. }
  5046. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5047. {
  5048. EXPECT_DECLS;
  5049. #if defined(OPENSSL_EXTRA) && \
  5050. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5051. EVP_ENCODE_CTX* ctx = NULL;
  5052. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5053. EVP_ENCODE_CTX_free(ctx);
  5054. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5055. return EXPECT_RESULT();
  5056. }
  5057. static int test_wolfSSL_EVP_EncodeInit(void)
  5058. {
  5059. EXPECT_DECLS;
  5060. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5061. EVP_ENCODE_CTX* ctx = NULL;
  5062. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5063. ExpectIntEQ(ctx->remaining, 0);
  5064. ExpectIntEQ(ctx->data[0], 0);
  5065. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5066. if (ctx != NULL) {
  5067. /* make ctx dirty */
  5068. ctx->remaining = 10;
  5069. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5070. }
  5071. EVP_EncodeInit(ctx);
  5072. ExpectIntEQ(ctx->remaining, 0);
  5073. ExpectIntEQ(ctx->data[0], 0);
  5074. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5075. EVP_ENCODE_CTX_free(ctx);
  5076. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5077. return EXPECT_RESULT();
  5078. }
  5079. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5080. {
  5081. EXPECT_DECLS;
  5082. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5083. int outl;
  5084. int total;
  5085. const unsigned char plain0[] = {"Th"};
  5086. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5087. const unsigned char plain2[] = {"This is additional data."};
  5088. const unsigned char encBlock0[] = {"VGg="};
  5089. const unsigned char enc0[] = {"VGg=\n"};
  5090. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5091. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5092. const unsigned char enc2[] =
  5093. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5094. unsigned char encOutBuff[300];
  5095. EVP_ENCODE_CTX* ctx = NULL;
  5096. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5097. EVP_EncodeInit(ctx);
  5098. /* illegal parameter test */
  5099. ExpectIntEQ(
  5100. EVP_EncodeUpdate(
  5101. NULL, /* pass NULL as ctx */
  5102. encOutBuff,
  5103. &outl,
  5104. plain1,
  5105. sizeof(plain1)-1),
  5106. 0 /* expected result code 0: fail */
  5107. );
  5108. ExpectIntEQ(
  5109. EVP_EncodeUpdate(
  5110. ctx,
  5111. NULL, /* pass NULL as out buff */
  5112. &outl,
  5113. plain1,
  5114. sizeof(plain1)-1),
  5115. 0 /* expected result code 0: fail */
  5116. );
  5117. ExpectIntEQ(
  5118. EVP_EncodeUpdate(
  5119. ctx,
  5120. encOutBuff,
  5121. NULL, /* pass NULL as outl */
  5122. plain1,
  5123. sizeof(plain1)-1),
  5124. 0 /* expected result code 0: fail */
  5125. );
  5126. ExpectIntEQ(
  5127. EVP_EncodeUpdate(
  5128. ctx,
  5129. encOutBuff,
  5130. &outl,
  5131. NULL, /* pass NULL as in */
  5132. sizeof(plain1)-1),
  5133. 0 /* expected result code 0: fail */
  5134. );
  5135. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5136. /* meaningless parameter test */
  5137. ExpectIntEQ(
  5138. EVP_EncodeUpdate(
  5139. ctx,
  5140. encOutBuff,
  5141. &outl,
  5142. plain1,
  5143. 0), /* pass zero input */
  5144. 1 /* expected result code 1: success */
  5145. );
  5146. /* very small data encoding test */
  5147. EVP_EncodeInit(ctx);
  5148. ExpectIntEQ(
  5149. EVP_EncodeUpdate(
  5150. ctx,
  5151. encOutBuff,
  5152. &outl,
  5153. plain0,
  5154. sizeof(plain0)-1),
  5155. 1 /* expected result code 1: success */
  5156. );
  5157. ExpectIntEQ(outl,0);
  5158. if (EXPECT_SUCCESS()) {
  5159. EVP_EncodeFinal(
  5160. ctx,
  5161. encOutBuff + outl,
  5162. &outl);
  5163. }
  5164. ExpectIntEQ( outl, sizeof(enc0)-1);
  5165. ExpectIntEQ(
  5166. XSTRNCMP(
  5167. (const char*)encOutBuff,
  5168. (const char*)enc0,sizeof(enc0) ),
  5169. 0);
  5170. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5171. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5172. sizeof(encBlock0)-1);
  5173. ExpectStrEQ(encOutBuff, encBlock0);
  5174. /* pass small size( < 48bytes ) input, then make sure they are not
  5175. * encoded and just stored in ctx
  5176. */
  5177. EVP_EncodeInit(ctx);
  5178. total = 0;
  5179. outl = 0;
  5180. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5181. ExpectIntEQ(
  5182. EVP_EncodeUpdate(
  5183. ctx,
  5184. encOutBuff, /* buffer for output */
  5185. &outl, /* size of output */
  5186. plain1, /* input */
  5187. sizeof(plain1)-1), /* size of input */
  5188. 1); /* expected result code 1:success */
  5189. total += outl;
  5190. ExpectIntEQ(outl, 0); /* no output expected */
  5191. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5192. ExpectTrue(
  5193. XSTRNCMP((const char*)(ctx->data),
  5194. (const char*)plain1,
  5195. ctx->remaining) ==0 );
  5196. ExpectTrue(encOutBuff[0] == 0);
  5197. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5198. * the stored data and the new input together
  5199. */
  5200. ExpectIntEQ(
  5201. EVP_EncodeUpdate(
  5202. ctx,
  5203. encOutBuff + outl, /* buffer for output */
  5204. &outl, /* size of output */
  5205. plain2, /* additional input */
  5206. sizeof(plain2) -1), /* size of additional input */
  5207. 1); /* expected result code 1:success */
  5208. total += outl;
  5209. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5210. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5211. ExpectIntEQ(
  5212. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5213. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5214. EVP_EncodeFinal(
  5215. ctx,
  5216. encOutBuff + outl,
  5217. &outl);
  5218. total += outl;
  5219. ExpectIntNE(total,0);
  5220. ExpectIntNE(outl,0);
  5221. ExpectIntEQ(XSTRNCMP(
  5222. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5223. /* test with illeagal parameters */
  5224. outl = 1;
  5225. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5226. ExpectIntEQ(outl, 0);
  5227. outl = 1;
  5228. EVP_EncodeFinal(ctx, NULL, &outl);
  5229. ExpectIntEQ(outl, 0);
  5230. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5231. EVP_EncodeFinal(NULL, NULL, NULL);
  5232. EVP_ENCODE_CTX_free(ctx);
  5233. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5234. return EXPECT_RESULT();
  5235. }
  5236. static int test_wolfSSL_EVP_EncodeFinal(void)
  5237. {
  5238. int res = TEST_SKIPPED;
  5239. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5240. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5241. * test_wolfSSL_EVP_EncodeUpdate
  5242. */
  5243. res = TEST_SUCCESS;
  5244. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5245. return res;
  5246. }
  5247. static int test_wolfSSL_EVP_DecodeInit(void)
  5248. {
  5249. EXPECT_DECLS;
  5250. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5251. EVP_ENCODE_CTX* ctx = NULL;
  5252. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5253. ExpectIntEQ( ctx->remaining,0);
  5254. ExpectIntEQ( ctx->data[0],0);
  5255. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5256. if (ctx != NULL) {
  5257. /* make ctx dirty */
  5258. ctx->remaining = 10;
  5259. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5260. }
  5261. EVP_DecodeInit(ctx);
  5262. ExpectIntEQ( ctx->remaining,0);
  5263. ExpectIntEQ( ctx->data[0],0);
  5264. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5265. EVP_ENCODE_CTX_free(ctx);
  5266. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5267. return EXPECT_RESULT();
  5268. }
  5269. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5270. {
  5271. EXPECT_DECLS;
  5272. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5273. int outl;
  5274. unsigned char decOutBuff[300];
  5275. EVP_ENCODE_CTX* ctx = NULL;
  5276. static const unsigned char enc1[] =
  5277. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5278. /* const unsigned char plain1[] =
  5279. {"This is a base64 decoding test."} */
  5280. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5281. EVP_DecodeInit(ctx);
  5282. /* illegal parameter tests */
  5283. /* pass NULL as ctx */
  5284. ExpectIntEQ(
  5285. EVP_DecodeUpdate(
  5286. NULL, /* pass NULL as ctx */
  5287. decOutBuff,
  5288. &outl,
  5289. enc1,
  5290. sizeof(enc1)-1),
  5291. -1 /* expected result code -1: fail */
  5292. );
  5293. ExpectIntEQ( outl, 0);
  5294. /* pass NULL as output */
  5295. ExpectIntEQ(
  5296. EVP_DecodeUpdate(
  5297. ctx,
  5298. NULL, /* pass NULL as out buff */
  5299. &outl,
  5300. enc1,
  5301. sizeof(enc1)-1),
  5302. -1 /* expected result code -1: fail */
  5303. );
  5304. ExpectIntEQ( outl, 0);
  5305. /* pass NULL as outl */
  5306. ExpectIntEQ(
  5307. EVP_DecodeUpdate(
  5308. ctx,
  5309. decOutBuff,
  5310. NULL, /* pass NULL as outl */
  5311. enc1,
  5312. sizeof(enc1)-1),
  5313. -1 /* expected result code -1: fail */
  5314. );
  5315. /* pass NULL as input */
  5316. ExpectIntEQ(
  5317. EVP_DecodeUpdate(
  5318. ctx,
  5319. decOutBuff,
  5320. &outl,
  5321. NULL, /* pass NULL as in */
  5322. sizeof(enc1)-1),
  5323. -1 /* expected result code -1: fail */
  5324. );
  5325. ExpectIntEQ( outl, 0);
  5326. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5327. /* pass zero length input */
  5328. ExpectIntEQ(
  5329. EVP_DecodeUpdate(
  5330. ctx,
  5331. decOutBuff,
  5332. &outl,
  5333. enc1,
  5334. 0), /* pass zero as input len */
  5335. 1 /* expected result code 1: success */
  5336. );
  5337. /* decode correct base64 string */
  5338. {
  5339. static const unsigned char enc2[] =
  5340. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5341. static const unsigned char plain2[] =
  5342. {"This is a base64 decoding test."};
  5343. EVP_EncodeInit(ctx);
  5344. ExpectIntEQ(
  5345. EVP_DecodeUpdate(
  5346. ctx,
  5347. decOutBuff,
  5348. &outl,
  5349. enc2,
  5350. sizeof(enc2)-1),
  5351. 0 /* expected result code 0: success */
  5352. );
  5353. ExpectIntEQ(outl,sizeof(plain2) -1);
  5354. ExpectIntEQ(
  5355. EVP_DecodeFinal(
  5356. ctx,
  5357. decOutBuff + outl,
  5358. &outl),
  5359. 1 /* expected result code 1: success */
  5360. );
  5361. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5362. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5363. sizeof(plain2) -1 ),0);
  5364. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5365. sizeof(plain2)-1);
  5366. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5367. sizeof(plain2) -1 ),0);
  5368. }
  5369. /* decode correct base64 string which does not have '\n' in its last*/
  5370. {
  5371. static const unsigned char enc3[] =
  5372. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5373. static const unsigned char plain3[] =
  5374. {"This is a base64 decoding test."}; /* 31 chars */
  5375. EVP_EncodeInit(ctx);
  5376. ExpectIntEQ(
  5377. EVP_DecodeUpdate(
  5378. ctx,
  5379. decOutBuff,
  5380. &outl,
  5381. enc3,
  5382. sizeof(enc3)-1),
  5383. 0 /* expected result code 0: success */
  5384. );
  5385. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5386. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5387. sizeof(plain3) -1 ),0);
  5388. ExpectIntEQ(
  5389. EVP_DecodeFinal(
  5390. ctx,
  5391. decOutBuff + outl,
  5392. &outl),
  5393. 1 /* expected result code 1: success */
  5394. );
  5395. ExpectIntEQ(outl,0 );
  5396. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5397. sizeof(plain3)-1);
  5398. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5399. sizeof(plain3) -1 ),0);
  5400. }
  5401. /* decode string which has a padding char ('=') in the illegal position*/
  5402. {
  5403. static const unsigned char enc4[] =
  5404. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5405. EVP_EncodeInit(ctx);
  5406. ExpectIntEQ(
  5407. EVP_DecodeUpdate(
  5408. ctx,
  5409. decOutBuff,
  5410. &outl,
  5411. enc4,
  5412. sizeof(enc4)-1),
  5413. -1 /* expected result code -1: error */
  5414. );
  5415. ExpectIntEQ(outl,0);
  5416. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5417. }
  5418. /* small data decode test */
  5419. {
  5420. static const unsigned char enc00[] = {"VG"};
  5421. static const unsigned char enc01[] = {"g=\n"};
  5422. static const unsigned char plain4[] = {"Th"};
  5423. EVP_EncodeInit(ctx);
  5424. ExpectIntEQ(
  5425. EVP_DecodeUpdate(
  5426. ctx,
  5427. decOutBuff,
  5428. &outl,
  5429. enc00,
  5430. sizeof(enc00)-1),
  5431. 1 /* expected result code 1: success */
  5432. );
  5433. ExpectIntEQ(outl,0);
  5434. ExpectIntEQ(
  5435. EVP_DecodeUpdate(
  5436. ctx,
  5437. decOutBuff + outl,
  5438. &outl,
  5439. enc01,
  5440. sizeof(enc01)-1),
  5441. 0 /* expected result code 0: success */
  5442. );
  5443. ExpectIntEQ(outl,sizeof(plain4)-1);
  5444. /* test with illegal parameters */
  5445. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5446. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5447. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5448. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5449. if (EXPECT_SUCCESS()) {
  5450. EVP_DecodeFinal(
  5451. ctx,
  5452. decOutBuff + outl,
  5453. &outl);
  5454. }
  5455. ExpectIntEQ( outl, 0);
  5456. ExpectIntEQ(
  5457. XSTRNCMP(
  5458. (const char*)decOutBuff,
  5459. (const char*)plain4,sizeof(plain4)-1 ),
  5460. 0);
  5461. }
  5462. EVP_ENCODE_CTX_free(ctx);
  5463. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5464. return EXPECT_RESULT();
  5465. }
  5466. static int test_wolfSSL_EVP_DecodeFinal(void)
  5467. {
  5468. int res = TEST_SKIPPED;
  5469. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5470. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5471. * test_wolfSSL_EVP_DecodeUpdate
  5472. */
  5473. res = TEST_SUCCESS;
  5474. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5475. return res;
  5476. }
  5477. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5478. */
  5479. #ifdef OPENSSL_EXTRA
  5480. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5481. {
  5482. EXPECT_DECLS;
  5483. #ifndef NO_AES
  5484. const WOLFSSL_EVP_CIPHER* c;
  5485. c = wolfSSL_EVP_get_cipherbynid(419);
  5486. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5487. defined(WOLFSSL_AES_128)
  5488. ExpectNotNull(c);
  5489. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5490. #else
  5491. ExpectNull(c);
  5492. #endif
  5493. c = wolfSSL_EVP_get_cipherbynid(423);
  5494. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5495. defined(WOLFSSL_AES_192)
  5496. ExpectNotNull(c);
  5497. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5498. #else
  5499. ExpectNull(c);
  5500. #endif
  5501. c = wolfSSL_EVP_get_cipherbynid(427);
  5502. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5503. defined(WOLFSSL_AES_256)
  5504. ExpectNotNull(c);
  5505. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5506. #else
  5507. ExpectNull(c);
  5508. #endif
  5509. c = wolfSSL_EVP_get_cipherbynid(904);
  5510. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5511. ExpectNotNull(c);
  5512. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5513. #else
  5514. ExpectNull(c);
  5515. #endif
  5516. c = wolfSSL_EVP_get_cipherbynid(905);
  5517. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5518. ExpectNotNull(c);
  5519. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5520. #else
  5521. ExpectNull(c);
  5522. #endif
  5523. c = wolfSSL_EVP_get_cipherbynid(906);
  5524. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5525. ExpectNotNull(c);
  5526. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5527. #else
  5528. ExpectNull(c);
  5529. #endif
  5530. c = wolfSSL_EVP_get_cipherbynid(418);
  5531. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5532. ExpectNotNull(c);
  5533. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5534. #else
  5535. ExpectNull(c);
  5536. #endif
  5537. c = wolfSSL_EVP_get_cipherbynid(422);
  5538. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5539. ExpectNotNull(c);
  5540. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5541. #else
  5542. ExpectNull(c);
  5543. #endif
  5544. c = wolfSSL_EVP_get_cipherbynid(426);
  5545. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5546. ExpectNotNull(c);
  5547. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5548. #else
  5549. ExpectNull(c);
  5550. #endif
  5551. #endif /* !NO_AES */
  5552. #ifndef NO_DES3
  5553. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5554. #ifdef WOLFSSL_DES_ECB
  5555. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5556. #endif
  5557. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5558. #ifdef WOLFSSL_DES_ECB
  5559. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5560. #endif
  5561. #endif /* !NO_DES3 */
  5562. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5563. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5564. #endif
  5565. /* test for nid is out of range */
  5566. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5567. return EXPECT_RESULT();
  5568. }
  5569. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5570. {
  5571. EXPECT_DECLS;
  5572. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5573. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5574. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5575. const EVP_CIPHER *test;
  5576. byte key[AES_BLOCK_SIZE] = {0};
  5577. byte iv[AES_BLOCK_SIZE] = {0};
  5578. ExpectNotNull(ctx);
  5579. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5580. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5581. test = EVP_CIPHER_CTX_cipher(ctx);
  5582. ExpectTrue(init == test);
  5583. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5584. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5585. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5586. EVP_CIPHER_CTX_free(ctx);
  5587. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5588. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5589. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5590. return EXPECT_RESULT();
  5591. }
  5592. #endif /* OPENSSL_EXTRA */
  5593. /*----------------------------------------------------------------------------*
  5594. | IO
  5595. *----------------------------------------------------------------------------*/
  5596. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5597. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5598. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5599. #ifdef WC_SHA512_DIGEST_SIZE
  5600. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5601. #else
  5602. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5603. #endif
  5604. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5605. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5606. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5607. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5608. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5609. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5610. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5611. defined(HAVE_AES_CBC)
  5612. typedef struct openssl_key_ctx {
  5613. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5614. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5615. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5616. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5617. } openssl_key_ctx;
  5618. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5619. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5620. static WC_INLINE int OpenSSLTicketInit(void)
  5621. {
  5622. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5623. if (ret != 0) return ret;
  5624. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5625. sizeof(myOpenSSLKey_ctx.name));
  5626. if (ret != 0) return ret;
  5627. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5628. sizeof(myOpenSSLKey_ctx.key));
  5629. if (ret != 0) return ret;
  5630. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5631. sizeof(myOpenSSLKey_ctx.hmacKey));
  5632. if (ret != 0) return ret;
  5633. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5634. sizeof(myOpenSSLKey_ctx.iv));
  5635. if (ret != 0) return ret;
  5636. return 0;
  5637. }
  5638. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5639. byte name[WOLFSSL_TICKET_NAME_SZ],
  5640. byte iv[WOLFSSL_TICKET_IV_SZ],
  5641. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5642. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5643. (void)ssl;
  5644. if (enc) {
  5645. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5646. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5647. }
  5648. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5649. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5650. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5651. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5652. return 0;
  5653. }
  5654. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5655. if (enc)
  5656. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5657. else
  5658. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5659. return 1;
  5660. }
  5661. static WC_INLINE void OpenSSLTicketCleanup(void)
  5662. {
  5663. wc_FreeRng(&myOpenSSLKey_rng);
  5664. }
  5665. #endif
  5666. #endif
  5667. /* helper functions */
  5668. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5669. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5670. void *ctx)
  5671. {
  5672. struct test_ssl_memio_ctx *test_ctx;
  5673. byte *buf;
  5674. int *len;
  5675. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5676. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5677. buf = test_ctx->c_buff;
  5678. len = &test_ctx->c_len;
  5679. }
  5680. else {
  5681. buf = test_ctx->s_buff;
  5682. len = &test_ctx->s_len;
  5683. }
  5684. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5685. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5686. XMEMCPY(buf + *len, data, sz);
  5687. *len += sz;
  5688. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5689. {
  5690. /* This can be imported into Wireshark by transforming the file with
  5691. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5692. * And then loading test_output.dump.hex into Wireshark using the
  5693. * "Import from Hex Dump..." option ion and selecting the TCP
  5694. * encapsulation option. */
  5695. char dump_file_name[64];
  5696. WOLFSSL_BIO *dump_file;
  5697. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5698. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5699. if (dump_file != NULL) {
  5700. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5701. wolfSSL_BIO_free(dump_file);
  5702. }
  5703. }
  5704. #endif
  5705. return sz;
  5706. }
  5707. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5708. void *ctx)
  5709. {
  5710. struct test_ssl_memio_ctx *test_ctx;
  5711. int read_sz;
  5712. byte *buf;
  5713. int *len;
  5714. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5715. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5716. buf = test_ctx->s_buff;
  5717. len = &test_ctx->s_len;
  5718. }
  5719. else {
  5720. buf = test_ctx->c_buff;
  5721. len = &test_ctx->c_len;
  5722. }
  5723. if (*len == 0)
  5724. return WOLFSSL_CBIO_ERR_WANT_READ;
  5725. read_sz = sz < *len ? sz : *len;
  5726. XMEMCPY(data, buf, read_sz);
  5727. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5728. *len -= read_sz;
  5729. return read_sz;
  5730. }
  5731. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5732. {
  5733. EXPECT_DECLS;
  5734. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5735. int c_sharedCtx = 0;
  5736. int s_sharedCtx = 0;
  5737. #endif
  5738. const char* clientCertFile = cliCertFile;
  5739. const char* clientKeyFile = cliKeyFile;
  5740. const char* serverCertFile = svrCertFile;
  5741. const char* serverKeyFile = svrKeyFile;
  5742. /********************************
  5743. * Create WOLFSSL_CTX for client.
  5744. ********************************/
  5745. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5746. if (ctx->c_ctx != NULL) {
  5747. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5748. }
  5749. else
  5750. #endif
  5751. {
  5752. WOLFSSL_METHOD* method = NULL;
  5753. if (ctx->c_cb.method != NULL) {
  5754. method = ctx->c_cb.method();
  5755. }
  5756. else {
  5757. method = wolfSSLv23_client_method();
  5758. }
  5759. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5760. }
  5761. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5762. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5763. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5764. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5765. #endif
  5766. if (ctx->c_cb.caPemFile != NULL)
  5767. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5768. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5769. else
  5770. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5771. caCertFile, 0), WOLFSSL_SUCCESS);
  5772. if (ctx->c_cb.certPemFile != NULL) {
  5773. clientCertFile = ctx->c_cb.certPemFile;
  5774. }
  5775. if (ctx->c_cb.keyPemFile != NULL) {
  5776. clientKeyFile = ctx->c_cb.keyPemFile;
  5777. }
  5778. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5779. if (!c_sharedCtx)
  5780. #endif
  5781. {
  5782. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5783. clientCertFile), WOLFSSL_SUCCESS);
  5784. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5785. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5786. }
  5787. #ifdef HAVE_CRL
  5788. if (ctx->c_cb.crlPemFile != NULL) {
  5789. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5790. WOLFSSL_SUCCESS);
  5791. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5792. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5793. }
  5794. #endif
  5795. if (ctx->c_ciphers != NULL) {
  5796. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5797. WOLFSSL_SUCCESS);
  5798. }
  5799. if (ctx->c_cb.ctx_ready != NULL) {
  5800. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5801. }
  5802. /********************************
  5803. * Create WOLFSSL_CTX for server.
  5804. ********************************/
  5805. if (ctx->s_ctx != NULL) {
  5806. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5807. s_sharedCtx = 1;
  5808. #endif
  5809. ctx->s_cb.isSharedCtx = 1;
  5810. }
  5811. else
  5812. {
  5813. WOLFSSL_METHOD* method = NULL;
  5814. if (ctx->s_cb.method != NULL) {
  5815. method = ctx->s_cb.method();
  5816. }
  5817. else {
  5818. method = wolfSSLv23_server_method();
  5819. }
  5820. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5821. ctx->s_cb.isSharedCtx = 0;
  5822. }
  5823. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5824. #if defined(HAVE_SESSION_TICKET) && \
  5825. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5826. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5827. OpenSSLTicketInit();
  5828. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5829. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5830. TicketInit();
  5831. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5832. #endif
  5833. #endif
  5834. }
  5835. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5836. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5837. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5838. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5839. if (ctx->s_cb.caPemFile != NULL)
  5840. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5841. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5842. else
  5843. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5844. cliCertFile, 0), WOLFSSL_SUCCESS);
  5845. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5846. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5847. #endif
  5848. if (ctx->s_cb.certPemFile != NULL) {
  5849. serverCertFile = ctx->s_cb.certPemFile;
  5850. }
  5851. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5852. if (!s_sharedCtx)
  5853. #endif
  5854. {
  5855. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5856. serverCertFile), WOLFSSL_SUCCESS);
  5857. }
  5858. if (ctx->s_cb.keyPemFile != NULL) {
  5859. serverKeyFile = ctx->s_cb.keyPemFile;
  5860. }
  5861. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5862. if (!s_sharedCtx)
  5863. #endif
  5864. {
  5865. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5866. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5867. }
  5868. if (ctx->s_ciphers != NULL) {
  5869. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5870. WOLFSSL_SUCCESS);
  5871. }
  5872. if (ctx->s_cb.ctx_ready != NULL) {
  5873. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5874. }
  5875. /****************************
  5876. * Create WOLFSSL for client.
  5877. ****************************/
  5878. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5879. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5880. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5881. if (0
  5882. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5883. || c_sharedCtx
  5884. #endif
  5885. )
  5886. {
  5887. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5888. clientCertFile), WOLFSSL_SUCCESS);
  5889. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5890. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5891. }
  5892. if (ctx->c_cb.ssl_ready != NULL) {
  5893. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5894. }
  5895. /****************************
  5896. * Create WOLFSSL for server.
  5897. ****************************/
  5898. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5899. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5900. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5901. if (0
  5902. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5903. || s_sharedCtx
  5904. #endif
  5905. )
  5906. {
  5907. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5908. serverCertFile), WOLFSSL_SUCCESS);
  5909. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5910. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5911. }
  5912. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5913. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5914. #elif !defined(NO_DH)
  5915. /* will repick suites with DHE, higher priority than PSK */
  5916. SetDH(ctx->s_ssl);
  5917. #endif
  5918. if (ctx->s_cb.ssl_ready != NULL) {
  5919. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5920. }
  5921. return EXPECT_RESULT();
  5922. }
  5923. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5924. int* rounds)
  5925. {
  5926. int handshake_complete = 0;
  5927. int hs_c = 0;
  5928. int hs_s = 0;
  5929. int failing_s = 0;
  5930. int failing_c = 0;
  5931. int ret;
  5932. int err;
  5933. if (rounds != NULL) {
  5934. *rounds = 0;
  5935. }
  5936. while ((!handshake_complete) && (max_rounds > 0)) {
  5937. if (!hs_c) {
  5938. wolfSSL_SetLoggingPrefix("client");
  5939. ret = wolfSSL_connect(ctx->c_ssl);
  5940. wolfSSL_SetLoggingPrefix(NULL);
  5941. if (ret == WOLFSSL_SUCCESS) {
  5942. hs_c = 1;
  5943. }
  5944. else {
  5945. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5946. if (err != WOLFSSL_ERROR_WANT_READ &&
  5947. err != WOLFSSL_ERROR_WANT_WRITE) {
  5948. char buff[WOLFSSL_MAX_ERROR_SZ];
  5949. fprintf(stderr, "error = %d, %s\n", err,
  5950. wolfSSL_ERR_error_string(err, buff));
  5951. failing_c = 1;
  5952. hs_c = 1;
  5953. if (failing_c && failing_s) {
  5954. break;
  5955. }
  5956. }
  5957. }
  5958. }
  5959. if (!hs_s) {
  5960. wolfSSL_SetLoggingPrefix("server");
  5961. ret = wolfSSL_accept(ctx->s_ssl);
  5962. wolfSSL_SetLoggingPrefix(NULL);
  5963. if (ret == WOLFSSL_SUCCESS) {
  5964. hs_s = 1;
  5965. }
  5966. else {
  5967. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5968. if (err != WOLFSSL_ERROR_WANT_READ &&
  5969. err != WOLFSSL_ERROR_WANT_WRITE) {
  5970. char buff[WOLFSSL_MAX_ERROR_SZ];
  5971. fprintf(stderr, "error = %d, %s\n", err,
  5972. wolfSSL_ERR_error_string(err, buff));
  5973. failing_s = 1;
  5974. hs_s = 1;
  5975. if (failing_c && failing_s) {
  5976. break;
  5977. }
  5978. }
  5979. }
  5980. }
  5981. handshake_complete = hs_c && hs_s;
  5982. max_rounds--;
  5983. if (rounds != NULL) {
  5984. *rounds += 1;
  5985. }
  5986. }
  5987. if (!handshake_complete || failing_c || failing_s) {
  5988. return TEST_FAIL;
  5989. }
  5990. return TEST_SUCCESS;
  5991. }
  5992. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5993. {
  5994. EXPECT_DECLS;
  5995. char input[1024];
  5996. int idx = 0;
  5997. const char* msg_c = "hello wolfssl!";
  5998. int msglen_c = (int)XSTRLEN(msg_c);
  5999. const char* msg_s = "I hear you fa shizzle!";
  6000. int msglen_s = (int)XSTRLEN(msg_s);
  6001. if (ctx->c_msg != NULL) {
  6002. msg_c = ctx->c_msg;
  6003. msglen_c = ctx->c_msglen;
  6004. }
  6005. if (ctx->s_msg != NULL) {
  6006. msg_s = ctx->s_msg;
  6007. msglen_s = ctx->s_msglen;
  6008. }
  6009. wolfSSL_SetLoggingPrefix("client");
  6010. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6011. wolfSSL_SetLoggingPrefix("server");
  6012. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6013. if (idx >= 0) {
  6014. input[idx] = '\0';
  6015. }
  6016. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6017. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6018. ctx->s_cb.return_code = EXPECT_RESULT();
  6019. wolfSSL_SetLoggingPrefix("client");
  6020. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6021. wolfSSL_SetLoggingPrefix(NULL);
  6022. if (idx >= 0) {
  6023. input[idx] = '\0';
  6024. }
  6025. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6026. ctx->c_cb.return_code = EXPECT_RESULT();
  6027. if (ctx->c_cb.on_result != NULL) {
  6028. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6029. }
  6030. if (ctx->s_cb.on_result != NULL) {
  6031. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6032. }
  6033. return EXPECT_RESULT();
  6034. }
  6035. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6036. {
  6037. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6038. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6039. if (ctx->c_cb.on_cleanup != NULL) {
  6040. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6041. }
  6042. if (ctx->s_cb.on_cleanup != NULL) {
  6043. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6044. }
  6045. wolfSSL_shutdown(ctx->s_ssl);
  6046. wolfSSL_shutdown(ctx->c_ssl);
  6047. wolfSSL_free(ctx->s_ssl);
  6048. wolfSSL_free(ctx->c_ssl);
  6049. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6050. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6051. }
  6052. if (!ctx->c_cb.isSharedCtx) {
  6053. wolfSSL_CTX_free(ctx->c_ctx);
  6054. ctx->c_ctx = NULL;
  6055. }
  6056. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6057. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6058. }
  6059. if (!ctx->s_cb.isSharedCtx) {
  6060. wolfSSL_CTX_free(ctx->s_ctx);
  6061. ctx->s_ctx = NULL;
  6062. }
  6063. if (!ctx->s_cb.ticNoInit) {
  6064. #if defined(HAVE_SESSION_TICKET) && \
  6065. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6066. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6067. OpenSSLTicketCleanup();
  6068. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6069. TicketCleanup();
  6070. #endif
  6071. #endif
  6072. }
  6073. }
  6074. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6075. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6076. {
  6077. EXPECT_DECLS;
  6078. struct test_ssl_memio_ctx test_ctx;
  6079. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6080. size_t msg_len;
  6081. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6082. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6083. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6084. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6085. test_ctx.c_ctx = client_cb->ctx;
  6086. test_ctx.s_ctx = server_cb->ctx;
  6087. test_ctx.c_cb.return_code = TEST_FAIL;
  6088. test_ctx.s_cb.return_code = TEST_FAIL;
  6089. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6090. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6091. if (client_on_handshake != NULL) {
  6092. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6093. TEST_SUCCESS);
  6094. }
  6095. if (client_cb->on_handshake != NULL) {
  6096. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6097. TEST_SUCCESS);
  6098. }
  6099. if (server_cb->on_handshake != NULL) {
  6100. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6101. TEST_SUCCESS);
  6102. }
  6103. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6104. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6105. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6106. MD_MAX_SIZE);
  6107. ExpectIntGE(msg_len, 0);
  6108. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6109. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6110. MD_MAX_SIZE);
  6111. ExpectIntGE(msg_len, 0);
  6112. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6113. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6114. test_ssl_memio_cleanup(&test_ctx);
  6115. client_cb->return_code = test_ctx.c_cb.return_code;
  6116. client_cb->last_err = test_ctx.c_cb.last_err;
  6117. server_cb->return_code = test_ctx.s_cb.return_code;
  6118. server_cb->last_err = test_ctx.s_cb.last_err;
  6119. return EXPECT_RESULT();
  6120. }
  6121. #endif
  6122. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6123. #ifdef WOLFSSL_SESSION_EXPORT
  6124. #ifdef WOLFSSL_DTLS
  6125. /* set up function for sending session information */
  6126. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6127. {
  6128. WOLFSSL_CTX* ctx = NULL;
  6129. WOLFSSL* ssl = NULL;
  6130. AssertNotNull(inSsl);
  6131. AssertNotNull(buf);
  6132. AssertIntNE(0, sz);
  6133. /* Set ctx to DTLS 1.2 */
  6134. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6135. AssertNotNull(ctx);
  6136. ssl = wolfSSL_new(ctx);
  6137. AssertNotNull(ssl);
  6138. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6139. wolfSSL_free(ssl);
  6140. wolfSSL_CTX_free(ctx);
  6141. (void)userCtx;
  6142. return 0;
  6143. }
  6144. #endif
  6145. /* returns negative value on fail and positive (including 0) on success */
  6146. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6147. {
  6148. int ret, err, loop_count, count, timeout = 10;
  6149. char msg[] = "I hear you fa shizzle!";
  6150. char input[1024];
  6151. loop_count = ((func_args*)args)->argc;
  6152. #ifdef WOLFSSL_ASYNC_CRYPT
  6153. err = 0; /* Reset error */
  6154. #endif
  6155. do {
  6156. #ifdef WOLFSSL_ASYNC_CRYPT
  6157. if (err == WC_PENDING_E) {
  6158. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6159. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6160. }
  6161. #endif
  6162. ret = wolfSSL_accept(ssl);
  6163. err = wolfSSL_get_error(ssl, 0);
  6164. if (err == WOLFSSL_ERROR_WANT_READ ||
  6165. err == WOLFSSL_ERROR_WANT_WRITE) {
  6166. int select_ret;
  6167. err = WC_PENDING_E;
  6168. select_ret = tcp_select(*sockfd, timeout);
  6169. if (select_ret == TEST_TIMEOUT) {
  6170. return WOLFSSL_FATAL_ERROR;
  6171. }
  6172. }
  6173. } while (err == WC_PENDING_E);
  6174. if (ret != WOLFSSL_SUCCESS) {
  6175. char buff[WOLFSSL_MAX_ERROR_SZ];
  6176. fprintf(stderr, "error = %d, %s\n", err,
  6177. wolfSSL_ERR_error_string(err, buff));
  6178. return ret;
  6179. }
  6180. for (count = 0; count < loop_count; count++) {
  6181. int select_ret;
  6182. select_ret = tcp_select(*sockfd, timeout);
  6183. if (select_ret == TEST_TIMEOUT) {
  6184. ret = WOLFSSL_FATAL_ERROR;
  6185. break;
  6186. }
  6187. do {
  6188. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6189. if (ret > 0) {
  6190. input[ret] = '\0';
  6191. fprintf(stderr, "Client message: %s\n", input);
  6192. }
  6193. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6194. do {
  6195. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6196. return WOLFSSL_FATAL_ERROR;
  6197. }
  6198. err = wolfSSL_get_error(ssl, ret);
  6199. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6200. }
  6201. return ret;
  6202. }
  6203. #endif /* WOLFSSL_SESSION_EXPORT */
  6204. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6205. {
  6206. SOCKET_T sockfd = 0;
  6207. SOCKET_T clientfd = 0;
  6208. word16 port;
  6209. callback_functions* cbf;
  6210. WOLFSSL_CTX* ctx = NULL;
  6211. WOLFSSL* ssl = NULL;
  6212. func_args* opts = (func_args*)args;
  6213. char msg[] = "I hear you fa shizzle!";
  6214. char input[1024];
  6215. int idx;
  6216. int ret, err = 0;
  6217. int sharedCtx = 0;
  6218. int doUdp = 0;
  6219. SOCKADDR_IN_T cliAddr;
  6220. socklen_t cliLen;
  6221. const char* certFile = svrCertFile;
  6222. const char* keyFile = svrKeyFile;
  6223. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6224. size_t msg_len = 0;
  6225. #endif
  6226. wolfSSL_SetLoggingPrefix("server");
  6227. #ifdef WOLFSSL_TIRTOS
  6228. fdOpenSession(Task_self());
  6229. #endif
  6230. opts->return_code = TEST_FAIL;
  6231. cbf = opts->callbacks;
  6232. if (cbf != NULL && cbf->ctx) {
  6233. ctx = cbf->ctx;
  6234. sharedCtx = 1;
  6235. }
  6236. else
  6237. {
  6238. WOLFSSL_METHOD* method = NULL;
  6239. if (cbf != NULL && cbf->method != NULL) {
  6240. method = cbf->method();
  6241. }
  6242. else {
  6243. method = wolfSSLv23_server_method();
  6244. }
  6245. ctx = wolfSSL_CTX_new(method);
  6246. }
  6247. if (ctx == NULL) {
  6248. /* Release the wait for TCP ready. */
  6249. signal_ready(opts->signal);
  6250. goto done;
  6251. }
  6252. if (cbf == NULL || !cbf->ticNoInit) {
  6253. #if defined(HAVE_SESSION_TICKET) && \
  6254. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6255. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6256. OpenSSLTicketInit();
  6257. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6258. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6259. TicketInit();
  6260. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6261. #endif
  6262. #endif
  6263. }
  6264. #if defined(USE_WINDOWS_API)
  6265. port = opts->signal->port;
  6266. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6267. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6268. /* Let tcp_listen assign port */
  6269. port = 0;
  6270. #else
  6271. /* Use default port */
  6272. port = wolfSSLPort;
  6273. #endif
  6274. if (cbf != NULL)
  6275. doUdp = cbf->doUdp;
  6276. /* do it here to detect failure */
  6277. tcp_accept(
  6278. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6279. if (doUdp) {
  6280. cliLen = sizeof(cliAddr);
  6281. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6282. (struct sockaddr*)&cliAddr, &cliLen);
  6283. AssertIntGT(idx, 0);
  6284. }
  6285. else {
  6286. CloseSocket(sockfd);
  6287. }
  6288. wolfSSL_CTX_set_verify(ctx,
  6289. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6290. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6291. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6292. #endif
  6293. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6294. != WOLFSSL_SUCCESS) {
  6295. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6296. goto done;
  6297. }
  6298. if (cbf != NULL && cbf->certPemFile != NULL)
  6299. certFile = cbf->certPemFile;
  6300. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6301. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6302. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6303. #else
  6304. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6305. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6306. #endif
  6307. /*err_sys("can't load server cert chain file, "
  6308. "Please run from wolfSSL home dir");*/
  6309. goto done;
  6310. }
  6311. if (cbf != NULL && cbf->keyPemFile != NULL)
  6312. keyFile = cbf->keyPemFile;
  6313. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6314. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6315. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6316. #else
  6317. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6318. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6319. #endif
  6320. /*err_sys("can't load server key file, "
  6321. "Please run from wolfSSL home dir");*/
  6322. goto done;
  6323. }
  6324. #ifdef HAVE_CRL
  6325. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6326. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6327. goto done;
  6328. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6329. != WOLFSSL_SUCCESS)
  6330. goto done;
  6331. }
  6332. #endif
  6333. /* call ctx setup callback */
  6334. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6335. cbf->ctx_ready(ctx);
  6336. }
  6337. ssl = wolfSSL_new(ctx);
  6338. if (ssl == NULL) {
  6339. goto done;
  6340. }
  6341. if (doUdp) {
  6342. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6343. if (err != WOLFSSL_SUCCESS)
  6344. goto done;
  6345. }
  6346. #ifdef WOLFSSL_SESSION_EXPORT
  6347. /* only add in more complex nonblocking case with session export tests */
  6348. if (args && opts->argc > 0) {
  6349. /* set as nonblock and time out for waiting on read/write */
  6350. tcp_set_nonblocking(&clientfd);
  6351. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6352. }
  6353. #endif
  6354. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6355. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6356. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6357. #else
  6358. if (wolfSSL_use_certificate_file(ssl, certFile,
  6359. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6360. #endif
  6361. /*err_sys("can't load server cert chain file, "
  6362. "Please run from wolfSSL home dir");*/
  6363. goto done;
  6364. }
  6365. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6366. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6367. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6368. #else
  6369. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6370. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6371. #endif
  6372. /*err_sys("can't load server key file, "
  6373. "Please run from wolfSSL home dir");*/
  6374. goto done;
  6375. }
  6376. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6377. /*err_sys("SSL_set_fd failed");*/
  6378. goto done;
  6379. }
  6380. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6381. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6382. #elif !defined(NO_DH)
  6383. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6384. #endif
  6385. /* call ssl setup callback */
  6386. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6387. cbf->ssl_ready(ssl);
  6388. }
  6389. #ifdef WOLFSSL_SESSION_EXPORT
  6390. /* only add in more complex nonblocking case with session export tests */
  6391. if (opts->argc > 0) {
  6392. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6393. if (ret >= 0) {
  6394. opts->return_code = TEST_SUCCESS;
  6395. }
  6396. #ifdef WOLFSSL_TIRTOS
  6397. Task_yield();
  6398. #endif
  6399. goto done;
  6400. }
  6401. #endif
  6402. #ifdef WOLFSSL_ASYNC_CRYPT
  6403. err = 0; /* Reset error */
  6404. #endif
  6405. do {
  6406. #ifdef WOLFSSL_ASYNC_CRYPT
  6407. if (err == WC_PENDING_E) {
  6408. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6409. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6410. }
  6411. #endif
  6412. ret = wolfSSL_negotiate(ssl);
  6413. err = wolfSSL_get_error(ssl, 0);
  6414. } while (err == WC_PENDING_E);
  6415. if (ret != WOLFSSL_SUCCESS) {
  6416. char buff[WOLFSSL_MAX_ERROR_SZ];
  6417. fprintf(stderr, "error = %d, %s\n", err,
  6418. wolfSSL_ERR_error_string(err, buff));
  6419. /*err_sys("SSL_accept failed");*/
  6420. goto done;
  6421. }
  6422. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6423. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6424. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6425. AssertIntGE(msg_len, 0);
  6426. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6427. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6428. AssertIntGE(msg_len, 0);
  6429. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6430. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6431. if (idx > 0) {
  6432. input[idx] = '\0';
  6433. fprintf(stderr, "Client message: %s\n", input);
  6434. }
  6435. else if (idx < 0) {
  6436. goto done;
  6437. }
  6438. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6439. /*err_sys("SSL_write failed");*/
  6440. goto done;
  6441. }
  6442. if (cbf != NULL && cbf->on_result != NULL)
  6443. cbf->on_result(ssl);
  6444. #ifdef WOLFSSL_TIRTOS
  6445. Task_yield();
  6446. #endif
  6447. opts->return_code = TEST_SUCCESS;
  6448. done:
  6449. if (cbf != NULL)
  6450. cbf->last_err = err;
  6451. if (cbf != NULL && cbf->on_cleanup != NULL)
  6452. cbf->on_cleanup(ssl);
  6453. wolfSSL_shutdown(ssl);
  6454. wolfSSL_free(ssl);
  6455. if (!sharedCtx)
  6456. wolfSSL_CTX_free(ctx);
  6457. CloseSocket(clientfd);
  6458. #ifdef WOLFSSL_TIRTOS
  6459. fdCloseSession(Task_self());
  6460. #endif
  6461. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6462. && defined(HAVE_THREAD_LS)
  6463. wc_ecc_fp_free(); /* free per thread cache */
  6464. #endif
  6465. if (cbf == NULL || !cbf->ticNoInit) {
  6466. #if defined(HAVE_SESSION_TICKET) && \
  6467. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6468. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6469. OpenSSLTicketCleanup();
  6470. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6471. TicketCleanup();
  6472. #endif
  6473. #endif
  6474. }
  6475. wolfSSL_SetLoggingPrefix(NULL);
  6476. WOLFSSL_RETURN_FROM_THREAD(0);
  6477. }
  6478. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6479. !defined(WOLFSSL_NO_TLS12)
  6480. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6481. {
  6482. SOCKET_T sockfd;
  6483. SOCKET_T clientfd = -1;
  6484. word16 port;
  6485. callback_functions* cbf;
  6486. WOLFSSL_CTX* ctx = 0;
  6487. WOLFSSL* ssl = 0;
  6488. char msg[] = "I hear you fa shizzle!";
  6489. char input[1024];
  6490. int idx;
  6491. int ret, err = 0;
  6492. int sharedCtx = 0;
  6493. func_args* opts = (func_args*)args;
  6494. int loop_count = opts->argc;
  6495. int count = 0;
  6496. #ifdef WOLFSSL_TIRTOS
  6497. fdOpenSession(Task_self());
  6498. #endif
  6499. opts->return_code = TEST_FAIL;
  6500. cbf = opts->callbacks;
  6501. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6502. if (cbf != NULL && cbf->ctx) {
  6503. ctx = cbf->ctx;
  6504. sharedCtx = 1;
  6505. }
  6506. else
  6507. #endif
  6508. {
  6509. WOLFSSL_METHOD* method = NULL;
  6510. if (cbf != NULL && cbf->method != NULL) {
  6511. method = cbf->method();
  6512. }
  6513. else {
  6514. method = wolfSSLv23_server_method();
  6515. }
  6516. ctx = wolfSSL_CTX_new(method);
  6517. }
  6518. #if defined(USE_WINDOWS_API)
  6519. port = opts->signal->port;
  6520. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6521. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6522. /* Let tcp_listen assign port */
  6523. port = 0;
  6524. #else
  6525. /* Use default port */
  6526. port = wolfSSLPort;
  6527. #endif
  6528. wolfSSL_CTX_set_verify(ctx,
  6529. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6530. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6531. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6532. #endif
  6533. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6534. != WOLFSSL_SUCCESS) {
  6535. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6536. /* Release the wait for TCP ready. */
  6537. signal_ready(opts->signal);
  6538. goto done;
  6539. }
  6540. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6541. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6542. /*err_sys("can't load server cert chain file, "
  6543. "Please run from wolfSSL home dir");*/
  6544. /* Release the wait for TCP ready. */
  6545. signal_ready(opts->signal);
  6546. goto done;
  6547. }
  6548. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6549. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6550. /*err_sys("can't load server key file, "
  6551. "Please run from wolfSSL home dir");*/
  6552. /* Release the wait for TCP ready. */
  6553. signal_ready(opts->signal);
  6554. goto done;
  6555. }
  6556. /* call ctx setup callback */
  6557. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6558. cbf->ctx_ready(ctx);
  6559. }
  6560. while (count != loop_count) {
  6561. ssl = wolfSSL_new(ctx);
  6562. if (ssl == NULL) {
  6563. signal_ready(opts->signal);
  6564. goto done;
  6565. }
  6566. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6567. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6568. /*err_sys("can't load server cert chain file, "
  6569. "Please run from wolfSSL home dir");*/
  6570. /* Release the wait for TCP ready. */
  6571. signal_ready(opts->signal);
  6572. goto done;
  6573. }
  6574. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6575. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6576. /*err_sys("can't load server key file, "
  6577. "Please run from wolfSSL home dir");*/
  6578. /* Release the wait for TCP ready. */
  6579. signal_ready(opts->signal);
  6580. goto done;
  6581. }
  6582. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6583. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6584. #elif !defined(NO_DH)
  6585. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6586. #endif
  6587. /* call ssl setup callback */
  6588. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6589. cbf->ssl_ready(ssl);
  6590. }
  6591. /* do it here to detect failure */
  6592. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6593. 0);
  6594. CloseSocket(sockfd);
  6595. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6596. /*err_sys("SSL_set_fd failed");*/
  6597. goto done;
  6598. }
  6599. #ifdef WOLFSSL_ASYNC_CRYPT
  6600. err = 0; /* Reset error */
  6601. #endif
  6602. do {
  6603. #ifdef WOLFSSL_ASYNC_CRYPT
  6604. if (err == WC_PENDING_E) {
  6605. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6606. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6607. }
  6608. #endif
  6609. ret = wolfSSL_accept(ssl);
  6610. err = wolfSSL_get_error(ssl, 0);
  6611. } while (err == WC_PENDING_E);
  6612. if (ret != WOLFSSL_SUCCESS) {
  6613. char buff[WOLFSSL_MAX_ERROR_SZ];
  6614. fprintf(stderr, "error = %d, %s\n", err,
  6615. wolfSSL_ERR_error_string(err, buff));
  6616. /*err_sys("SSL_accept failed");*/
  6617. goto done;
  6618. }
  6619. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6620. if (idx > 0) {
  6621. input[idx] = '\0';
  6622. fprintf(stderr, "Client message: %s\n", input);
  6623. }
  6624. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6625. /*err_sys("SSL_write failed");*/
  6626. goto done;
  6627. }
  6628. /* free ssl for this connection */
  6629. wolfSSL_shutdown(ssl);
  6630. wolfSSL_free(ssl); ssl = NULL;
  6631. CloseSocket(clientfd);
  6632. clientfd = -1;
  6633. count++;
  6634. }
  6635. #ifdef WOLFSSL_TIRTOS
  6636. Task_yield();
  6637. #endif
  6638. opts->return_code = TEST_SUCCESS;
  6639. done:
  6640. if (ssl != NULL) {
  6641. wolfSSL_shutdown(ssl);
  6642. wolfSSL_free(ssl);
  6643. }
  6644. if (!sharedCtx)
  6645. wolfSSL_CTX_free(ctx);
  6646. if (clientfd >= 0)
  6647. CloseSocket(clientfd);
  6648. #ifdef WOLFSSL_TIRTOS
  6649. fdCloseSession(Task_self());
  6650. #endif
  6651. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6652. && defined(HAVE_THREAD_LS)
  6653. wc_ecc_fp_free(); /* free per thread cache */
  6654. #endif
  6655. WOLFSSL_RETURN_FROM_THREAD(0);
  6656. }
  6657. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6658. static int test_client_nofail(void* args, cbType cb)
  6659. {
  6660. #if !defined(NO_WOLFSSL_CLIENT)
  6661. SOCKET_T sockfd = 0;
  6662. callback_functions* cbf;
  6663. WOLFSSL_CTX* ctx = 0;
  6664. WOLFSSL* ssl = 0;
  6665. WOLFSSL_CIPHER* cipher;
  6666. char msg[64] = "hello wolfssl!";
  6667. char reply[1024];
  6668. int input;
  6669. int msgSz = (int)XSTRLEN(msg);
  6670. int ret, err = 0;
  6671. int cipherSuite;
  6672. int sharedCtx = 0;
  6673. int doUdp = 0;
  6674. const char* cipherName1, *cipherName2;
  6675. wolfSSL_SetLoggingPrefix("client");
  6676. #ifdef WOLFSSL_TIRTOS
  6677. fdOpenSession(Task_self());
  6678. #endif
  6679. ((func_args*)args)->return_code = TEST_FAIL;
  6680. cbf = ((func_args*)args)->callbacks;
  6681. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6682. if (cbf != NULL && cbf->ctx) {
  6683. ctx = cbf->ctx;
  6684. sharedCtx = cbf->isSharedCtx;
  6685. }
  6686. else
  6687. #endif
  6688. {
  6689. WOLFSSL_METHOD* method = NULL;
  6690. if (cbf != NULL && cbf->method != NULL) {
  6691. method = cbf->method();
  6692. }
  6693. else {
  6694. method = wolfSSLv23_client_method();
  6695. }
  6696. ctx = wolfSSL_CTX_new(method);
  6697. }
  6698. if (cbf != NULL)
  6699. doUdp = cbf->doUdp;
  6700. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6701. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6702. #endif
  6703. /* Do connect here so server detects failures */
  6704. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6705. doUdp, 0, NULL);
  6706. /* Connect the socket so that we don't have to set the peer later on */
  6707. if (doUdp)
  6708. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6709. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6710. {
  6711. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6712. goto done;
  6713. }
  6714. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6715. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6716. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6717. #else
  6718. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6719. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6720. #endif
  6721. /*err_sys("can't load client cert file, "
  6722. "Please run from wolfSSL home dir");*/
  6723. goto done;
  6724. }
  6725. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6726. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6727. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6728. #else
  6729. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6730. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6731. #endif
  6732. /*err_sys("can't load client key file, "
  6733. "Please run from wolfSSL home dir");*/
  6734. goto done;
  6735. }
  6736. #ifdef HAVE_CRL
  6737. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6738. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6739. goto done;
  6740. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6741. != WOLFSSL_SUCCESS)
  6742. goto done;
  6743. }
  6744. #endif
  6745. /* call ctx setup callback */
  6746. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6747. cbf->ctx_ready(ctx);
  6748. }
  6749. ssl = wolfSSL_new(ctx);
  6750. if (ssl == NULL) {
  6751. goto done;
  6752. }
  6753. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6754. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6755. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6756. #else
  6757. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6758. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6759. #endif
  6760. /*err_sys("can't load client cert file, "
  6761. "Please run from wolfSSL home dir");*/
  6762. goto done;
  6763. }
  6764. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6765. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6766. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6767. #else
  6768. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6769. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6770. #endif
  6771. /*err_sys("can't load client key file, "
  6772. "Please run from wolfSSL home dir");*/
  6773. goto done;
  6774. }
  6775. if (!doUdp) {
  6776. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6777. /*err_sys("SSL_set_fd failed");*/
  6778. goto done;
  6779. }
  6780. }
  6781. else {
  6782. #ifdef WOLFSSL_DTLS
  6783. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6784. /*err_sys("SSL_set_fd failed");*/
  6785. goto done;
  6786. }
  6787. #else
  6788. goto done;
  6789. #endif
  6790. }
  6791. /* call ssl setup callback */
  6792. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6793. cbf->ssl_ready(ssl);
  6794. }
  6795. #ifdef WOLFSSL_ASYNC_CRYPT
  6796. err = 0; /* Reset error */
  6797. #endif
  6798. do {
  6799. #ifdef WOLFSSL_ASYNC_CRYPT
  6800. if (err == WC_PENDING_E) {
  6801. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6802. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6803. }
  6804. #endif
  6805. ret = wolfSSL_negotiate(ssl);
  6806. err = wolfSSL_get_error(ssl, 0);
  6807. } while (err == WC_PENDING_E);
  6808. if (ret != WOLFSSL_SUCCESS) {
  6809. char buff[WOLFSSL_MAX_ERROR_SZ];
  6810. fprintf(stderr, "error = %d, %s\n", err,
  6811. wolfSSL_ERR_error_string(err, buff));
  6812. /*err_sys("SSL_connect failed");*/
  6813. goto done;
  6814. }
  6815. /* test the various get cipher methods */
  6816. /* Internal cipher suite names */
  6817. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6818. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6819. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6820. (cipherSuite >> 8), cipherSuite & 0xFF);
  6821. AssertStrEQ(cipherName1, cipherName2);
  6822. /* IANA Cipher Suites Names */
  6823. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6824. then it's the internal cipher suite name */
  6825. cipher = wolfSSL_get_current_cipher(ssl);
  6826. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6827. cipherName2 = wolfSSL_get_cipher(ssl);
  6828. AssertStrEQ(cipherName1, cipherName2);
  6829. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6830. !defined(WOLFSSL_QT)
  6831. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6832. (cipherSuite >> 8), cipherSuite & 0xFF);
  6833. AssertStrEQ(cipherName1, cipherName2);
  6834. #endif
  6835. if (cb != NULL)
  6836. (cb)(ctx, ssl);
  6837. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6838. /*err_sys("SSL_write failed");*/
  6839. goto done;
  6840. }
  6841. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6842. if (input > 0) {
  6843. reply[input] = '\0';
  6844. fprintf(stderr, "Server response: %s\n", reply);
  6845. }
  6846. if (cbf != NULL && cbf->on_result != NULL)
  6847. cbf->on_result(ssl);
  6848. ((func_args*)args)->return_code = TEST_SUCCESS;
  6849. done:
  6850. if (cbf != NULL)
  6851. cbf->last_err = err;
  6852. if (cbf != NULL && cbf->on_cleanup != NULL)
  6853. cbf->on_cleanup(ssl);
  6854. wolfSSL_free(ssl);
  6855. if (!sharedCtx)
  6856. wolfSSL_CTX_free(ctx);
  6857. CloseSocket(sockfd);
  6858. #ifdef WOLFSSL_TIRTOS
  6859. fdCloseSession(Task_self());
  6860. #endif
  6861. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6862. && defined(HAVE_THREAD_LS)
  6863. wc_ecc_fp_free(); /* free per thread cache */
  6864. #endif
  6865. #else
  6866. (void)args;
  6867. (void)cb;
  6868. #endif /* !NO_WOLFSSL_CLIENT */
  6869. wolfSSL_SetLoggingPrefix(NULL);
  6870. return 0;
  6871. }
  6872. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6873. callback_functions* server_cb, cbType client_on_handshake)
  6874. {
  6875. func_args client_args;
  6876. func_args server_args;
  6877. tcp_ready ready;
  6878. THREAD_TYPE serverThread;
  6879. XMEMSET(&client_args, 0, sizeof(func_args));
  6880. XMEMSET(&server_args, 0, sizeof(func_args));
  6881. #ifdef WOLFSSL_TIRTOS
  6882. fdOpenSession(Task_self());
  6883. #endif
  6884. StartTCP();
  6885. InitTcpReady(&ready);
  6886. #if defined(USE_WINDOWS_API)
  6887. /* use RNG to get random port if using windows */
  6888. ready.port = GetRandomPort();
  6889. #endif
  6890. server_args.signal = &ready;
  6891. server_args.callbacks = server_cb;
  6892. client_args.signal = &ready;
  6893. client_args.callbacks = client_cb;
  6894. start_thread(test_server_nofail, &server_args, &serverThread);
  6895. wait_tcp_ready(&server_args);
  6896. test_client_nofail(&client_args, client_on_handshake);
  6897. join_thread(serverThread);
  6898. client_cb->return_code = client_args.return_code;
  6899. server_cb->return_code = server_args.return_code;
  6900. FreeTcpReady(&ready);
  6901. #ifdef WOLFSSL_TIRTOS
  6902. fdOpenSession(Task_self());
  6903. #endif
  6904. }
  6905. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6906. callback_functions* server_cb)
  6907. {
  6908. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6909. }
  6910. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6911. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6912. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6913. void* server_args)
  6914. {
  6915. SOCKET_T sockfd = 0;
  6916. callback_functions* cbf;
  6917. WOLFSSL_CTX* ctx = 0;
  6918. WOLFSSL* ssl = 0;
  6919. WOLFSSL_SESSION* session = NULL;
  6920. char msg[64] = "hello wolfssl!";
  6921. char reply[1024];
  6922. int input;
  6923. int msgSz = (int)XSTRLEN(msg);
  6924. int ret, err = 0;
  6925. int sharedCtx = 0;
  6926. #ifdef WOLFSSL_TIRTOS
  6927. fdOpenSession(Task_self());
  6928. #endif
  6929. ((func_args*)args)->return_code = TEST_FAIL;
  6930. cbf = ((func_args*)args)->callbacks;
  6931. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6932. if (cbf != NULL && cbf->ctx) {
  6933. ctx = cbf->ctx;
  6934. sharedCtx = 1;
  6935. }
  6936. else
  6937. #endif
  6938. {
  6939. WOLFSSL_METHOD* method = NULL;
  6940. if (cbf != NULL && cbf->method != NULL) {
  6941. method = cbf->method();
  6942. }
  6943. else {
  6944. method = wolfSSLv23_client_method();
  6945. }
  6946. ctx = wolfSSL_CTX_new(method);
  6947. }
  6948. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6949. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6950. #endif
  6951. /* Do connect here so server detects failures */
  6952. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6953. 0, 0, NULL);
  6954. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6955. WOLFSSL_SUCCESS) {
  6956. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6957. goto done;
  6958. }
  6959. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6960. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6961. /*err_sys("can't load client cert file, "
  6962. "Please run from wolfSSL home dir");*/
  6963. goto done;
  6964. }
  6965. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6966. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6967. /*err_sys("can't load client key file, "
  6968. "Please run from wolfSSL home dir");*/
  6969. goto done;
  6970. }
  6971. /* call ctx setup callback */
  6972. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6973. cbf->ctx_ready(ctx);
  6974. }
  6975. ssl = wolfSSL_new(ctx);
  6976. if (ssl == NULL) {
  6977. goto done;
  6978. }
  6979. /* keep handshake resources for re-using WOLFSSL obj */
  6980. wolfSSL_KeepArrays(ssl);
  6981. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6982. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6983. goto done;
  6984. }
  6985. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6986. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6987. /*err_sys("can't load client cert file, "
  6988. "Please run from wolfSSL home dir");*/
  6989. goto done;
  6990. }
  6991. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6992. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6993. /*err_sys("can't load client key file, "
  6994. "Please run from wolfSSL home dir");*/
  6995. goto done;
  6996. }
  6997. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6998. /*err_sys("SSL_set_fd failed");*/
  6999. goto done;
  7000. }
  7001. /* call ssl setup callback */
  7002. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7003. cbf->ssl_ready(ssl);
  7004. }
  7005. #ifdef WOLFSSL_ASYNC_CRYPT
  7006. err = 0; /* Reset error */
  7007. #endif
  7008. do {
  7009. #ifdef WOLFSSL_ASYNC_CRYPT
  7010. if (err == WC_PENDING_E) {
  7011. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7012. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7013. }
  7014. #endif
  7015. ret = wolfSSL_connect(ssl);
  7016. err = wolfSSL_get_error(ssl, 0);
  7017. } while (err == WC_PENDING_E);
  7018. if (ret != WOLFSSL_SUCCESS) {
  7019. char buff[WOLFSSL_MAX_ERROR_SZ];
  7020. fprintf(stderr, "error = %d, %s\n", err,
  7021. wolfSSL_ERR_error_string(err, buff));
  7022. /*err_sys("SSL_connect failed");*/
  7023. goto done;
  7024. }
  7025. /* Build first session */
  7026. if (cb != NULL)
  7027. cb(ctx, ssl);
  7028. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7029. /*err_sys("SSL_write failed");*/
  7030. goto done;
  7031. }
  7032. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7033. if (input > 0) {
  7034. reply[input] = '\0';
  7035. fprintf(stderr, "Server response: %s\n", reply);
  7036. }
  7037. /* Session Resumption by re-using WOLFSSL object */
  7038. wolfSSL_set_quiet_shutdown(ssl, 1);
  7039. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7040. /* err_sys ("SSL shutdown failed"); */
  7041. goto done;
  7042. }
  7043. session = wolfSSL_get1_session(ssl);
  7044. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7045. wolfSSL_SESSION_free(session);
  7046. /* err_sys ("SSL_clear failed"); */
  7047. goto done;
  7048. }
  7049. wolfSSL_set_session(ssl, session);
  7050. wolfSSL_SESSION_free(session);
  7051. session = NULL;
  7052. /* close socket once */
  7053. CloseSocket(sockfd);
  7054. sockfd = 0;
  7055. /* wait until server ready */
  7056. wait_tcp_ready((func_args*)server_args);
  7057. fprintf(stderr, "session resumption\n");
  7058. /* Do re-connect */
  7059. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7060. 0, 0, NULL);
  7061. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7062. /*err_sys("SSL_set_fd failed");*/
  7063. goto done;
  7064. }
  7065. #ifdef WOLFSSL_ASYNC_CRYPT
  7066. err = 0; /* Reset error */
  7067. #endif
  7068. do {
  7069. #ifdef WOLFSSL_ASYNC_CRYPT
  7070. if (err == WC_PENDING_E) {
  7071. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7072. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7073. }
  7074. #endif
  7075. ret = wolfSSL_connect(ssl);
  7076. err = wolfSSL_get_error(ssl, 0);
  7077. } while (err == WC_PENDING_E);
  7078. if (ret != WOLFSSL_SUCCESS) {
  7079. char buff[WOLFSSL_MAX_ERROR_SZ];
  7080. fprintf(stderr, "error = %d, %s\n", err,
  7081. wolfSSL_ERR_error_string(err, buff));
  7082. /*err_sys("SSL_connect failed");*/
  7083. goto done;
  7084. }
  7085. /* Build first session */
  7086. if (cb != NULL)
  7087. cb(ctx, ssl);
  7088. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7089. /*err_sys("SSL_write failed");*/
  7090. goto done;
  7091. }
  7092. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7093. if (input > 0) {
  7094. reply[input] = '\0';
  7095. fprintf(stderr, "Server response: %s\n", reply);
  7096. }
  7097. ((func_args*)args)->return_code = TEST_SUCCESS;
  7098. done:
  7099. wolfSSL_free(ssl);
  7100. if (!sharedCtx)
  7101. wolfSSL_CTX_free(ctx);
  7102. CloseSocket(sockfd);
  7103. #ifdef WOLFSSL_TIRTOS
  7104. fdCloseSession(Task_self());
  7105. #endif
  7106. return;
  7107. }
  7108. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7109. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7110. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7111. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7112. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7113. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7114. #define HAVE_ALPN_PROTOS_SUPPORT
  7115. #endif
  7116. /* Generic TLS client / server with callbacks for API unit tests
  7117. * Used by SNI / ALPN / crypto callback helper functions */
  7118. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7119. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7120. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7121. #define ENABLE_TLS_CALLBACK_TEST
  7122. #endif
  7123. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7124. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7125. /* TLS server for API unit testing - generic */
  7126. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7127. {
  7128. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7129. WOLFSSL_CTX* ctx = NULL;
  7130. WOLFSSL* ssl = NULL;
  7131. SOCKET_T sfd = 0;
  7132. SOCKET_T cfd = 0;
  7133. word16 port;
  7134. char msg[] = "I hear you fa shizzle!";
  7135. int len = (int) XSTRLEN(msg);
  7136. char input[1024];
  7137. int idx;
  7138. int ret, err = 0;
  7139. ((func_args*)args)->return_code = TEST_FAIL;
  7140. #if defined(USE_WINDOWS_API)
  7141. port = ((func_args*)args)->signal->port;
  7142. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7143. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7144. /* Let tcp_listen assign port */
  7145. port = 0;
  7146. #else
  7147. /* Use default port */
  7148. port = wolfSSLPort;
  7149. #endif
  7150. #ifdef WOLFSSL_DTLS
  7151. if (callbacks->method == wolfDTLS_server_method
  7152. #ifdef WOLFSSL_STATIC_MEMORY
  7153. || callbacks->method_ex == wolfDTLS_server_method_ex
  7154. #endif
  7155. #ifndef NO_OLD_TLS
  7156. || callbacks->method == wolfDTLSv1_server_method
  7157. #ifdef WOLFSSL_STATIC_MEMORY
  7158. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7159. #endif
  7160. #endif
  7161. #ifndef WOLFSSL_NO_TLS12
  7162. || callbacks->method == wolfDTLSv1_2_server_method
  7163. #ifdef WOLFSSL_STATIC_MEMORY
  7164. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7165. #endif
  7166. #endif
  7167. #ifdef WOLFSSL_DTLS13
  7168. || callbacks->method == wolfDTLSv1_3_server_method
  7169. #ifdef WOLFSSL_STATIC_MEMORY
  7170. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7171. #endif
  7172. #endif
  7173. ) {
  7174. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7175. }
  7176. else
  7177. #endif
  7178. {
  7179. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7180. }
  7181. #ifdef WOLFSSL_STATIC_MEMORY
  7182. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7183. callbacks->memSz > 0) {
  7184. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7185. callbacks->mem, callbacks->memSz, 0, 1);
  7186. if (ret != WOLFSSL_SUCCESS) {
  7187. fprintf(stderr, "CTX static new failed %d\n", ret);
  7188. goto cleanup;
  7189. }
  7190. }
  7191. #else
  7192. ctx = wolfSSL_CTX_new(callbacks->method());
  7193. #endif
  7194. if (ctx == NULL) {
  7195. fprintf(stderr, "CTX new failed\n");
  7196. goto cleanup;
  7197. }
  7198. /* set defaults */
  7199. if (callbacks->caPemFile == NULL)
  7200. callbacks->caPemFile = cliCertFile;
  7201. if (callbacks->certPemFile == NULL)
  7202. callbacks->certPemFile = svrCertFile;
  7203. if (callbacks->keyPemFile == NULL)
  7204. callbacks->keyPemFile = svrKeyFile;
  7205. #ifdef WOLFSSL_TIRTOS
  7206. fdOpenSession(Task_self());
  7207. #endif
  7208. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7209. wolfSSL_CTX_set_verify(ctx,
  7210. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7211. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7212. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7213. #endif
  7214. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7215. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7216. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7217. goto cleanup;
  7218. }
  7219. #endif
  7220. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7221. WOLFSSL_SUCCESS) {
  7222. goto cleanup;
  7223. }
  7224. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7225. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7226. goto cleanup;
  7227. }
  7228. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7229. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7230. goto cleanup;
  7231. }
  7232. #ifdef HAVE_CRL
  7233. if (callbacks->crlPemFile != NULL) {
  7234. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7235. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7236. goto cleanup;
  7237. }
  7238. }
  7239. #endif
  7240. if (callbacks->ctx_ready)
  7241. callbacks->ctx_ready(ctx);
  7242. ssl = wolfSSL_new(ctx);
  7243. if (ssl == NULL) {
  7244. fprintf(stderr, "SSL new failed\n");
  7245. goto cleanup;
  7246. }
  7247. if (wolfSSL_dtls(ssl)) {
  7248. SOCKADDR_IN_T cliAddr;
  7249. socklen_t cliLen;
  7250. cliLen = sizeof(cliAddr);
  7251. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7252. (struct sockaddr*)&cliAddr, &cliLen);
  7253. if (idx <= 0) {
  7254. goto cleanup;
  7255. }
  7256. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7257. }
  7258. else {
  7259. CloseSocket(sfd);
  7260. }
  7261. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7262. goto cleanup;
  7263. }
  7264. if (callbacks->loadToSSL) {
  7265. wolfSSL_SetDevId(ssl, callbacks->devId);
  7266. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7267. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7268. goto cleanup;
  7269. }
  7270. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7271. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7272. goto cleanup;
  7273. }
  7274. }
  7275. #ifdef NO_PSK
  7276. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7277. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7278. #elif !defined(NO_DH)
  7279. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7280. #endif
  7281. #endif
  7282. if (callbacks->ssl_ready)
  7283. callbacks->ssl_ready(ssl);
  7284. #ifdef WOLFSSL_ASYNC_CRYPT
  7285. err = 0; /* Reset error */
  7286. #endif
  7287. do {
  7288. #ifdef WOLFSSL_ASYNC_CRYPT
  7289. if (err == WC_PENDING_E) {
  7290. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7291. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7292. }
  7293. #endif
  7294. ret = wolfSSL_accept(ssl);
  7295. err = wolfSSL_get_error(ssl, ret);
  7296. } while (err == WC_PENDING_E);
  7297. if (ret != WOLFSSL_SUCCESS) {
  7298. char buff[WOLFSSL_MAX_ERROR_SZ];
  7299. fprintf(stderr, "accept error = %d, %s\n", err,
  7300. wolfSSL_ERR_error_string(err, buff));
  7301. /*err_sys("SSL_accept failed");*/
  7302. }
  7303. else {
  7304. #ifdef WOLFSSL_ASYNC_CRYPT
  7305. err = 0; /* Reset error */
  7306. #endif
  7307. do {
  7308. #ifdef WOLFSSL_ASYNC_CRYPT
  7309. if (err == WC_PENDING_E) {
  7310. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7311. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7312. }
  7313. #endif
  7314. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7315. err = wolfSSL_get_error(ssl, idx);
  7316. } while (err == WC_PENDING_E);
  7317. if (idx > 0) {
  7318. input[idx] = 0;
  7319. fprintf(stderr, "Client message: %s\n", input);
  7320. }
  7321. #ifdef WOLFSSL_ASYNC_CRYPT
  7322. err = 0; /* Reset error */
  7323. #endif
  7324. do {
  7325. #ifdef WOLFSSL_ASYNC_CRYPT
  7326. if (err == WC_PENDING_E) {
  7327. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7328. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7329. }
  7330. #endif
  7331. ret = wolfSSL_write(ssl, msg, len);
  7332. err = wolfSSL_get_error(ssl, ret);
  7333. } while (err == WC_PENDING_E);
  7334. if (len != ret) {
  7335. goto cleanup;
  7336. }
  7337. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7338. defined(WOLFSSL_DTLS)
  7339. if (wolfSSL_dtls(ssl)) {
  7340. byte* import;
  7341. word32 sz;
  7342. wolfSSL_dtls_export(ssl, NULL, &sz);
  7343. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7344. if (import == NULL) {
  7345. goto cleanup;
  7346. }
  7347. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7348. if (idx < 0) {
  7349. goto cleanup;
  7350. }
  7351. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7352. goto cleanup;
  7353. }
  7354. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7355. }
  7356. #endif
  7357. #ifdef WOLFSSL_TIRTOS
  7358. Task_yield();
  7359. #endif
  7360. ((func_args*)args)->return_code = TEST_SUCCESS;
  7361. }
  7362. if (callbacks->on_result)
  7363. callbacks->on_result(ssl);
  7364. wolfSSL_shutdown(ssl);
  7365. cleanup:
  7366. wolfSSL_free(ssl);
  7367. wolfSSL_CTX_free(ctx);
  7368. CloseSocket(cfd);
  7369. #ifdef WOLFSSL_TIRTOS
  7370. fdCloseSession(Task_self());
  7371. #endif
  7372. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7373. && defined(HAVE_THREAD_LS)
  7374. wc_ecc_fp_free(); /* free per thread cache */
  7375. #endif
  7376. WOLFSSL_RETURN_FROM_THREAD(0);
  7377. }
  7378. /* TLS Client for API unit testing - generic */
  7379. static void run_wolfssl_client(void* args)
  7380. {
  7381. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7382. WOLFSSL_CTX* ctx = NULL;
  7383. WOLFSSL* ssl = NULL;
  7384. SOCKET_T sfd = 0;
  7385. char msg[] = "hello wolfssl server!";
  7386. int len = (int) XSTRLEN(msg);
  7387. char input[1024];
  7388. int ret, err = 0;
  7389. ((func_args*)args)->return_code = TEST_FAIL;
  7390. /* set defaults */
  7391. if (callbacks->caPemFile == NULL)
  7392. callbacks->caPemFile = caCertFile;
  7393. if (callbacks->certPemFile == NULL)
  7394. callbacks->certPemFile = cliCertFile;
  7395. if (callbacks->keyPemFile == NULL)
  7396. callbacks->keyPemFile = cliKeyFile;
  7397. #ifdef WOLFSSL_STATIC_MEMORY
  7398. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7399. callbacks->memSz > 0) {
  7400. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7401. callbacks->mem, callbacks->memSz, 0, 1);
  7402. if (ret != WOLFSSL_SUCCESS) {
  7403. fprintf(stderr, "CTX static new failed %d\n", ret);
  7404. goto cleanup;
  7405. }
  7406. }
  7407. #else
  7408. ctx = wolfSSL_CTX_new(callbacks->method());
  7409. #endif
  7410. if (ctx == NULL) {
  7411. fprintf(stderr, "CTX new failed\n");
  7412. goto cleanup;
  7413. }
  7414. #ifdef WOLFSSL_TIRTOS
  7415. fdOpenSession(Task_self());
  7416. #endif
  7417. if (!callbacks->loadToSSL) {
  7418. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7419. }
  7420. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7421. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7422. #endif
  7423. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7424. WOLFSSL_SUCCESS) {
  7425. goto cleanup;
  7426. }
  7427. if (!callbacks->loadToSSL) {
  7428. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7429. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7430. goto cleanup;
  7431. }
  7432. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7433. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7434. goto cleanup;
  7435. }
  7436. }
  7437. #ifdef HAVE_CRL
  7438. if (callbacks->crlPemFile != NULL) {
  7439. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7440. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7441. goto cleanup;
  7442. }
  7443. }
  7444. #endif
  7445. if (callbacks->ctx_ready)
  7446. callbacks->ctx_ready(ctx);
  7447. ssl = wolfSSL_new(ctx);
  7448. if (wolfSSL_dtls(ssl)) {
  7449. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7450. 1, 0, ssl);
  7451. }
  7452. else {
  7453. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7454. 0, 0, ssl);
  7455. }
  7456. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7457. goto cleanup;
  7458. }
  7459. if (callbacks->loadToSSL) {
  7460. wolfSSL_SetDevId(ssl, callbacks->devId);
  7461. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7462. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7463. goto cleanup;
  7464. }
  7465. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7466. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7467. goto cleanup;
  7468. }
  7469. }
  7470. if (callbacks->ssl_ready)
  7471. callbacks->ssl_ready(ssl);
  7472. #ifdef WOLFSSL_ASYNC_CRYPT
  7473. err = 0; /* Reset error */
  7474. #endif
  7475. do {
  7476. #ifdef WOLFSSL_ASYNC_CRYPT
  7477. if (err == WC_PENDING_E) {
  7478. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7479. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7480. }
  7481. #endif
  7482. ret = wolfSSL_connect(ssl);
  7483. err = wolfSSL_get_error(ssl, ret);
  7484. } while (err == WC_PENDING_E);
  7485. if (ret != WOLFSSL_SUCCESS) {
  7486. char buff[WOLFSSL_MAX_ERROR_SZ];
  7487. fprintf(stderr, "error = %d, %s\n", err,
  7488. wolfSSL_ERR_error_string(err, buff));
  7489. /*err_sys("SSL_connect failed");*/
  7490. }
  7491. else {
  7492. #ifdef WOLFSSL_ASYNC_CRYPT
  7493. err = 0; /* Reset error */
  7494. #endif
  7495. do {
  7496. #ifdef WOLFSSL_ASYNC_CRYPT
  7497. if (err == WC_PENDING_E) {
  7498. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7499. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7500. }
  7501. #endif
  7502. ret = wolfSSL_write(ssl, msg, len);
  7503. err = wolfSSL_get_error(ssl, ret);
  7504. } while (err == WC_PENDING_E);
  7505. if (len != ret)
  7506. goto cleanup;
  7507. #ifdef WOLFSSL_ASYNC_CRYPT
  7508. err = 0; /* Reset error */
  7509. #endif
  7510. do {
  7511. #ifdef WOLFSSL_ASYNC_CRYPT
  7512. if (err == WC_PENDING_E) {
  7513. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7514. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7515. }
  7516. #endif
  7517. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7518. err = wolfSSL_get_error(ssl, ret);
  7519. } while (err == WC_PENDING_E);
  7520. if (ret > 0) {
  7521. input[ret] = '\0'; /* null term */
  7522. fprintf(stderr, "Server response: %s\n", input);
  7523. }
  7524. ((func_args*)args)->return_code = TEST_SUCCESS;
  7525. }
  7526. if (callbacks->on_result)
  7527. callbacks->on_result(ssl);
  7528. cleanup:
  7529. wolfSSL_free(ssl);
  7530. wolfSSL_CTX_free(ctx);
  7531. CloseSocket(sfd);
  7532. #ifdef WOLFSSL_TIRTOS
  7533. fdCloseSession(Task_self());
  7534. #endif
  7535. }
  7536. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7537. static int test_wolfSSL_read_write(void)
  7538. {
  7539. /* The unit testing for read and write shall happen simultaneously, since
  7540. * one can't do anything with one without the other. (Except for a failure
  7541. * test case.) This function will call all the others that will set up,
  7542. * execute, and report their test findings.
  7543. *
  7544. * Set up the success case first. This function will become the template
  7545. * for the other tests. This should eventually be renamed
  7546. *
  7547. * The success case isn't interesting, how can this fail?
  7548. * - Do not give the client context a CA certificate. The connect should
  7549. * fail. Do not need server for this?
  7550. * - Using NULL for the ssl object on server. Do not need client for this.
  7551. * - Using NULL for the ssl object on client. Do not need server for this.
  7552. * - Good ssl objects for client and server. Client write() without server
  7553. * read().
  7554. * - Good ssl objects for client and server. Server write() without client
  7555. * read().
  7556. * - Forgetting the password callback?
  7557. */
  7558. tcp_ready ready;
  7559. func_args client_args;
  7560. func_args server_args;
  7561. THREAD_TYPE serverThread;
  7562. EXPECT_DECLS;
  7563. XMEMSET(&client_args, 0, sizeof(func_args));
  7564. XMEMSET(&server_args, 0, sizeof(func_args));
  7565. #ifdef WOLFSSL_TIRTOS
  7566. fdOpenSession(Task_self());
  7567. #endif
  7568. StartTCP();
  7569. InitTcpReady(&ready);
  7570. #if defined(USE_WINDOWS_API)
  7571. /* use RNG to get random port if using windows */
  7572. ready.port = GetRandomPort();
  7573. #endif
  7574. server_args.signal = &ready;
  7575. client_args.signal = &ready;
  7576. start_thread(test_server_nofail, &server_args, &serverThread);
  7577. wait_tcp_ready(&server_args);
  7578. test_client_nofail(&client_args, NULL);
  7579. join_thread(serverThread);
  7580. ExpectTrue(client_args.return_code);
  7581. ExpectTrue(server_args.return_code);
  7582. FreeTcpReady(&ready);
  7583. #ifdef WOLFSSL_TIRTOS
  7584. fdOpenSession(Task_self());
  7585. #endif
  7586. return EXPECT_RESULT();
  7587. }
  7588. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7589. {
  7590. EXPECT_DECLS;
  7591. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7592. !defined(WOLFSSL_NO_TLS12)
  7593. /* The unit test for session resumption by re-using WOLFSSL object.
  7594. * WOLFSSL object is not cleared after first session. It reuse the object
  7595. * for second connection.
  7596. */
  7597. tcp_ready ready;
  7598. func_args client_args;
  7599. func_args server_args;
  7600. THREAD_TYPE serverThread;
  7601. callback_functions client_cbf;
  7602. callback_functions server_cbf;
  7603. XMEMSET(&client_args, 0, sizeof(func_args));
  7604. XMEMSET(&server_args, 0, sizeof(func_args));
  7605. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7606. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7607. #ifdef WOLFSSL_TIRTOS
  7608. fdOpenSession(Task_self());
  7609. #endif
  7610. StartTCP();
  7611. InitTcpReady(&ready);
  7612. #if defined(USE_WINDOWS_API)
  7613. /* use RNG to get random port if using windows */
  7614. ready.port = GetRandomPort();
  7615. #endif
  7616. client_cbf.method = wolfTLSv1_2_client_method;
  7617. server_cbf.method = wolfTLSv1_2_server_method;
  7618. client_args.callbacks = &client_cbf;
  7619. server_args.callbacks = &server_cbf;
  7620. server_args.signal = &ready;
  7621. client_args.signal = &ready;
  7622. /* the var is used for loop number */
  7623. server_args.argc = 2;
  7624. start_thread(test_server_loop, &server_args, &serverThread);
  7625. wait_tcp_ready(&server_args);
  7626. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7627. join_thread(serverThread);
  7628. ExpectTrue(client_args.return_code);
  7629. ExpectTrue(server_args.return_code);
  7630. FreeTcpReady(&ready);
  7631. #ifdef WOLFSSL_TIRTOS
  7632. fdOpenSession(Task_self());
  7633. #endif
  7634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7635. * !defined(WOLFSSL_TLS13) */
  7636. return EXPECT_RESULT();
  7637. }
  7638. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7639. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7640. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7641. WOLFSSL_CTX* ctx)
  7642. {
  7643. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7644. myVerifyAction = VERIFY_USE_PREVERFIY;
  7645. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7646. return TEST_SUCCESS;
  7647. }
  7648. #endif
  7649. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7650. {
  7651. EXPECT_DECLS;
  7652. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7653. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7654. test_ssl_cbf client_cbf;
  7655. test_ssl_cbf server_cbf;
  7656. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7657. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7658. #ifdef WOLFSSL_TLS13
  7659. client_cbf.method = wolfTLSv1_3_client_method;
  7660. #endif /* WOLFSSL_TLS13 */
  7661. client_cbf.ctx_ready =
  7662. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7663. /* test case 1 verify depth is equal to peer chain */
  7664. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7665. &server_cbf, NULL), TEST_SUCCESS);
  7666. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7667. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7668. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7669. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7670. return EXPECT_RESULT();
  7671. }
  7672. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7673. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7674. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7675. WOLFSSL_CTX* ctx)
  7676. {
  7677. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7678. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7679. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7680. return TEST_SUCCESS;
  7681. }
  7682. #endif
  7683. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7684. {
  7685. EXPECT_DECLS;
  7686. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7687. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7688. test_ssl_cbf client_cbf;
  7689. test_ssl_cbf server_cbf;
  7690. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7691. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7692. #ifdef WOLFSSL_TLS13
  7693. client_cbf.method = wolfTLSv1_3_client_method;
  7694. #endif /* WOLFSSL_TLS13 */
  7695. client_cbf.ctx_ready =
  7696. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7697. /* test case 2
  7698. * verify depth is zero, number of peer's chain is 2.
  7699. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7700. * callback.
  7701. */
  7702. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7703. &server_cbf, NULL), TEST_SUCCESS);
  7704. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7705. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7706. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7707. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7708. return EXPECT_RESULT();
  7709. }
  7710. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7711. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7712. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7713. WOLFSSL_CTX* ctx)
  7714. {
  7715. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7716. myVerifyAction = VERIFY_USE_PREVERFIY;
  7717. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7718. return TEST_SUCCESS;
  7719. }
  7720. #endif
  7721. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7722. {
  7723. EXPECT_DECLS;
  7724. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7725. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7726. test_ssl_cbf client_cbf;
  7727. test_ssl_cbf server_cbf;
  7728. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7729. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7730. #ifdef WOLFSSL_TLS13
  7731. client_cbf.method = wolfTLSv1_3_client_method;
  7732. #endif /* WOLFSSL_TLS13 */
  7733. client_cbf.ctx_ready =
  7734. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7735. /* test case 3
  7736. * verify depth is zero, number of peer's chain is 2
  7737. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7738. * therefore, handshake becomes failure.
  7739. */
  7740. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7741. &server_cbf, NULL), TEST_FAIL);
  7742. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7743. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7744. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7745. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7746. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7747. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7748. return EXPECT_RESULT();
  7749. }
  7750. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7751. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7752. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7753. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7754. {
  7755. EXPECT_DECLS;
  7756. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7757. return EXPECT_RESULT();
  7758. }
  7759. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7760. {
  7761. EXPECT_DECLS;
  7762. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7763. return EXPECT_RESULT();
  7764. }
  7765. #endif
  7766. static int test_wolfSSL_CTX_set_cipher_list(void)
  7767. {
  7768. EXPECT_DECLS;
  7769. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7770. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7771. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7772. WOLFSSL_CTX* ctxClient = NULL;
  7773. WOLFSSL* sslClient = NULL;
  7774. test_ssl_cbf client_cbf;
  7775. test_ssl_cbf server_cbf;
  7776. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7777. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7778. server_cbf.method = wolfTLSv1_2_server_method;
  7779. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7780. client_cbf.method = wolfTLSv1_2_client_method;
  7781. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7782. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7783. &server_cbf, NULL), TEST_SUCCESS);
  7784. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7785. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7786. /* check with cipher string that has '+' */
  7787. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7788. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7789. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7790. /* check for the existence of an ECDHE ECDSA cipher suite */
  7791. if (EXPECT_SUCCESS()) {
  7792. int i = 0;
  7793. int found = 0;
  7794. const char* suite;
  7795. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7796. WOLFSSL_CIPHER* current;
  7797. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7798. do {
  7799. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7800. if (current) {
  7801. suite = wolfSSL_CIPHER_get_name(current);
  7802. if (suite && XSTRSTR(suite, "ECDSA")) {
  7803. found = 1;
  7804. break;
  7805. }
  7806. }
  7807. } while (current);
  7808. ExpectIntEQ(found, 1);
  7809. }
  7810. wolfSSL_free(sslClient);
  7811. wolfSSL_CTX_free(ctxClient);
  7812. #endif
  7813. return EXPECT_RESULT();
  7814. }
  7815. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7816. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7817. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7818. WOLFSSL* ssl)
  7819. {
  7820. EXPECT_DECLS;
  7821. size_t msg_len;
  7822. (void)ctx;
  7823. /* get_finished test */
  7824. /* 1. get own sent message */
  7825. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7826. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7827. ExpectIntGE(msg_len, 0);
  7828. /* 2. get peer message */
  7829. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7830. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7831. ExpectIntGE(msg_len, 0);
  7832. return EXPECT_RESULT();
  7833. }
  7834. #endif
  7835. static int test_wolfSSL_get_finished(void)
  7836. {
  7837. EXPECT_DECLS;
  7838. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7839. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7840. test_ssl_cbf client_cbf;
  7841. test_ssl_cbf server_cbf;
  7842. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7843. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7844. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7845. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7846. TEST_SUCCESS);
  7847. /* test received msg vs sent msg */
  7848. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7849. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7850. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7851. return EXPECT_RESULT();
  7852. }
  7853. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7854. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7855. !defined(NO_SESSION_CACHE)
  7856. /* Sessions to restore/store */
  7857. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7858. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7859. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7860. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7861. {
  7862. /* Don't store sessions. Lookup is still enabled. */
  7863. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7864. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7865. #ifdef OPENSSL_EXTRA
  7866. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7867. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7868. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7869. #endif
  7870. /* Require both peers to provide certs */
  7871. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7872. }
  7873. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7874. {
  7875. WOLFSSL_SESSION** sess;
  7876. if (wolfSSL_is_server(ssl))
  7877. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7878. else
  7879. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7880. if (*sess == NULL) {
  7881. #ifdef NO_SESSION_CACHE_REF
  7882. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7883. #else
  7884. /* Test for backwards compatibility */
  7885. if (wolfSSL_is_server(ssl)) {
  7886. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7887. }
  7888. else {
  7889. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7890. }
  7891. #endif
  7892. /* Now save the session in the internal store to make it available
  7893. * for lookup. For TLS 1.3, we can't save the session without
  7894. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7895. * session from cache. */
  7896. if (wolfSSL_is_server(ssl)
  7897. #ifndef WOLFSSL_TICKET_HAVE_ID
  7898. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7899. #endif
  7900. )
  7901. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7902. *sess), WOLFSSL_SUCCESS);
  7903. }
  7904. else {
  7905. /* If we have a session retrieved then remaining connections should be
  7906. * resuming on that session */
  7907. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7908. }
  7909. /* Save CTX to be able to decrypt tickets */
  7910. if (wolfSSL_is_server(ssl) &&
  7911. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7912. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7913. = wolfSSL_get_SSL_CTX(ssl));
  7914. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7915. WOLFSSL_SUCCESS);
  7916. }
  7917. #ifdef SESSION_CERTS
  7918. #ifndef WOLFSSL_TICKET_HAVE_ID
  7919. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7920. wolfSSL_session_reused(ssl))
  7921. #endif
  7922. {
  7923. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7924. * for all connections. TLS 1.3 only has tickets so if we don't
  7925. * include the session id in the ticket then the certificates
  7926. * will not be available on resumption. */
  7927. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7928. AssertNotNull(peer);
  7929. wolfSSL_X509_free(peer);
  7930. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7931. #ifdef OPENSSL_EXTRA
  7932. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7933. #endif
  7934. }
  7935. #endif /* SESSION_CERTS */
  7936. }
  7937. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7938. {
  7939. /* Set the session to reuse for the client */
  7940. AssertIntEQ(wolfSSL_set_session(ssl,
  7941. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7942. }
  7943. #endif
  7944. static int test_wolfSSL_CTX_add_session(void)
  7945. {
  7946. EXPECT_DECLS;
  7947. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7948. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7949. !defined(NO_SESSION_CACHE)
  7950. tcp_ready ready;
  7951. func_args client_args;
  7952. func_args server_args;
  7953. THREAD_TYPE serverThread;
  7954. callback_functions client_cb;
  7955. callback_functions server_cb;
  7956. method_provider methods[][2] = {
  7957. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7958. !defined(NO_DES3))
  7959. /* Without AES there are almost no ciphersuites available. This leads
  7960. * to no ciphersuites being available and an error. */
  7961. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7962. #endif
  7963. #ifndef WOLFSSL_NO_TLS12
  7964. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7965. #endif
  7966. /* Needs the default ticket callback since it is tied to the
  7967. * connection context and this makes it easy to carry over the ticket
  7968. * crypto context between connections */
  7969. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7970. defined(HAVE_SESSION_TICKET)
  7971. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7972. #endif
  7973. };
  7974. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7975. size_t i, j;
  7976. for (i = 0; i < methodsLen; i++) {
  7977. /* First run creates a connection while the second+ run will attempt
  7978. * to resume the connection. The trick is that the internal cache
  7979. * is turned off. wolfSSL_CTX_add_session should put the session in
  7980. * the cache anyway. */
  7981. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7982. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7983. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7984. #ifdef NO_SESSION_CACHE_REF
  7985. for (j = 0; j < 4; j++) {
  7986. #else
  7987. /* The session may be overwritten in this case. Do only one resumption
  7988. * to stop this test from failing intermittently. */
  7989. for (j = 0; j < 2; j++) {
  7990. #endif
  7991. #ifdef WOLFSSL_TIRTOS
  7992. fdOpenSession(Task_self());
  7993. #endif
  7994. StartTCP();
  7995. InitTcpReady(&ready);
  7996. XMEMSET(&client_args, 0, sizeof(func_args));
  7997. XMEMSET(&server_args, 0, sizeof(func_args));
  7998. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7999. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  8000. client_cb.method = methods[i][0];
  8001. server_cb.method = methods[i][1];
  8002. server_args.signal = &ready;
  8003. server_args.callbacks = &server_cb;
  8004. client_args.signal = &ready;
  8005. client_args.callbacks = &client_cb;
  8006. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8007. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8008. server_cb.isSharedCtx = 1;
  8009. }
  8010. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8011. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8012. if (j != 0)
  8013. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8014. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8015. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8016. server_cb.ticNoInit = 1; /* Use default builtin */
  8017. start_thread(test_server_nofail, &server_args, &serverThread);
  8018. wait_tcp_ready(&server_args);
  8019. test_client_nofail(&client_args, NULL);
  8020. join_thread(serverThread);
  8021. ExpectTrue(client_args.return_code);
  8022. ExpectTrue(server_args.return_code);
  8023. FreeTcpReady(&ready);
  8024. if (EXPECT_FAIL())
  8025. break;
  8026. }
  8027. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8028. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8029. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8030. if (EXPECT_FAIL())
  8031. break;
  8032. }
  8033. #endif
  8034. return EXPECT_RESULT();
  8035. }
  8036. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8037. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8038. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8039. defined(HAVE_SESSION_TICKET) && \
  8040. !defined(TITAN_SESSION_CACHE) && \
  8041. !defined(HUGE_SESSION_CACHE) && \
  8042. !defined(BIG_SESSION_CACHE) && \
  8043. !defined(MEDIUM_SESSION_CACHE)
  8044. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8045. /* Sessions to restore/store */
  8046. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8047. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8048. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8049. static int twcase_new_session_called = 0;
  8050. static int twcase_remove_session_called = 0;
  8051. static int twcase_get_session_called = 0;
  8052. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8053. #define SESSION_CACHE_SIZE 33
  8054. typedef struct {
  8055. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8056. WOLFSSL_SESSION* value;
  8057. } hashTable_entry;
  8058. typedef struct {
  8059. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8060. size_t capacity; /* size of entries */
  8061. size_t length; /* number of items in the hash table */
  8062. wolfSSL_Mutex htLock; /* lock */
  8063. }hashTable;
  8064. static hashTable server_sessionCache;
  8065. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8066. {
  8067. int i;
  8068. unsigned int len;
  8069. (void)ssl;
  8070. /*
  8071. * This example uses a hash table.
  8072. * Steps you should take for a non-demo code:
  8073. * - acquire a lock for the file named according to the session id
  8074. * - open the file
  8075. * - encrypt and write the SSL_SESSION object to the file
  8076. * - release the lock
  8077. *
  8078. * Return:
  8079. * 0: The callback does not wish to hold a reference of the sess
  8080. * 1: The callback wants to hold a reference of the sess. The callback is
  8081. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8082. */
  8083. if (sess == NULL)
  8084. return 0;
  8085. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8086. return 0;
  8087. }
  8088. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8089. if (server_sessionCache.entries[i].value == NULL) {
  8090. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8091. server_sessionCache.entries[i].value = sess;
  8092. server_sessionCache.length++;
  8093. break;
  8094. }
  8095. }
  8096. ++twcase_new_session_called;
  8097. wc_UnLockMutex(&server_sessionCache.htLock);
  8098. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8099. twcase_new_session_called);
  8100. return 1;
  8101. }
  8102. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8103. {
  8104. int i;
  8105. (void)ctx;
  8106. (void)sess;
  8107. if (sess == NULL)
  8108. return;
  8109. /*
  8110. * This example uses a hash table.
  8111. * Steps you should take for a non-demo code:
  8112. * - acquire a lock for the file named according to the session id
  8113. * - remove the file
  8114. * - release the lock
  8115. */
  8116. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8117. return;
  8118. }
  8119. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8120. if (server_sessionCache.entries[i].key != NULL &&
  8121. XMEMCMP(server_sessionCache.entries[i].key,
  8122. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8123. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8124. server_sessionCache.entries[i].value = NULL;
  8125. server_sessionCache.entries[i].key = NULL;
  8126. server_sessionCache.length--;
  8127. break;
  8128. }
  8129. }
  8130. ++twcase_remove_session_called;
  8131. wc_UnLockMutex(&server_sessionCache.htLock);
  8132. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8133. twcase_remove_session_called);
  8134. }
  8135. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8136. const unsigned char *id, int len, int *ref)
  8137. {
  8138. int i;
  8139. (void)ssl;
  8140. (void)id;
  8141. (void)len;
  8142. /*
  8143. * This example uses a hash table.
  8144. * Steps you should take for a non-demo code:
  8145. * - acquire a lock for the file named according to the session id in the
  8146. * 2nd arg
  8147. * - read and decrypt contents of file and create a new SSL_SESSION
  8148. * - object release the lock
  8149. * - return the new session object
  8150. */
  8151. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8152. ++twcase_get_session_called);
  8153. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8154. * be responsible for the pointer then set to 0. */
  8155. *ref = 1;
  8156. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8157. if (server_sessionCache.entries[i].key != NULL &&
  8158. XMEMCMP(server_sessionCache.entries[i].key, id,
  8159. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8160. return server_sessionCache.entries[i].value;
  8161. }
  8162. }
  8163. return NULL;
  8164. }
  8165. static int twcase_get_sessionCb_cleanup(void)
  8166. {
  8167. int i;
  8168. int cnt = 0;
  8169. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8170. * for freeing sessions */
  8171. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8172. if (server_sessionCache.entries[i].value != NULL) {
  8173. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8174. cnt++;
  8175. }
  8176. }
  8177. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8178. cnt);
  8179. return TEST_SUCCESS;
  8180. }
  8181. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8182. {
  8183. EXPECT_DECLS;
  8184. /* off - Disable internal cache */
  8185. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8186. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8187. #ifdef OPENSSL_EXTRA
  8188. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8189. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8190. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8191. #endif
  8192. /* off - Do not setup external cache */
  8193. /* Require both peers to provide certs */
  8194. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8195. return EXPECT_RESULT();
  8196. }
  8197. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8198. {
  8199. /* on - internal cache is on by default */
  8200. /* off - Do not setup external cache */
  8201. /* Require both peers to provide certs */
  8202. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8203. return TEST_SUCCESS;
  8204. }
  8205. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8206. {
  8207. EXPECT_DECLS;
  8208. /* off - Disable internal cache */
  8209. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8210. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8211. #ifdef OPENSSL_EXTRA
  8212. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8213. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8214. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8215. #endif
  8216. /* on - Enable external cache */
  8217. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8218. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8219. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8220. /* Require both peers to provide certs */
  8221. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8222. return EXPECT_RESULT();
  8223. }
  8224. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8225. {
  8226. /* on - internal cache is on by default */
  8227. /* on - Enable external cache */
  8228. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8229. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8230. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8231. /* Require both peers to provide certs */
  8232. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8233. return TEST_SUCCESS;
  8234. }
  8235. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8236. {
  8237. /* on - internal cache is on by default */
  8238. /* on - Enable external cache */
  8239. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8240. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8241. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8242. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8243. /* Require both peers to provide certs */
  8244. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8245. return TEST_SUCCESS;
  8246. }
  8247. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8248. {
  8249. EXPECT_DECLS;
  8250. WOLFSSL_SESSION** sess;
  8251. if (wolfSSL_is_server(ssl))
  8252. sess = &twcase_server_first_session_ptr;
  8253. else
  8254. return TEST_SUCCESS;
  8255. if (*sess == NULL) {
  8256. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8257. /* Now save the session in the internal store to make it available
  8258. * for lookup. For TLS 1.3, we can't save the session without
  8259. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8260. * session from cache. */
  8261. if (wolfSSL_is_server(ssl)
  8262. #ifndef WOLFSSL_TICKET_HAVE_ID
  8263. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8264. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8265. #endif
  8266. ) {
  8267. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8268. *sess), WOLFSSL_SUCCESS);
  8269. }
  8270. }
  8271. /* Save CTX to be able to decrypt tickets */
  8272. if (twcase_server_current_ctx_ptr == NULL) {
  8273. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8274. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8275. WOLFSSL_SUCCESS);
  8276. }
  8277. #ifdef SESSION_CERTS
  8278. #ifndef WOLFSSL_TICKET_HAVE_ID
  8279. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8280. wolfSSL_session_reused(ssl))
  8281. #endif
  8282. {
  8283. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8284. * for all connections. TLS 1.3 only has tickets so if we don't
  8285. * include the session id in the ticket then the certificates
  8286. * will not be available on resumption. */
  8287. WOLFSSL_X509* peer = NULL;
  8288. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8289. wolfSSL_X509_free(peer);
  8290. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8291. }
  8292. #endif
  8293. return EXPECT_RESULT();
  8294. }
  8295. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8296. {
  8297. EXPECT_DECLS;
  8298. WOLFSSL_SESSION** sess;
  8299. sess = &twcase_client_first_session_ptr;
  8300. if (*sess == NULL) {
  8301. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8302. }
  8303. else {
  8304. /* If we have a session retrieved then remaining connections should be
  8305. * resuming on that session */
  8306. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8307. }
  8308. #ifdef SESSION_CERTS
  8309. #ifndef WOLFSSL_TICKET_HAVE_ID
  8310. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8311. wolfSSL_session_reused(ssl))
  8312. #endif
  8313. {
  8314. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8315. ExpectNotNull(peer);
  8316. wolfSSL_X509_free(peer);
  8317. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8318. #ifdef OPENSSL_EXTRA
  8319. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8320. #endif
  8321. }
  8322. #endif
  8323. return EXPECT_RESULT();
  8324. }
  8325. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8326. {
  8327. EXPECT_DECLS;
  8328. /* Set the session to reuse for the client */
  8329. ExpectNotNull(ssl);
  8330. ExpectNotNull(twcase_client_first_session_ptr);
  8331. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8332. WOLFSSL_SUCCESS);
  8333. return EXPECT_RESULT();
  8334. }
  8335. struct test_add_session_ext_params {
  8336. method_provider client_meth;
  8337. method_provider server_meth;
  8338. const char* tls_version;
  8339. };
  8340. static int test_wolfSSL_CTX_add_session_ext(
  8341. struct test_add_session_ext_params* param)
  8342. {
  8343. EXPECT_DECLS;
  8344. /* Test the default 33 sessions */
  8345. int j;
  8346. /* Clear cache before starting */
  8347. wolfSSL_CTX_flush_sessions(NULL, -1);
  8348. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8349. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8350. return BAD_MUTEX_E;
  8351. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8352. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8353. for (j = 0; j < 5; j++) {
  8354. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8355. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8356. test_ssl_cbf client_cb;
  8357. test_ssl_cbf server_cb;
  8358. (void)dtls;
  8359. /* Test five cache configurations */
  8360. twcase_client_first_session_ptr = NULL;
  8361. twcase_server_first_session_ptr = NULL;
  8362. twcase_server_current_ctx_ptr = NULL;
  8363. twcase_new_session_called = 0;
  8364. twcase_remove_session_called = 0;
  8365. twcase_get_session_called = 0;
  8366. /* connection 1 - first connection */
  8367. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8368. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8369. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8370. client_cb.method = param->client_meth;
  8371. server_cb.method = param->server_meth;
  8372. if (dtls)
  8373. client_cb.doUdp = server_cb.doUdp = 1;
  8374. /* Setup internal and external cache */
  8375. switch (j) {
  8376. case 0:
  8377. /* SSL_OP_NO_TICKET stateful ticket case */
  8378. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8379. break;
  8380. case 1:
  8381. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8382. break;
  8383. case 2:
  8384. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8385. break;
  8386. case 3:
  8387. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8388. break;
  8389. case 4:
  8390. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8391. break;
  8392. }
  8393. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8394. /* Add session to internal cache and save SSL session for testing */
  8395. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8396. /* Save client SSL session for testing */
  8397. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8398. server_cb.ticNoInit = 1; /* Use default builtin */
  8399. /* Don't free/release ctx */
  8400. server_cb.ctx = twcase_server_current_ctx_ptr;
  8401. server_cb.isSharedCtx = 1;
  8402. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8403. &server_cb, NULL), TEST_SUCCESS);
  8404. ExpectIntEQ(twcase_get_session_called, 0);
  8405. if (EXPECT_FAIL()) {
  8406. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8407. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8408. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8409. break;
  8410. }
  8411. switch (j) {
  8412. case 0:
  8413. case 1:
  8414. case 2:
  8415. /* cache cannot be searched with out a connection */
  8416. /* Add a new session */
  8417. ExpectIntEQ(twcase_new_session_called, 1);
  8418. /* In twcase_server_sess_ctx_pre_shutdown
  8419. * wolfSSL_CTX_add_session which evicts the existing session
  8420. * in cache and adds it back in */
  8421. ExpectIntLE(twcase_remove_session_called, 1);
  8422. break;
  8423. case 3:
  8424. case 4:
  8425. /* no external cache */
  8426. ExpectIntEQ(twcase_new_session_called, 0);
  8427. ExpectIntEQ(twcase_remove_session_called, 0);
  8428. break;
  8429. }
  8430. /* connection 2 - session resume */
  8431. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8432. twcase_new_session_called = 0;
  8433. twcase_remove_session_called = 0;
  8434. twcase_get_session_called = 0;
  8435. server_cb.on_result = 0;
  8436. client_cb.on_result = 0;
  8437. server_cb.ticNoInit = 1; /* Use default builtin */
  8438. server_cb.ctx = twcase_server_current_ctx_ptr;
  8439. /* try session resumption */
  8440. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8441. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8442. &server_cb, NULL), TEST_SUCCESS);
  8443. /* Clear cache before checking */
  8444. wolfSSL_CTX_flush_sessions(NULL, -1);
  8445. switch (j) {
  8446. case 0:
  8447. if (tls13) {
  8448. /* (D)TLSv1.3 stateful case */
  8449. /* cache hit */
  8450. /* DTLS accesses cache once for stateless parsing and
  8451. * once for stateful parsing */
  8452. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8453. /* (D)TLSv1.3 creates a new ticket,
  8454. * updates both internal and external cache */
  8455. ExpectIntEQ(twcase_new_session_called, 1);
  8456. /* A new session ID is created for a new ticket */
  8457. ExpectIntEQ(twcase_remove_session_called, 2);
  8458. }
  8459. else {
  8460. /* non (D)TLSv1.3 case, no update */
  8461. /* DTLS accesses cache once for stateless parsing and
  8462. * once for stateful parsing */
  8463. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8464. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8465. #else
  8466. ExpectIntEQ(twcase_get_session_called, 1);
  8467. #endif
  8468. ExpectIntEQ(twcase_new_session_called, 0);
  8469. /* Called on session added in
  8470. * twcase_server_sess_ctx_pre_shutdown */
  8471. ExpectIntEQ(twcase_remove_session_called, 1);
  8472. }
  8473. break;
  8474. case 1:
  8475. if (tls13) {
  8476. /* (D)TLSv1.3 case */
  8477. /* cache hit */
  8478. ExpectIntEQ(twcase_get_session_called, 1);
  8479. /* (D)TLSv1.3 creates a new ticket,
  8480. * updates both internal and external cache */
  8481. ExpectIntEQ(twcase_new_session_called, 1);
  8482. /* Called on session added in
  8483. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8484. ExpectIntEQ(twcase_remove_session_called, 1);
  8485. }
  8486. else {
  8487. /* non (D)TLSv1.3 case */
  8488. /* cache hit */
  8489. /* DTLS accesses cache once for stateless parsing and
  8490. * once for stateful parsing */
  8491. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8492. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8493. #else
  8494. ExpectIntEQ(twcase_get_session_called, 1);
  8495. #endif
  8496. ExpectIntEQ(twcase_new_session_called, 0);
  8497. /* Called on session added in
  8498. * twcase_server_sess_ctx_pre_shutdown */
  8499. ExpectIntEQ(twcase_remove_session_called, 1);
  8500. }
  8501. break;
  8502. case 2:
  8503. if (tls13) {
  8504. /* (D)TLSv1.3 case */
  8505. /* cache hit */
  8506. ExpectIntEQ(twcase_get_session_called, 1);
  8507. /* (D)TLSv1.3 creates a new ticket,
  8508. * updates both internal and external cache */
  8509. ExpectIntEQ(twcase_new_session_called, 1);
  8510. /* Called on session added in
  8511. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8512. ExpectIntEQ(twcase_remove_session_called, 1);
  8513. }
  8514. else {
  8515. /* non (D)TLSv1.3 case */
  8516. /* cache hit */
  8517. /* DTLS accesses cache once for stateless parsing and
  8518. * once for stateful parsing */
  8519. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8520. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8521. #else
  8522. ExpectIntEQ(twcase_get_session_called, 1);
  8523. #endif
  8524. ExpectIntEQ(twcase_new_session_called, 0);
  8525. /* Called on session added in
  8526. * twcase_server_sess_ctx_pre_shutdown */
  8527. ExpectIntEQ(twcase_remove_session_called, 1);
  8528. }
  8529. break;
  8530. case 3:
  8531. case 4:
  8532. /* no external cache */
  8533. ExpectIntEQ(twcase_get_session_called, 0);
  8534. ExpectIntEQ(twcase_new_session_called, 0);
  8535. ExpectIntEQ(twcase_remove_session_called, 0);
  8536. break;
  8537. }
  8538. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8539. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8540. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8541. if (EXPECT_FAIL())
  8542. break;
  8543. }
  8544. twcase_get_sessionCb_cleanup();
  8545. XMEMSET(&server_sessionCache.entries, 0,
  8546. sizeof(server_sessionCache.entries));
  8547. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8548. wc_FreeMutex(&server_sessionCache.htLock);
  8549. return EXPECT_RESULT();
  8550. }
  8551. #endif
  8552. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8553. {
  8554. EXPECT_DECLS;
  8555. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8556. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8557. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8558. defined(HAVE_SESSION_TICKET) && \
  8559. !defined(TITAN_SESSION_CACHE) && \
  8560. !defined(HUGE_SESSION_CACHE) && \
  8561. !defined(BIG_SESSION_CACHE) && \
  8562. !defined(MEDIUM_SESSION_CACHE)
  8563. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8564. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8565. struct test_add_session_ext_params param[1] = {
  8566. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8567. };
  8568. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8569. #endif
  8570. #endif
  8571. return EXPECT_RESULT();
  8572. }
  8573. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8574. {
  8575. EXPECT_DECLS;
  8576. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8577. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8578. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8579. defined(HAVE_SESSION_TICKET) && \
  8580. !defined(TITAN_SESSION_CACHE) && \
  8581. !defined(HUGE_SESSION_CACHE) && \
  8582. !defined(BIG_SESSION_CACHE) && \
  8583. !defined(MEDIUM_SESSION_CACHE)
  8584. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8585. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8586. #ifdef WOLFSSL_DTLS13
  8587. struct test_add_session_ext_params param[1] = {
  8588. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8589. };
  8590. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8591. #endif
  8592. #endif
  8593. #endif
  8594. return EXPECT_RESULT();
  8595. }
  8596. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8597. {
  8598. EXPECT_DECLS;
  8599. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8600. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8601. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8602. defined(HAVE_SESSION_TICKET) && \
  8603. !defined(TITAN_SESSION_CACHE) && \
  8604. !defined(HUGE_SESSION_CACHE) && \
  8605. !defined(BIG_SESSION_CACHE) && \
  8606. !defined(MEDIUM_SESSION_CACHE)
  8607. #ifndef WOLFSSL_NO_TLS12
  8608. struct test_add_session_ext_params param[1] = {
  8609. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8610. };
  8611. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8612. #endif
  8613. #endif
  8614. return EXPECT_RESULT();
  8615. }
  8616. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8617. {
  8618. EXPECT_DECLS;
  8619. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8620. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8621. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8622. defined(HAVE_SESSION_TICKET) && \
  8623. !defined(TITAN_SESSION_CACHE) && \
  8624. !defined(HUGE_SESSION_CACHE) && \
  8625. !defined(BIG_SESSION_CACHE) && \
  8626. !defined(MEDIUM_SESSION_CACHE)
  8627. #ifndef WOLFSSL_NO_TLS12
  8628. #ifdef WOLFSSL_DTLS
  8629. struct test_add_session_ext_params param[1] = {
  8630. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8631. };
  8632. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8633. #endif
  8634. #endif
  8635. #endif
  8636. return EXPECT_RESULT();
  8637. }
  8638. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8639. {
  8640. EXPECT_DECLS;
  8641. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8642. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8643. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8644. defined(HAVE_SESSION_TICKET) && \
  8645. !defined(TITAN_SESSION_CACHE) && \
  8646. !defined(HUGE_SESSION_CACHE) && \
  8647. !defined(BIG_SESSION_CACHE) && \
  8648. !defined(MEDIUM_SESSION_CACHE)
  8649. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8650. !defined(NO_DES3))
  8651. struct test_add_session_ext_params param[1] = {
  8652. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8653. };
  8654. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8655. #endif
  8656. #endif
  8657. return EXPECT_RESULT();
  8658. }
  8659. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8660. {
  8661. EXPECT_DECLS;
  8662. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8663. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8664. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8665. defined(HAVE_SESSION_TICKET) && \
  8666. !defined(TITAN_SESSION_CACHE) && \
  8667. !defined(HUGE_SESSION_CACHE) && \
  8668. !defined(BIG_SESSION_CACHE) && \
  8669. !defined(MEDIUM_SESSION_CACHE)
  8670. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8671. !defined(NO_DES3))
  8672. #ifdef WOLFSSL_DTLS
  8673. struct test_add_session_ext_params param[1] = {
  8674. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8675. };
  8676. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8677. #endif
  8678. #endif
  8679. #endif
  8680. return EXPECT_RESULT();
  8681. }
  8682. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8683. /* canned export of a session using older version 3 */
  8684. static unsigned char version_3[] = {
  8685. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8686. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8687. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8688. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8689. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8690. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8691. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8692. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8693. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8695. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8696. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8697. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8698. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8699. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8700. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8701. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8702. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8703. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8709. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8713. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8714. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8715. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8716. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8717. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8718. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8719. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8720. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8721. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8722. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8723. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8724. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8725. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8726. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8727. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8728. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8729. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8730. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8731. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8732. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8733. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8734. 0x2E, 0x31, 0xED, 0x4F
  8735. };
  8736. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8737. static int test_wolfSSL_dtls_export(void)
  8738. {
  8739. EXPECT_DECLS;
  8740. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8741. tcp_ready ready;
  8742. func_args client_args;
  8743. func_args server_args;
  8744. THREAD_TYPE serverThread;
  8745. callback_functions server_cbf;
  8746. callback_functions client_cbf;
  8747. #ifdef WOLFSSL_TIRTOS
  8748. fdOpenSession(Task_self());
  8749. #endif
  8750. InitTcpReady(&ready);
  8751. #if defined(USE_WINDOWS_API)
  8752. /* use RNG to get random port if using windows */
  8753. ready.port = GetRandomPort();
  8754. #endif
  8755. /* set using dtls */
  8756. XMEMSET(&client_args, 0, sizeof(func_args));
  8757. XMEMSET(&server_args, 0, sizeof(func_args));
  8758. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8759. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8760. server_cbf.method = wolfDTLSv1_2_server_method;
  8761. client_cbf.method = wolfDTLSv1_2_client_method;
  8762. server_args.callbacks = &server_cbf;
  8763. client_args.callbacks = &client_cbf;
  8764. server_args.signal = &ready;
  8765. client_args.signal = &ready;
  8766. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8767. wait_tcp_ready(&server_args);
  8768. run_wolfssl_client(&client_args);
  8769. join_thread(serverThread);
  8770. ExpectTrue(client_args.return_code);
  8771. ExpectTrue(server_args.return_code);
  8772. FreeTcpReady(&ready);
  8773. #ifdef WOLFSSL_TIRTOS
  8774. fdOpenSession(Task_self());
  8775. #endif
  8776. if (EXPECT_SUCCESS()) {
  8777. SOCKET_T sockfd = 0;
  8778. WOLFSSL_CTX* ctx = NULL;
  8779. WOLFSSL* ssl = NULL;
  8780. char msg[64] = "hello wolfssl!";
  8781. char reply[1024];
  8782. int msgSz = (int)XSTRLEN(msg);
  8783. byte *session, *window;
  8784. unsigned int sessionSz = 0;
  8785. unsigned int windowSz = 0;
  8786. #ifndef TEST_IPV6
  8787. struct sockaddr_in peerAddr;
  8788. #else
  8789. struct sockaddr_in6 peerAddr;
  8790. #endif /* TEST_IPV6 */
  8791. int i;
  8792. /* Set ctx to DTLS 1.2 */
  8793. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8794. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8795. /* test importing version 3 */
  8796. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8797. /* test importing bad length and bad version */
  8798. version_3[2] += 1;
  8799. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8800. version_3[2] -= 1; version_3[1] = 0XA0;
  8801. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8802. wolfSSL_free(ssl);
  8803. wolfSSL_CTX_free(ctx);
  8804. /* check storing client state after connection and storing window only */
  8805. #ifdef WOLFSSL_TIRTOS
  8806. fdOpenSession(Task_self());
  8807. #endif
  8808. InitTcpReady(&ready);
  8809. #if defined(USE_WINDOWS_API)
  8810. /* use RNG to get random port if using windows */
  8811. ready.port = GetRandomPort();
  8812. #endif
  8813. /* set using dtls */
  8814. XMEMSET(&server_args, 0, sizeof(func_args));
  8815. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8816. server_cbf.method = wolfDTLSv1_2_server_method;
  8817. server_cbf.doUdp = 1;
  8818. server_args.callbacks = &server_cbf;
  8819. server_args.argc = 3; /* set loop_count to 3 */
  8820. server_args.signal = &ready;
  8821. start_thread(test_server_nofail, &server_args, &serverThread);
  8822. wait_tcp_ready(&server_args);
  8823. /* create and connect with client */
  8824. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8825. ExpectIntEQ(WOLFSSL_SUCCESS,
  8826. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8827. ExpectIntEQ(WOLFSSL_SUCCESS,
  8828. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8829. ExpectIntEQ(WOLFSSL_SUCCESS,
  8830. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8831. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8832. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8833. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8834. /* store server information connected too */
  8835. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8836. #ifndef TEST_IPV6
  8837. peerAddr.sin_family = AF_INET;
  8838. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8839. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8840. #else
  8841. peerAddr.sin6_family = AF_INET6;
  8842. ExpectIntEQ(
  8843. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8844. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8845. #endif
  8846. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8847. WOLFSSL_SUCCESS);
  8848. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8849. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8850. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8851. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8852. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8853. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8854. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8855. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8856. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8857. wolfSSL_free(ssl);
  8858. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8859. /* restore state */
  8860. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8861. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8862. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8863. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8864. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8865. WOLFSSL_SUCCESS);
  8866. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8867. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8868. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8869. wolfSSL_free(ssl);
  8870. }
  8871. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8872. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8873. wolfSSL_CTX_free(ctx);
  8874. fprintf(stderr, "done and waiting for server\n");
  8875. join_thread(serverThread);
  8876. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8877. FreeTcpReady(&ready);
  8878. #ifdef WOLFSSL_TIRTOS
  8879. fdOpenSession(Task_self());
  8880. #endif
  8881. }
  8882. #endif
  8883. return EXPECT_RESULT();
  8884. }
  8885. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8886. #ifdef WOLFSSL_TLS13
  8887. static const byte canned_client_tls13_session[] = {
  8888. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8889. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8890. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8891. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8892. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8893. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8894. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8895. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8896. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8897. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8898. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8899. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8900. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8901. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8902. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8903. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8904. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8905. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8906. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8907. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8908. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8909. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8910. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8911. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8912. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8913. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8914. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8915. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8916. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8917. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8918. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8919. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8920. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8921. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8922. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8923. 0x00, 0x03
  8924. };
  8925. static const byte canned_server_tls13_session[] = {
  8926. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8927. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8928. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8929. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8930. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8931. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8932. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8933. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8934. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8935. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8936. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8937. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8938. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8939. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8940. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8941. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8942. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8943. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8944. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8945. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8946. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8947. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8948. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8949. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8950. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8951. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8952. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8953. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8954. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8955. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8956. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8957. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8958. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8959. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8960. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8961. 0x00, 0x04
  8962. };
  8963. #endif /* WOLFSSL_TLS13 */
  8964. static const byte canned_client_session[] = {
  8965. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8966. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8967. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8968. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8969. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8970. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8971. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8972. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8973. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8974. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8975. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8976. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8977. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8978. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8979. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8980. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8981. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8982. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8983. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8984. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8985. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8986. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8987. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8988. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8989. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8990. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8991. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8992. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8993. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8995. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8996. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8997. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8998. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8999. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9000. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9001. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9002. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9003. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9004. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9005. 0x00, 0x03
  9006. };
  9007. static const byte canned_server_session[] = {
  9008. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9009. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9010. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9011. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9012. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9013. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9014. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9015. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9016. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9017. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9018. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9019. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9020. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9021. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9022. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9023. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9024. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9025. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9026. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9027. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9028. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9029. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9030. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9031. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9032. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9033. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9034. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9035. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9036. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9038. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9039. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9040. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9041. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9042. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9043. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9044. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9045. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9046. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9047. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9048. 0x00, 0x04
  9049. };
  9050. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9051. {
  9052. SOCKET_T sockfd = 0;
  9053. SOCKET_T clientfd = 0;
  9054. word16 port;
  9055. callback_functions* cbf;
  9056. WOLFSSL_CTX* ctx = 0;
  9057. WOLFSSL* ssl = 0;
  9058. char msg[] = "I hear you fa shizzle!";
  9059. char input[1024];
  9060. int idx;
  9061. #ifdef WOLFSSL_TIRTOS
  9062. fdOpenSession(Task_self());
  9063. #endif
  9064. ((func_args*)args)->return_code = TEST_FAIL;
  9065. cbf = ((func_args*)args)->callbacks;
  9066. #if defined(USE_WINDOWS_API)
  9067. port = ((func_args*)args)->signal->port;
  9068. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9069. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9070. /* Let tcp_listen assign port */
  9071. port = 0;
  9072. #else
  9073. /* Use default port */
  9074. port = wolfSSLPort;
  9075. #endif
  9076. /* do it here to detect failure */
  9077. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9078. CloseSocket(sockfd);
  9079. {
  9080. WOLFSSL_METHOD* method = NULL;
  9081. if (cbf != NULL && cbf->method != NULL) {
  9082. method = cbf->method();
  9083. }
  9084. else {
  9085. method = wolfTLSv1_2_server_method();
  9086. }
  9087. ctx = wolfSSL_CTX_new(method);
  9088. }
  9089. if (ctx == NULL) {
  9090. goto done;
  9091. }
  9092. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9093. /* call ctx setup callback */
  9094. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9095. cbf->ctx_ready(ctx);
  9096. }
  9097. ssl = wolfSSL_new(ctx);
  9098. if (ssl == NULL) {
  9099. goto done;
  9100. }
  9101. wolfSSL_set_fd(ssl, clientfd);
  9102. /* call ssl setup callback */
  9103. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9104. cbf->ssl_ready(ssl);
  9105. }
  9106. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9107. if (idx > 0) {
  9108. input[idx] = '\0';
  9109. fprintf(stderr, "Client message export/import: %s\n", input);
  9110. }
  9111. else {
  9112. fprintf(stderr, "ret = %d error = %d\n", idx,
  9113. wolfSSL_get_error(ssl, idx));
  9114. goto done;
  9115. }
  9116. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9117. /*err_sys("SSL_write failed");*/
  9118. WOLFSSL_RETURN_FROM_THREAD(0);
  9119. }
  9120. #ifdef WOLFSSL_TIRTOS
  9121. Task_yield();
  9122. #endif
  9123. ((func_args*)args)->return_code = TEST_SUCCESS;
  9124. done:
  9125. wolfSSL_shutdown(ssl);
  9126. wolfSSL_free(ssl);
  9127. wolfSSL_CTX_free(ctx);
  9128. CloseSocket(clientfd);
  9129. #ifdef WOLFSSL_TIRTOS
  9130. fdCloseSession(Task_self());
  9131. #endif
  9132. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9133. && defined(HAVE_THREAD_LS)
  9134. wc_ecc_fp_free(); /* free per thread cache */
  9135. #endif
  9136. #if defined(HAVE_SESSION_TICKET) && \
  9137. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9138. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9139. OpenSSLTicketCleanup();
  9140. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9141. TicketCleanup();
  9142. #endif
  9143. #endif
  9144. WOLFSSL_RETURN_FROM_THREAD(0);
  9145. }
  9146. static void load_tls12_canned_server(WOLFSSL* ssl)
  9147. {
  9148. int clientfd = wolfSSL_get_fd(ssl);
  9149. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9150. sizeof(canned_server_session)), sizeof(canned_server_session));
  9151. wolfSSL_set_fd(ssl, clientfd);
  9152. }
  9153. #ifdef WOLFSSL_TLS13
  9154. static void load_tls13_canned_server(WOLFSSL* ssl)
  9155. {
  9156. int clientfd = wolfSSL_get_fd(ssl);
  9157. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9158. sizeof(canned_server_tls13_session)),
  9159. sizeof(canned_server_tls13_session));
  9160. wolfSSL_set_fd(ssl, clientfd);
  9161. }
  9162. #endif
  9163. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9164. static int test_wolfSSL_tls_export_run(int v)
  9165. {
  9166. EXPECT_DECLS;
  9167. SOCKET_T sockfd = 0;
  9168. WOLFSSL_CTX* ctx = 0;
  9169. WOLFSSL* ssl = 0;
  9170. char msg[64] = "hello wolfssl!";
  9171. char reply[1024];
  9172. word32 replySz;
  9173. int msgSz = (int)XSTRLEN(msg);
  9174. const byte* clientSession = NULL;
  9175. int clientSessionSz = 0;
  9176. tcp_ready ready;
  9177. func_args server_args;
  9178. THREAD_TYPE serverThread;
  9179. callback_functions server_cbf;
  9180. #ifdef WOLFSSL_TIRTOS
  9181. fdOpenSession(Task_self());
  9182. #endif
  9183. InitTcpReady(&ready);
  9184. #if defined(USE_WINDOWS_API)
  9185. /* use RNG to get random port if using windows */
  9186. ready.port = GetRandomPort();
  9187. #endif
  9188. XMEMSET(&server_args, 0, sizeof(func_args));
  9189. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9190. switch (v) {
  9191. case WOLFSSL_TLSV1_2:
  9192. server_cbf.method = wolfTLSv1_2_server_method;
  9193. server_cbf.ssl_ready = load_tls12_canned_server;
  9194. /* setup the client side */
  9195. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9196. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9197. clientSession = canned_client_session;
  9198. clientSessionSz = sizeof(canned_client_session);
  9199. break;
  9200. #ifdef WOLFSSL_TLS13
  9201. case WOLFSSL_TLSV1_3:
  9202. server_cbf.method = wolfTLSv1_3_server_method;
  9203. server_cbf.ssl_ready = load_tls13_canned_server;
  9204. /* setup the client side */
  9205. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9206. clientSession = canned_client_tls13_session;
  9207. clientSessionSz = sizeof(canned_client_tls13_session);
  9208. break;
  9209. #endif
  9210. }
  9211. server_args.callbacks = &server_cbf;
  9212. server_args.signal = &ready;
  9213. start_thread(tls_export_server, &server_args, &serverThread);
  9214. wait_tcp_ready(&server_args);
  9215. #ifdef WOLFSSL_TIRTOS
  9216. fdOpenSession(Task_self());
  9217. #endif
  9218. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9219. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9220. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9221. clientSessionSz);
  9222. replySz = sizeof(reply);
  9223. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9224. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9225. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9226. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9227. #endif
  9228. wolfSSL_set_fd(ssl, sockfd);
  9229. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9230. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9231. wolfSSL_free(ssl);
  9232. wolfSSL_CTX_free(ctx);
  9233. CloseSocket(sockfd);
  9234. #ifdef WOLFSSL_TIRTOS
  9235. fdCloseSession(Task_self());
  9236. #endif
  9237. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9238. && defined(HAVE_THREAD_LS)
  9239. wc_ecc_fp_free(); /* free per thread cache */
  9240. #endif
  9241. join_thread(serverThread);
  9242. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9243. FreeTcpReady(&ready);
  9244. #ifdef WOLFSSL_TIRTOS
  9245. fdOpenSession(Task_self());
  9246. #endif
  9247. return EXPECT_RESULT();
  9248. }
  9249. #endif
  9250. static int test_wolfSSL_tls_export(void)
  9251. {
  9252. int res = TEST_SKIPPED;
  9253. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9254. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9255. #ifdef WOLFSSL_TLS13
  9256. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9257. #endif
  9258. res = TEST_RES_CHECK(1);
  9259. #endif
  9260. return res;
  9261. }
  9262. /*----------------------------------------------------------------------------*
  9263. | TLS extensions tests
  9264. *----------------------------------------------------------------------------*/
  9265. #ifdef ENABLE_TLS_CALLBACK_TEST
  9266. /* Connection test runner - generic */
  9267. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9268. callback_functions* server_callbacks)
  9269. {
  9270. tcp_ready ready;
  9271. func_args client_args;
  9272. func_args server_args;
  9273. THREAD_TYPE serverThread;
  9274. XMEMSET(&client_args, 0, sizeof(func_args));
  9275. XMEMSET(&server_args, 0, sizeof(func_args));
  9276. StartTCP();
  9277. client_args.callbacks = client_callbacks;
  9278. server_args.callbacks = server_callbacks;
  9279. #ifdef WOLFSSL_TIRTOS
  9280. fdOpenSession(Task_self());
  9281. #endif
  9282. /* RUN Server side */
  9283. InitTcpReady(&ready);
  9284. #if defined(USE_WINDOWS_API)
  9285. /* use RNG to get random port if using windows */
  9286. ready.port = GetRandomPort();
  9287. #endif
  9288. server_args.signal = &ready;
  9289. client_args.signal = &ready;
  9290. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9291. wait_tcp_ready(&server_args);
  9292. /* RUN Client side */
  9293. run_wolfssl_client(&client_args);
  9294. join_thread(serverThread);
  9295. FreeTcpReady(&ready);
  9296. #ifdef WOLFSSL_TIRTOS
  9297. fdCloseSession(Task_self());
  9298. #endif
  9299. client_callbacks->return_code = client_args.return_code;
  9300. server_callbacks->return_code = server_args.return_code;
  9301. }
  9302. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9303. #ifdef HAVE_SNI
  9304. static int test_wolfSSL_UseSNI_params(void)
  9305. {
  9306. EXPECT_DECLS;
  9307. #if !defined(NO_WOLFSSL_CLIENT)
  9308. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9309. WOLFSSL *ssl = wolfSSL_new(ctx);
  9310. ExpectNotNull(ctx);
  9311. ExpectNotNull(ssl);
  9312. /* invalid [ctx|ssl] */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9315. /* invalid type */
  9316. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9317. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9318. /* invalid data */
  9319. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9320. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9321. /* success case */
  9322. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9323. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9324. wolfSSL_free(ssl);
  9325. wolfSSL_CTX_free(ctx);
  9326. #endif /* !NO_WOLFSSL_CLIENT */
  9327. return EXPECT_RESULT();
  9328. }
  9329. /* BEGIN of connection tests callbacks */
  9330. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9331. {
  9332. AssertIntEQ(WOLFSSL_SUCCESS,
  9333. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9334. }
  9335. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9336. {
  9337. AssertIntEQ(WOLFSSL_SUCCESS,
  9338. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9339. }
  9340. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9341. {
  9342. AssertIntEQ(WOLFSSL_SUCCESS,
  9343. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9344. }
  9345. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9346. {
  9347. use_SNI_at_ssl(ssl);
  9348. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9349. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9350. }
  9351. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9352. {
  9353. use_SNI_at_ssl(ssl);
  9354. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9355. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9356. }
  9357. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9358. {
  9359. use_SNI_at_ctx(ctx);
  9360. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9361. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9362. }
  9363. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9364. {
  9365. use_SNI_at_ssl(ssl);
  9366. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9367. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9368. }
  9369. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9370. {
  9371. use_SNI_at_ctx(ctx);
  9372. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9373. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9374. }
  9375. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9376. {
  9377. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9378. }
  9379. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9380. {
  9381. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9382. }
  9383. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9384. {
  9385. byte type = WOLFSSL_SNI_HOST_NAME;
  9386. void* request = (void*) &type; /* to be overwritten */
  9387. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9388. AssertNotNull(request);
  9389. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9390. AssertNull(request);
  9391. }
  9392. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9393. {
  9394. byte type = WOLFSSL_SNI_HOST_NAME;
  9395. void* request = NULL;
  9396. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9397. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9398. AssertNotNull(request);
  9399. AssertStrEQ("www.wolfssl.com", (char*)request);
  9400. }
  9401. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9402. {
  9403. byte type = WOLFSSL_SNI_HOST_NAME;
  9404. void* request = NULL;
  9405. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9406. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9407. AssertNotNull(request);
  9408. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9409. }
  9410. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9411. {
  9412. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9413. }
  9414. /* END of connection tests callbacks */
  9415. static int test_wolfSSL_UseSNI_connection(void)
  9416. {
  9417. int res = TEST_SKIPPED;
  9418. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9419. callback_functions client_cb;
  9420. callback_functions server_cb;
  9421. size_t i;
  9422. #ifdef WOLFSSL_STATIC_MEMORY
  9423. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9424. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9425. #endif
  9426. struct {
  9427. method_provider client_meth;
  9428. method_provider server_meth;
  9429. #ifdef WOLFSSL_STATIC_MEMORY
  9430. wolfSSL_method_func client_meth_ex;
  9431. wolfSSL_method_func server_meth_ex;
  9432. #endif
  9433. } methods[] = {
  9434. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9435. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9436. #ifdef WOLFSSL_STATIC_MEMORY
  9437. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9438. #endif
  9439. },
  9440. #endif
  9441. #ifndef WOLFSSL_NO_TLS12
  9442. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9443. #ifdef WOLFSSL_STATIC_MEMORY
  9444. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9445. #endif
  9446. },
  9447. #endif
  9448. #ifdef WOLFSSL_TLS13
  9449. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9450. #ifdef WOLFSSL_STATIC_MEMORY
  9451. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9452. #endif
  9453. },
  9454. #endif
  9455. };
  9456. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9457. for (i = 0; i < methodsSz; i++) {
  9458. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9459. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9460. client_cb.method = methods[i].client_meth;
  9461. server_cb.method = methods[i].server_meth;
  9462. client_cb.devId = testDevId;
  9463. server_cb.devId = testDevId;
  9464. #ifdef WOLFSSL_STATIC_MEMORY
  9465. client_cb.method_ex = methods[i].client_meth_ex;
  9466. server_cb.method_ex = methods[i].server_meth_ex;
  9467. client_cb.mem = cliMem;
  9468. client_cb.memSz = (word32)sizeof(cliMem);
  9469. server_cb.mem = svrMem;
  9470. server_cb.memSz = (word32)sizeof(svrMem);;
  9471. #endif
  9472. /* success case at ctx */
  9473. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9474. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9475. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9476. test_wolfSSL_client_server(&client_cb, &server_cb);
  9477. /* success case at ssl */
  9478. fprintf(stderr, "\tsuccess case at ssl\n");
  9479. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9480. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9481. test_wolfSSL_client_server(&client_cb, &server_cb);
  9482. /* default mismatch behavior */
  9483. fprintf(stderr, "\tdefault mismatch behavior\n");
  9484. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9485. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9486. test_wolfSSL_client_server(&client_cb, &server_cb);
  9487. /* continue on mismatch */
  9488. fprintf(stderr, "\tcontinue on mismatch\n");
  9489. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9490. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9491. test_wolfSSL_client_server(&client_cb, &server_cb);
  9492. /* fake answer on mismatch */
  9493. fprintf(stderr, "\tfake answer on mismatch\n");
  9494. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9495. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9496. test_wolfSSL_client_server(&client_cb, &server_cb);
  9497. /* sni abort - success */
  9498. fprintf(stderr, "\tsni abort - success\n");
  9499. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9500. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9501. test_wolfSSL_client_server(&client_cb, &server_cb);
  9502. /* sni abort - abort when absent (ctx) */
  9503. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9504. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9505. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9506. test_wolfSSL_client_server(&client_cb, &server_cb);
  9507. /* sni abort - abort when absent (ssl) */
  9508. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9509. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9510. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9511. test_wolfSSL_client_server(&client_cb, &server_cb);
  9512. /* sni abort - success when overwritten */
  9513. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9514. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9515. 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;
  9516. test_wolfSSL_client_server(&client_cb, &server_cb);
  9517. /* sni abort - success when allowing mismatches */
  9518. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9519. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9520. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9521. test_wolfSSL_client_server(&client_cb, &server_cb);
  9522. }
  9523. res = TEST_RES_CHECK(1);
  9524. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9525. return res;
  9526. }
  9527. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9528. {
  9529. EXPECT_DECLS;
  9530. byte buff[] = { /* www.paypal.com */
  9531. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9532. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9533. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9534. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9535. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9536. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9537. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9538. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9539. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9540. };
  9541. byte buff2[] = { /* api.textmate.org */
  9542. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9543. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9544. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9545. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9546. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9547. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9548. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9549. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9550. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9551. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9552. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9553. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9554. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9555. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9556. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9557. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9558. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9559. };
  9560. byte buff3[] = { /* no sni extension */
  9561. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9562. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9563. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9564. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9565. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9566. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9567. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9568. };
  9569. byte buff4[] = { /* last extension has zero size */
  9570. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9571. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9572. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9573. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9574. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9575. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9576. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9577. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9578. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9579. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9580. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9581. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9582. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9583. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9584. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9585. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9586. 0x12, 0x00, 0x00
  9587. };
  9588. byte buff5[] = { /* SSL v2.0 client hello */
  9589. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9590. /* dummy bytes below, just to pass size check */
  9591. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9592. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9593. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9594. };
  9595. byte result[32] = {0};
  9596. word32 length = 32;
  9597. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9598. 0, result, &length));
  9599. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9600. 0, result, &length));
  9601. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9602. 1, result, &length));
  9603. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9604. 0, result, &length));
  9605. buff[0] = 0x16;
  9606. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9607. 0, result, &length));
  9608. buff[1] = 0x03;
  9609. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9610. sizeof(buff), 0, result, &length));
  9611. buff[2] = 0x03;
  9612. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9613. sizeof(buff), 0, result, &length));
  9614. buff[4] = 0x64;
  9615. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9616. 0, result, &length));
  9617. if (EXPECT_SUCCESS())
  9618. result[length] = 0;
  9619. ExpectStrEQ("www.paypal.com", (const char*) result);
  9620. length = 32;
  9621. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9622. 0, result, &length));
  9623. if (EXPECT_SUCCESS())
  9624. result[length] = 0;
  9625. ExpectStrEQ("api.textmate.org", (const char*) result);
  9626. /* SSL v2.0 tests */
  9627. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9628. sizeof(buff5), 0, result, &length));
  9629. buff5[2] = 0x02;
  9630. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9631. sizeof(buff5), 0, result, &length));
  9632. buff5[2] = 0x01; buff5[6] = 0x08;
  9633. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9634. sizeof(buff5), 0, result, &length));
  9635. buff5[6] = 0x09; buff5[8] = 0x01;
  9636. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9637. sizeof(buff5), 0, result, &length));
  9638. return EXPECT_RESULT();
  9639. }
  9640. #endif /* HAVE_SNI */
  9641. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9642. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9643. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9644. /* Dummy peer functions to satisfy the exporter/importer */
  9645. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9646. int* ipSz, unsigned short* port, int* fam)
  9647. {
  9648. (void)ssl;
  9649. ip[0] = -1;
  9650. *ipSz = 1;
  9651. *port = 1;
  9652. *fam = 2;
  9653. return 1;
  9654. }
  9655. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9656. int ipSz, unsigned short port, int fam)
  9657. {
  9658. (void)ssl;
  9659. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9660. return 0;
  9661. return 1;
  9662. }
  9663. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9664. WOLFSSL **ssl)
  9665. {
  9666. EXPECT_DECLS;
  9667. unsigned char* sessionBuf = NULL;
  9668. unsigned int sessionSz = 0;
  9669. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9670. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9671. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9672. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9673. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9674. ExpectNotNull(sessionBuf =
  9675. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9676. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9677. wolfSSL_free(*ssl);
  9678. *ssl = NULL;
  9679. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9680. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9681. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9682. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9683. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9684. return EXPECT_RESULT();
  9685. }
  9686. #endif
  9687. static int test_wolfSSL_dtls_export_peers(void)
  9688. {
  9689. EXPECT_DECLS;
  9690. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9691. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9692. test_ssl_cbf client_cbf;
  9693. test_ssl_cbf server_cbf;
  9694. size_t i, j;
  9695. struct test_params {
  9696. method_provider client_meth;
  9697. method_provider server_meth;
  9698. const char* dtls_version;
  9699. } params[] = {
  9700. #ifndef NO_OLD_TLS
  9701. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9702. #endif
  9703. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9704. /* TODO DTLS 1.3 exporting not supported
  9705. #ifdef WOLFSSL_DTLS13
  9706. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9707. #endif
  9708. */
  9709. };
  9710. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9711. for (j = 0; j <= 0b11; j++) {
  9712. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9713. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9714. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9715. client_cbf.method = params[i].client_meth;
  9716. server_cbf.method = params[i].server_meth;
  9717. if (j & 0b01) {
  9718. client_cbf.on_handshake =
  9719. test_wolfSSL_dtls_export_peers_on_handshake;
  9720. printf(" With client export;");
  9721. }
  9722. if (j & 0b10) {
  9723. server_cbf.on_handshake =
  9724. test_wolfSSL_dtls_export_peers_on_handshake;
  9725. printf(" With server export;");
  9726. }
  9727. printf("\n");
  9728. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9729. &server_cbf, NULL), TEST_SUCCESS);
  9730. if (!EXPECT_SUCCESS())
  9731. break;
  9732. }
  9733. }
  9734. #endif
  9735. return EXPECT_RESULT();
  9736. }
  9737. static int test_wolfSSL_UseTrustedCA(void)
  9738. {
  9739. EXPECT_DECLS;
  9740. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9741. && !defined(NO_RSA)
  9742. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9743. WOLFSSL_CTX *ctx = NULL;
  9744. WOLFSSL *ssl = NULL;
  9745. byte id[20];
  9746. #ifndef NO_WOLFSSL_SERVER
  9747. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9748. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9749. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9750. #else
  9751. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9752. #endif
  9753. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9754. XMEMSET(id, 0, sizeof(id));
  9755. /* error cases */
  9756. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9757. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9758. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9759. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9760. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9761. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9762. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9763. #ifdef NO_SHA
  9764. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9765. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9766. #endif
  9767. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9768. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9769. /* success cases */
  9770. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9771. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9772. #ifndef NO_SHA
  9773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9774. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9775. #endif
  9776. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9777. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9778. wolfSSL_free(ssl);
  9779. wolfSSL_CTX_free(ctx);
  9780. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9781. #endif /* HAVE_TRUSTED_CA */
  9782. return EXPECT_RESULT();
  9783. }
  9784. static int test_wolfSSL_UseMaxFragment(void)
  9785. {
  9786. EXPECT_DECLS;
  9787. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9788. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9789. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9790. #ifndef NO_WOLFSSL_SERVER
  9791. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9792. #else
  9793. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9794. #endif
  9795. WOLFSSL *ssl = NULL;
  9796. #ifdef OPENSSL_EXTRA
  9797. int (*UseMaxFragment)(SSL *s, unsigned char mode);
  9798. int (*CTX_UseMaxFragment)(SSL_CTX *c, unsigned char mode);
  9799. #else
  9800. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9801. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9802. #endif
  9803. #ifndef NO_WOLFSSL_SERVER
  9804. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9805. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9806. #endif
  9807. ExpectNotNull(ctx);
  9808. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9809. #ifdef OPENSSL_EXTRA
  9810. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9811. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9812. #else
  9813. UseMaxFragment = wolfSSL_UseMaxFragment;
  9814. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9815. #endif
  9816. /* error cases */
  9817. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9818. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9819. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9820. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9821. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9822. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9823. /* success case */
  9824. #ifdef OPENSSL_EXTRA
  9825. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9826. #else
  9827. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9828. #endif
  9829. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9830. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9831. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9832. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9833. #ifdef OPENSSL_EXTRA
  9834. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9835. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9836. #else
  9837. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9838. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9839. #endif
  9840. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9841. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9842. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9843. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9844. #ifdef OPENSSL_EXTRA
  9845. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9846. #else
  9847. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9848. #endif
  9849. wolfSSL_free(ssl);
  9850. wolfSSL_CTX_free(ctx);
  9851. #if defined(OPENSSL_EXTRA) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  9852. /* check negotiated max fragment size */
  9853. {
  9854. WOLFSSL *ssl_c = NULL;
  9855. WOLFSSL *ssl_s = NULL;
  9856. struct test_memio_ctx test_ctx;
  9857. WOLFSSL_CTX *ctx_c = NULL;
  9858. WOLFSSL_CTX *ctx_s = NULL;
  9859. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  9860. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  9861. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  9862. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c, WOLFSSL_MFL_2_8),
  9863. WOLFSSL_SUCCESS);
  9864. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  9865. ExpectIntEQ(SSL_SESSION_get_max_fragment_length(
  9866. wolfSSL_get_session(ssl_c)), WOLFSSL_MFL_2_8);
  9867. wolfSSL_free(ssl_c);
  9868. wolfSSL_free(ssl_s);
  9869. wolfSSL_CTX_free(ctx_c);
  9870. wolfSSL_CTX_free(ctx_s);
  9871. }
  9872. #endif
  9873. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9874. #endif
  9875. return EXPECT_RESULT();
  9876. }
  9877. static int test_wolfSSL_UseTruncatedHMAC(void)
  9878. {
  9879. EXPECT_DECLS;
  9880. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9881. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9882. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9883. #ifndef NO_WOLFSSL_SERVER
  9884. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9885. #else
  9886. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9887. #endif
  9888. WOLFSSL *ssl = NULL;
  9889. ExpectNotNull(ctx);
  9890. #ifndef NO_WOLFSSL_SERVER
  9891. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9892. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9893. #endif
  9894. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9895. /* error cases */
  9896. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9897. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9898. /* success case */
  9899. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9900. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9901. wolfSSL_free(ssl);
  9902. wolfSSL_CTX_free(ctx);
  9903. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9904. #endif
  9905. return EXPECT_RESULT();
  9906. }
  9907. static int test_wolfSSL_UseSupportedCurve(void)
  9908. {
  9909. EXPECT_DECLS;
  9910. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9911. !defined(NO_TLS)
  9912. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9913. WOLFSSL *ssl = wolfSSL_new(ctx);
  9914. ExpectNotNull(ctx);
  9915. ExpectNotNull(ssl);
  9916. /* error cases */
  9917. ExpectIntNE(WOLFSSL_SUCCESS,
  9918. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9919. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9920. ExpectIntNE(WOLFSSL_SUCCESS,
  9921. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9922. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9923. /* success case */
  9924. ExpectIntEQ(WOLFSSL_SUCCESS,
  9925. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9926. ExpectIntEQ(WOLFSSL_SUCCESS,
  9927. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9928. wolfSSL_free(ssl);
  9929. wolfSSL_CTX_free(ctx);
  9930. #endif
  9931. return EXPECT_RESULT();
  9932. }
  9933. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9934. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9935. {
  9936. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9937. }
  9938. static void use_ALPN_all(WOLFSSL* ssl)
  9939. {
  9940. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9941. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9942. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9943. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9944. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9945. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9946. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9947. }
  9948. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9949. {
  9950. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9951. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9952. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9953. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9954. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9955. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9956. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9957. }
  9958. static void use_ALPN_one(WOLFSSL* ssl)
  9959. {
  9960. /* spdy/2 */
  9961. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9962. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9963. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9964. }
  9965. static void use_ALPN_unknown(WOLFSSL* ssl)
  9966. {
  9967. /* http/2.0 */
  9968. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9969. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9970. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9971. }
  9972. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9973. {
  9974. /* http/2.0 */
  9975. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9976. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9977. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9978. }
  9979. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9980. {
  9981. /* spdy/3 */
  9982. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9983. char *proto = NULL;
  9984. word16 protoSz = 0;
  9985. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9986. /* check value */
  9987. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9988. if (proto) {
  9989. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9990. }
  9991. }
  9992. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9993. {
  9994. char *proto = NULL;
  9995. word16 protoSz = 0;
  9996. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9997. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9998. /* check value */
  9999. AssertIntEQ(1, (0 == protoSz));
  10000. AssertIntEQ(1, (NULL == proto));
  10001. }
  10002. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  10003. {
  10004. /* http/1.1 */
  10005. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10006. char *proto;
  10007. word16 protoSz = 0;
  10008. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10009. /* check value */
  10010. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10011. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10012. }
  10013. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  10014. {
  10015. /* spdy/2 */
  10016. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10017. char *proto;
  10018. word16 protoSz = 0;
  10019. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  10020. /* check value */
  10021. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10022. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10023. }
  10024. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10025. {
  10026. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10027. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10028. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10029. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10030. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10031. char *clist = NULL;
  10032. word16 clistSz = 0;
  10033. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10034. &clistSz));
  10035. /* check value */
  10036. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10037. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10038. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10039. }
  10040. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10041. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10042. /* ALPN select callback, success with spdy/2 */
  10043. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10044. unsigned char *outlen, const unsigned char *in,
  10045. unsigned int inlen, void *arg)
  10046. {
  10047. /* spdy/2 */
  10048. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10049. (void)ssl;
  10050. (void)arg;
  10051. /* adding +1 since LEN byte comes first */
  10052. if (inlen < sizeof(proto) + 1) {
  10053. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10054. }
  10055. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10056. *out = in + 1;
  10057. *outlen = (unsigned char)sizeof(proto);
  10058. return SSL_TLSEXT_ERR_OK;
  10059. }
  10060. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10061. }
  10062. /* ALPN select callback, force failure */
  10063. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10064. unsigned char *outlen, const unsigned char *in,
  10065. unsigned int inlen, void *arg)
  10066. {
  10067. (void)ssl;
  10068. (void)out;
  10069. (void)outlen;
  10070. (void)in;
  10071. (void)inlen;
  10072. (void)arg;
  10073. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10074. }
  10075. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10076. {
  10077. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10078. }
  10079. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10080. {
  10081. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10082. }
  10083. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10084. static int test_wolfSSL_UseALPN_connection(void)
  10085. {
  10086. int res = TEST_SKIPPED;
  10087. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10088. callback_functions client_cb;
  10089. callback_functions server_cb;
  10090. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10091. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10092. client_cb.method = wolfSSLv23_client_method;
  10093. server_cb.method = wolfSSLv23_server_method;
  10094. client_cb.devId = testDevId;
  10095. server_cb.devId = testDevId;
  10096. /* success case same list */
  10097. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10098. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10099. test_wolfSSL_client_server(&client_cb, &server_cb);
  10100. /* success case only one for server */
  10101. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10102. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10103. test_wolfSSL_client_server(&client_cb, &server_cb);
  10104. /* success case only one for client */
  10105. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10106. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10107. test_wolfSSL_client_server(&client_cb, &server_cb);
  10108. /* success case none for client */
  10109. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10110. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10111. test_wolfSSL_client_server(&client_cb, &server_cb);
  10112. /* success case mismatch behavior but option 'continue' set */
  10113. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10114. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10115. test_wolfSSL_client_server(&client_cb, &server_cb);
  10116. /* success case read protocol send by client */
  10117. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10118. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10119. test_wolfSSL_client_server(&client_cb, &server_cb);
  10120. /* mismatch behavior with same list
  10121. * the first and only this one must be taken */
  10122. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10123. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10124. test_wolfSSL_client_server(&client_cb, &server_cb);
  10125. /* default mismatch behavior */
  10126. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10127. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10128. test_wolfSSL_client_server(&client_cb, &server_cb);
  10129. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10130. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10131. /* WOLFSSL-level ALPN select callback tests */
  10132. /* Callback: success (one protocol, spdy/2) */
  10133. client_cb.ctx_ready = NULL;
  10134. client_cb.ssl_ready = use_ALPN_one;
  10135. client_cb.on_result = verify_ALPN_matching_spdy2;
  10136. server_cb.ctx_ready = NULL;
  10137. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10138. server_cb.on_result = verify_ALPN_matching_spdy2;
  10139. test_wolfSSL_client_server(&client_cb, &server_cb);
  10140. /* Callback: failure (one client protocol, spdy/2) */
  10141. client_cb.ctx_ready = NULL;
  10142. client_cb.ssl_ready = use_ALPN_one;
  10143. client_cb.on_result = NULL;
  10144. server_cb.ctx_ready = NULL;
  10145. server_cb.ssl_ready = use_ALPN_failure_callback;
  10146. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10147. test_wolfSSL_client_server(&client_cb, &server_cb);
  10148. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10149. res = TEST_RES_CHECK(1);
  10150. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10151. return res;
  10152. }
  10153. static int test_wolfSSL_UseALPN_params(void)
  10154. {
  10155. EXPECT_DECLS;
  10156. #ifndef NO_WOLFSSL_CLIENT
  10157. /* "http/1.1" */
  10158. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10159. /* "spdy/1" */
  10160. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10161. /* "spdy/2" */
  10162. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10163. /* "spdy/3" */
  10164. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10165. char buff[256];
  10166. word32 idx;
  10167. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10168. WOLFSSL *ssl = wolfSSL_new(ctx);
  10169. ExpectNotNull(ctx);
  10170. ExpectNotNull(ssl);
  10171. /* error cases */
  10172. ExpectIntNE(WOLFSSL_SUCCESS,
  10173. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10174. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10175. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10176. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10177. /* success case */
  10178. /* http1 only */
  10179. ExpectIntEQ(WOLFSSL_SUCCESS,
  10180. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10181. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10182. /* http1, spdy1 */
  10183. XMEMCPY(buff, http1, sizeof(http1));
  10184. idx = sizeof(http1);
  10185. buff[idx++] = ',';
  10186. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10187. idx += sizeof(spdy1);
  10188. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10189. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10190. /* http1, spdy2, spdy1 */
  10191. XMEMCPY(buff, http1, sizeof(http1));
  10192. idx = sizeof(http1);
  10193. buff[idx++] = ',';
  10194. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10195. idx += sizeof(spdy2);
  10196. buff[idx++] = ',';
  10197. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10198. idx += sizeof(spdy1);
  10199. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10200. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10201. /* spdy3, http1, spdy2, spdy1 */
  10202. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10203. idx = sizeof(spdy3);
  10204. buff[idx++] = ',';
  10205. XMEMCPY(buff+idx, http1, sizeof(http1));
  10206. idx += sizeof(http1);
  10207. buff[idx++] = ',';
  10208. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10209. idx += sizeof(spdy2);
  10210. buff[idx++] = ',';
  10211. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10212. idx += sizeof(spdy1);
  10213. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10214. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10215. wolfSSL_free(ssl);
  10216. wolfSSL_CTX_free(ctx);
  10217. #endif
  10218. return EXPECT_RESULT();
  10219. }
  10220. #endif /* HAVE_ALPN */
  10221. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10222. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10223. {
  10224. unsigned char p[] = {
  10225. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10226. 6, 's', 'p', 'd', 'y', '/', '2',
  10227. 6, 's', 'p', 'd', 'y', '/', '1',
  10228. };
  10229. unsigned char p_len = sizeof(p);
  10230. int ret;
  10231. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10232. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10233. AssertIntEQ(ret, 0);
  10234. #else
  10235. AssertIntEQ(ret, SSL_SUCCESS);
  10236. #endif
  10237. }
  10238. static void set_alpn_protos(SSL* ssl)
  10239. {
  10240. unsigned char p[] = {
  10241. 6, 's', 'p', 'd', 'y', '/', '3',
  10242. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10243. 6, 's', 'p', 'd', 'y', '/', '2',
  10244. 6, 's', 'p', 'd', 'y', '/', '1',
  10245. };
  10246. unsigned char p_len = sizeof(p);
  10247. int ret;
  10248. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10249. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10250. AssertIntEQ(ret, 0);
  10251. #else
  10252. AssertIntEQ(ret, SSL_SUCCESS);
  10253. #endif
  10254. }
  10255. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10256. {
  10257. /* "spdy/3" */
  10258. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10259. const unsigned char *proto;
  10260. unsigned int protoSz = 0;
  10261. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10262. /* check value */
  10263. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10264. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10265. }
  10266. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10267. {
  10268. /* "http/1.1" */
  10269. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10270. const unsigned char *proto;
  10271. unsigned int protoSz = 0;
  10272. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10273. /* check value */
  10274. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10275. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10276. }
  10277. static int test_wolfSSL_set_alpn_protos(void)
  10278. {
  10279. int res = TEST_SKIPPED;
  10280. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10281. callback_functions client_cb;
  10282. callback_functions server_cb;
  10283. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10284. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10285. client_cb.method = wolfSSLv23_client_method;
  10286. server_cb.method = wolfSSLv23_server_method;
  10287. client_cb.devId = testDevId;
  10288. server_cb.devId = testDevId;
  10289. /* use CTX_alpn_protos */
  10290. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10291. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10292. test_wolfSSL_client_server(&client_cb, &server_cb);
  10293. /* use set_alpn_protos */
  10294. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10295. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10296. test_wolfSSL_client_server(&client_cb, &server_cb);
  10297. res = TEST_SUCCESS;
  10298. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10299. return res;
  10300. }
  10301. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10302. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10303. {
  10304. EXPECT_DECLS;
  10305. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10306. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10307. WOLFSSL *ssl = wolfSSL_new(ctx);
  10308. ExpectNotNull(ctx);
  10309. ExpectNotNull(ssl);
  10310. /* error cases */
  10311. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10312. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10313. /* success cases */
  10314. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10315. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10316. wolfSSL_free(ssl);
  10317. wolfSSL_CTX_free(ctx);
  10318. #endif
  10319. return EXPECT_RESULT();
  10320. }
  10321. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10322. {
  10323. EXPECT_DECLS;
  10324. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10325. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10326. WOLFSSL *ssl = wolfSSL_new(ctx);
  10327. ExpectNotNull(ctx);
  10328. ExpectNotNull(ssl);
  10329. /* error cases */
  10330. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10331. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10332. /* success cases */
  10333. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10334. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10335. wolfSSL_free(ssl);
  10336. wolfSSL_CTX_free(ctx);
  10337. #endif
  10338. return EXPECT_RESULT();
  10339. }
  10340. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10341. static int test_wolfSSL_SCR_Reconnect(void)
  10342. {
  10343. EXPECT_DECLS;
  10344. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10345. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10346. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10347. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10348. struct test_memio_ctx test_ctx;
  10349. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10350. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10351. byte data;
  10352. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10353. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10354. test_ctx.s_ciphers =
  10355. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10356. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10357. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10358. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10359. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10360. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10361. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10362. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10363. /* WOLFSSL_FATAL_ERROR since it will block */
  10364. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10365. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10366. WOLFSSL_ERROR_WANT_READ);
  10367. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10368. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10369. WOLFSSL_ERROR_WANT_READ);
  10370. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10371. wolfSSL_free(ssl_c);
  10372. ssl_c = NULL;
  10373. wolfSSL_free(ssl_s);
  10374. ssl_s = NULL;
  10375. wolfSSL_CTX_free(ctx_c);
  10376. ctx_c = NULL;
  10377. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10378. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10379. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10380. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10381. wolfSSL_free(ssl_s);
  10382. wolfSSL_free(ssl_c);
  10383. wolfSSL_CTX_free(ctx_s);
  10384. wolfSSL_CTX_free(ctx_c);
  10385. #endif
  10386. return EXPECT_RESULT();
  10387. }
  10388. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10389. (!defined(NO_RSA) || defined(HAVE_ECC))
  10390. /* Called when writing. */
  10391. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10392. {
  10393. (void)ssl;
  10394. (void)buf;
  10395. (void)sz;
  10396. (void)ctx;
  10397. /* Force error return from wolfSSL_accept_TLSv13(). */
  10398. return WANT_WRITE;
  10399. }
  10400. /* Called when reading. */
  10401. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10402. {
  10403. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10404. int len = (int)msg->length;
  10405. (void)ssl;
  10406. (void)sz;
  10407. /* Pass back as much of message as will fit in buffer. */
  10408. if (len > sz)
  10409. len = sz;
  10410. XMEMCPY(buf, msg->buffer, len);
  10411. /* Move over returned data. */
  10412. msg->buffer += len;
  10413. msg->length -= len;
  10414. /* Amount actually copied. */
  10415. return len;
  10416. }
  10417. #endif
  10418. /* Test the detection of duplicate known TLS extensions.
  10419. * Specifically in a ClientHello.
  10420. */
  10421. static int test_tls_ext_duplicate(void)
  10422. {
  10423. EXPECT_DECLS;
  10424. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10425. !defined(NO_FILESYSTEM)
  10426. const unsigned char clientHelloDupTlsExt[] = {
  10427. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10428. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10429. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10430. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10431. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10432. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10433. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10434. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10435. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10436. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10437. 0x00, 0x9e, 0x01, 0x00,
  10438. /* Extensions - duplicate signature algorithms. */
  10439. 0x00, 0x19, 0x00, 0x0d,
  10440. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10441. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10442. /* Supported Versions extension for TLS 1.3. */
  10443. 0x00, 0x2b,
  10444. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10445. };
  10446. WOLFSSL_BUFFER_INFO msg;
  10447. const char* testCertFile;
  10448. const char* testKeyFile;
  10449. WOLFSSL_CTX *ctx = NULL;
  10450. WOLFSSL *ssl = NULL;
  10451. #ifndef NO_RSA
  10452. testCertFile = svrCertFile;
  10453. testKeyFile = svrKeyFile;
  10454. #elif defined(HAVE_ECC)
  10455. testCertFile = eccCertFile;
  10456. testKeyFile = eccKeyFile;
  10457. #endif
  10458. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10459. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10460. WOLFSSL_FILETYPE_PEM));
  10461. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10462. WOLFSSL_FILETYPE_PEM));
  10463. /* Read from 'msg'. */
  10464. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10465. /* No where to send to - dummy sender. */
  10466. wolfSSL_SetIOSend(ctx, DummySend);
  10467. ssl = wolfSSL_new(ctx);
  10468. ExpectNotNull(ssl);
  10469. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10470. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10471. wolfSSL_SetIOReadCtx(ssl, &msg);
  10472. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10473. /* can return duplicate ext error or socket error if the peer closed down
  10474. * while sending alert */
  10475. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10476. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10477. }
  10478. wolfSSL_free(ssl);
  10479. wolfSSL_CTX_free(ctx);
  10480. #endif
  10481. return EXPECT_RESULT();
  10482. }
  10483. /*----------------------------------------------------------------------------*
  10484. | X509 Tests
  10485. *----------------------------------------------------------------------------*/
  10486. static int test_wolfSSL_X509_NAME_get_entry(void)
  10487. {
  10488. EXPECT_DECLS;
  10489. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10490. #if defined(OPENSSL_ALL) || \
  10491. (defined(OPENSSL_EXTRA) && \
  10492. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10493. /* use openssl like name to test mapping */
  10494. X509_NAME_ENTRY* ne = NULL;
  10495. X509_NAME* name = NULL;
  10496. X509* x509 = NULL;
  10497. #ifndef NO_FILESYSTEM
  10498. ASN1_STRING* asn = NULL;
  10499. char* subCN = NULL;
  10500. #endif
  10501. int idx = 0;
  10502. ASN1_OBJECT *object = NULL;
  10503. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10504. defined(WOLFSSL_NGINX)
  10505. #ifndef NO_BIO
  10506. BIO* bio = NULL;
  10507. #endif
  10508. #endif
  10509. #ifndef NO_FILESYSTEM
  10510. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10511. WOLFSSL_FILETYPE_PEM));
  10512. ExpectNotNull(name = X509_get_subject_name(x509));
  10513. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10514. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10515. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10516. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10517. wolfSSL_FreeX509(x509);
  10518. x509 = NULL;
  10519. #endif
  10520. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10521. WOLFSSL_FILETYPE_PEM));
  10522. ExpectNotNull(name = X509_get_subject_name(x509));
  10523. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10524. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10525. defined(WOLFSSL_NGINX)
  10526. #ifndef NO_BIO
  10527. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10528. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10529. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10530. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10531. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10532. BIO_free(bio);
  10533. #endif
  10534. #endif
  10535. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10536. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10537. wolfSSL_FreeX509(x509);
  10538. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10539. #endif /* !NO_CERTS && !NO_RSA */
  10540. return EXPECT_RESULT();
  10541. }
  10542. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10543. static int test_wolfSSL_PKCS12(void)
  10544. {
  10545. EXPECT_DECLS;
  10546. /* .p12 file is encrypted with DES3 */
  10547. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10548. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10549. * Password Key
  10550. */
  10551. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10552. !defined(NO_STDIO_FILESYSTEM) && \
  10553. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10554. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10555. byte buf[6000];
  10556. char file[] = "./certs/test-servercert.p12";
  10557. char order[] = "./certs/ecc-rsa-server.p12";
  10558. #ifdef WC_RC2
  10559. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10560. #endif
  10561. char pass[] = "a password";
  10562. const char goodPsw[] = "wolfSSL test";
  10563. const char badPsw[] = "bad";
  10564. #ifdef HAVE_ECC
  10565. WOLFSSL_X509_NAME *subject = NULL;
  10566. WOLFSSL_X509 *x509 = NULL;
  10567. #endif
  10568. XFILE f = XBADFILE;
  10569. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10570. WOLFSSL_BIO *bio = NULL;
  10571. WOLFSSL_EVP_PKEY *pkey = NULL;
  10572. WC_PKCS12 *pkcs12 = NULL;
  10573. WC_PKCS12 *pkcs12_2 = NULL;
  10574. WOLFSSL_X509 *cert = NULL;
  10575. WOLFSSL_X509 *tmp = NULL;
  10576. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10577. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10578. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10579. WOLFSSL_CTX *ctx = NULL;
  10580. WOLFSSL *ssl = NULL;
  10581. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10582. #endif
  10583. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10584. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10585. if (f != XBADFILE) {
  10586. XFCLOSE(f);
  10587. f = XBADFILE;
  10588. }
  10589. goodPswLen = (int)XSTRLEN(goodPsw);
  10590. badPswLen = (int)XSTRLEN(badPsw);
  10591. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10592. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10593. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10594. ExpectNotNull(pkcs12);
  10595. BIO_free(bio);
  10596. bio = NULL;
  10597. /* check verify MAC directly */
  10598. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10599. /* check verify MAC fail case directly */
  10600. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10601. /* check verify MAC fail case */
  10602. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10603. ExpectNull(pkey);
  10604. ExpectNull(cert);
  10605. /* check parse with no extra certs kept */
  10606. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10607. 1);
  10608. ExpectNotNull(pkey);
  10609. ExpectNotNull(cert);
  10610. wolfSSL_EVP_PKEY_free(pkey);
  10611. pkey = NULL;
  10612. wolfSSL_X509_free(cert);
  10613. cert = NULL;
  10614. /* check parse with extra certs kept */
  10615. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10616. 1);
  10617. ExpectNotNull(pkey);
  10618. ExpectNotNull(cert);
  10619. ExpectNotNull(ca);
  10620. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10621. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10622. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10623. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10624. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10625. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10626. #else
  10627. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10628. #endif
  10629. /* Copy stack structure */
  10630. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10631. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10632. /* CTX now owns the tmp_ca stack structure */
  10633. tmp_ca = NULL;
  10634. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10635. ExpectNotNull(tmp_ca);
  10636. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10637. /* Check that the main cert is also set */
  10638. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10639. ExpectNotNull(ssl = SSL_new(ctx));
  10640. ExpectNotNull(SSL_get_certificate(ssl));
  10641. SSL_free(ssl);
  10642. SSL_CTX_free(ctx);
  10643. ctx = NULL;
  10644. #endif
  10645. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10646. /* should be 2 other certs on stack */
  10647. ExpectNotNull(tmp = sk_X509_pop(ca));
  10648. X509_free(tmp);
  10649. ExpectNotNull(tmp = sk_X509_pop(ca));
  10650. X509_free(tmp);
  10651. ExpectNull(sk_X509_pop(ca));
  10652. EVP_PKEY_free(pkey);
  10653. pkey = NULL;
  10654. X509_free(cert);
  10655. cert = NULL;
  10656. sk_X509_pop_free(ca, X509_free);
  10657. ca = NULL;
  10658. /* check PKCS12_create */
  10659. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10660. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10661. SSL_SUCCESS);
  10662. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10663. -1, -1, 100, -1, 0)));
  10664. EVP_PKEY_free(pkey);
  10665. pkey = NULL;
  10666. X509_free(cert);
  10667. cert = NULL;
  10668. sk_X509_pop_free(ca, NULL);
  10669. ca = NULL;
  10670. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10671. SSL_SUCCESS);
  10672. PKCS12_free(pkcs12_2);
  10673. pkcs12_2 = NULL;
  10674. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10675. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10676. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10677. 2000, 1, 0)));
  10678. EVP_PKEY_free(pkey);
  10679. pkey = NULL;
  10680. X509_free(cert);
  10681. cert = NULL;
  10682. sk_X509_pop_free(ca, NULL);
  10683. ca = NULL;
  10684. /* convert to DER then back and parse */
  10685. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10686. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10687. PKCS12_free(pkcs12_2);
  10688. pkcs12_2 = NULL;
  10689. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10690. BIO_free(bio);
  10691. bio = NULL;
  10692. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10693. SSL_SUCCESS);
  10694. /* should be 2 other certs on stack */
  10695. ExpectNotNull(tmp = sk_X509_pop(ca));
  10696. X509_free(tmp);
  10697. ExpectNotNull(tmp = sk_X509_pop(ca));
  10698. X509_free(tmp);
  10699. ExpectNull(sk_X509_pop(ca));
  10700. #ifndef NO_RC4
  10701. PKCS12_free(pkcs12_2);
  10702. pkcs12_2 = NULL;
  10703. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10704. NID_pbe_WithSHA1And128BitRC4,
  10705. NID_pbe_WithSHA1And128BitRC4,
  10706. 2000, 1, 0)));
  10707. EVP_PKEY_free(pkey);
  10708. pkey = NULL;
  10709. X509_free(cert);
  10710. cert = NULL;
  10711. sk_X509_pop_free(ca, NULL);
  10712. ca = NULL;
  10713. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10714. SSL_SUCCESS);
  10715. #endif /* NO_RC4 */
  10716. EVP_PKEY_free(pkey);
  10717. pkey = NULL;
  10718. X509_free(cert);
  10719. cert = NULL;
  10720. PKCS12_free(pkcs12);
  10721. pkcs12 = NULL;
  10722. PKCS12_free(pkcs12_2);
  10723. pkcs12_2 = NULL;
  10724. sk_X509_pop_free(ca, NULL);
  10725. ca = NULL;
  10726. #ifdef HAVE_ECC
  10727. /* test order of parsing */
  10728. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10729. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10730. if (f != XBADFILE) {
  10731. XFCLOSE(f);
  10732. f = XBADFILE;
  10733. }
  10734. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10735. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10736. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10737. WOLFSSL_SUCCESS);
  10738. /* check use of pkey after parse */
  10739. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10740. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10741. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10742. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10743. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10744. #else
  10745. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10746. #endif
  10747. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10748. SSL_CTX_free(ctx);
  10749. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10750. #endif
  10751. ExpectNotNull(pkey);
  10752. ExpectNotNull(cert);
  10753. ExpectNotNull(ca);
  10754. /* compare subject lines of certificates */
  10755. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10756. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10757. SSL_FILETYPE_PEM));
  10758. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10759. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10760. X509_free(x509);
  10761. x509 = NULL;
  10762. /* test expected fail case */
  10763. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10764. SSL_FILETYPE_PEM));
  10765. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10766. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10767. X509_free(x509);
  10768. x509 = NULL;
  10769. X509_free(cert);
  10770. cert = NULL;
  10771. /* get subject line from ca stack */
  10772. ExpectNotNull(cert = sk_X509_pop(ca));
  10773. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10774. /* compare subject from certificate in ca to expected */
  10775. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10776. SSL_FILETYPE_PEM));
  10777. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10778. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10779. /* modify case and compare subject from certificate in ca to expected.
  10780. * The first bit of the name is:
  10781. * /C=US/ST=Washington
  10782. * So we'll change subject->name[1] to 'c' (lower case) */
  10783. if (subject != NULL) {
  10784. subject->name[1] = 'c';
  10785. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10786. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10787. }
  10788. EVP_PKEY_free(pkey);
  10789. pkey = NULL;
  10790. X509_free(x509);
  10791. x509 = NULL;
  10792. X509_free(cert);
  10793. cert = NULL;
  10794. BIO_free(bio);
  10795. bio = NULL;
  10796. PKCS12_free(pkcs12);
  10797. pkcs12 = NULL;
  10798. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10799. ca = NULL;
  10800. /* test order of parsing */
  10801. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10802. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10803. if (f != XBADFILE) {
  10804. XFCLOSE(f);
  10805. f = XBADFILE;
  10806. }
  10807. /* check verify MAC fail case */
  10808. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10809. ExpectNull(pkey);
  10810. ExpectNull(cert);
  10811. /* check parse with no extra certs kept */
  10812. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10813. 1);
  10814. ExpectNotNull(pkey);
  10815. ExpectNotNull(cert);
  10816. wolfSSL_EVP_PKEY_free(pkey);
  10817. pkey = NULL;
  10818. wolfSSL_X509_free(cert);
  10819. cert = NULL;
  10820. /* check parse with extra certs kept */
  10821. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10822. 1);
  10823. ExpectNotNull(pkey);
  10824. ExpectNotNull(cert);
  10825. ExpectNotNull(ca);
  10826. wolfSSL_EVP_PKEY_free(pkey);
  10827. pkey = NULL;
  10828. wolfSSL_X509_free(cert);
  10829. cert = NULL;
  10830. sk_X509_pop_free(ca, NULL);
  10831. ca = NULL;
  10832. PKCS12_free(pkcs12);
  10833. pkcs12 = NULL;
  10834. #endif /* HAVE_ECC */
  10835. #ifdef WC_RC2
  10836. /* test PKCS#12 with RC2 encryption */
  10837. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10838. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10839. if (f != XBADFILE) {
  10840. XFCLOSE(f);
  10841. f = XBADFILE;
  10842. }
  10843. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10844. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10845. /* check verify MAC fail case */
  10846. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10847. ExpectNull(pkey);
  10848. ExpectNull(cert);
  10849. /* check parse with not extra certs kept */
  10850. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10851. WOLFSSL_SUCCESS);
  10852. ExpectNotNull(pkey);
  10853. ExpectNotNull(cert);
  10854. wolfSSL_EVP_PKEY_free(pkey);
  10855. pkey = NULL;
  10856. wolfSSL_X509_free(cert);
  10857. cert = NULL;
  10858. /* check parse with extra certs kept */
  10859. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10860. WOLFSSL_SUCCESS);
  10861. ExpectNotNull(pkey);
  10862. ExpectNotNull(cert);
  10863. ExpectNotNull(ca);
  10864. wolfSSL_EVP_PKEY_free(pkey);
  10865. wolfSSL_X509_free(cert);
  10866. sk_X509_pop_free(ca, NULL);
  10867. BIO_free(bio);
  10868. bio = NULL;
  10869. PKCS12_free(pkcs12);
  10870. pkcs12 = NULL;
  10871. #endif /* WC_RC2 */
  10872. /* Test i2d_PKCS12_bio */
  10873. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10874. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10875. if (f != XBADFILE)
  10876. XFCLOSE(f);
  10877. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10878. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10879. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10880. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10881. PKCS12_free(pkcs12);
  10882. BIO_free(bio);
  10883. (void)order;
  10884. #endif /* OPENSSL_EXTRA */
  10885. #endif /* HAVE_FIPS */
  10886. return EXPECT_RESULT();
  10887. }
  10888. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10889. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10890. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10891. #define TEST_PKCS8_ENC
  10892. #endif
  10893. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10894. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10895. /* used to keep track if FailTestCallback was called */
  10896. static int failTestCallbackCalled = 0;
  10897. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10898. {
  10899. (void)passwd;
  10900. (void)sz;
  10901. (void)rw;
  10902. (void)userdata;
  10903. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10904. failTestCallbackCalled = 1;
  10905. return -1;
  10906. }
  10907. #endif
  10908. static int test_wolfSSL_no_password_cb(void)
  10909. {
  10910. EXPECT_DECLS;
  10911. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10912. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10913. WOLFSSL_CTX* ctx = NULL;
  10914. byte buff[FOURK_BUF];
  10915. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10916. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10917. XFILE f = XBADFILE;
  10918. int bytes = 0;
  10919. #ifndef NO_WOLFSSL_CLIENT
  10920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10921. #else
  10922. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10923. #endif
  10924. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10925. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10926. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10927. if (f != XBADFILE) {
  10928. XFCLOSE(f);
  10929. f = XBADFILE;
  10930. }
  10931. ExpectIntLE(bytes, sizeof(buff));
  10932. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10933. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10934. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10935. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10936. if (f != XBADFILE)
  10937. XFCLOSE(f);
  10938. ExpectIntLE(bytes, sizeof(buff));
  10939. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10940. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10941. wolfSSL_CTX_free(ctx);
  10942. /* Password callback should not be called by default */
  10943. ExpectIntEQ(failTestCallbackCalled, 0);
  10944. #endif
  10945. return EXPECT_RESULT();
  10946. }
  10947. #ifdef TEST_PKCS8_ENC
  10948. /* for PKCS8 test case */
  10949. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10950. {
  10951. int flag = 0;
  10952. (void)rw;
  10953. if (userdata != NULL) {
  10954. flag = *((int*)userdata); /* user set data */
  10955. }
  10956. switch (flag) {
  10957. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10958. * can be anything the user wishes to be passed to the callback
  10959. * associated with the WOLFSSL_CTX */
  10960. XSTRNCPY(passwd, "yassl123", sz);
  10961. return 8;
  10962. default:
  10963. return BAD_FUNC_ARG;
  10964. }
  10965. }
  10966. #endif /* TEST_PKCS8_ENC */
  10967. /* Testing functions dealing with PKCS8 */
  10968. static int test_wolfSSL_PKCS8(void)
  10969. {
  10970. EXPECT_DECLS;
  10971. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10972. !defined(WOLFCRYPT_ONLY)
  10973. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10974. byte buff[FOURK_BUF];
  10975. byte der[FOURK_BUF];
  10976. #ifndef NO_RSA
  10977. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10978. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10979. #endif
  10980. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10981. #ifdef HAVE_ECC
  10982. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10983. #endif
  10984. XFILE f = XBADFILE;
  10985. int bytes = 0;
  10986. WOLFSSL_CTX* ctx = NULL;
  10987. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10988. int ret;
  10989. ecc_key key;
  10990. word32 x = 0;
  10991. #endif
  10992. #ifdef TEST_PKCS8_ENC
  10993. #if !defined(NO_RSA) && !defined(NO_SHA)
  10994. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10995. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10996. #endif
  10997. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10998. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10999. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  11000. #endif
  11001. int flag;
  11002. #endif
  11003. (void)der;
  11004. #ifndef NO_WOLFSSL_CLIENT
  11005. #ifndef WOLFSSL_NO_TLS12
  11006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11007. #else
  11008. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11009. #endif
  11010. #else
  11011. #ifndef WOLFSSL_NO_TLS12
  11012. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11013. #else
  11014. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11015. #endif
  11016. #endif
  11017. #ifdef TEST_PKCS8_ENC
  11018. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  11019. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  11020. flag = 1; /* used by password callback as return code */
  11021. #if !defined(NO_RSA) && !defined(NO_SHA)
  11022. /* test loading PEM PKCS8 encrypted file */
  11023. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  11024. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11025. if (f != XBADFILE) {
  11026. XFCLOSE(f);
  11027. f = XBADFILE;
  11028. }
  11029. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11030. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11031. /* this next case should fail because of password callback return code */
  11032. flag = 0; /* used by password callback as return code */
  11033. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11034. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11035. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11036. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11037. "yassl123"), 0);
  11038. /* test that error value is returned with a bad password */
  11039. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11040. "bad"), 0);
  11041. /* test loading PEM PKCS8 encrypted file */
  11042. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11043. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11044. if (f != XBADFILE) {
  11045. XFCLOSE(f);
  11046. f = XBADFILE;
  11047. }
  11048. flag = 1; /* used by password callback as return code */
  11049. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11050. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11051. /* this next case should fail because of password callback return code */
  11052. flag = 0; /* used by password callback as return code */
  11053. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11054. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11055. #endif /* !NO_RSA && !NO_SHA */
  11056. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11057. /* test loading PEM PKCS8 encrypted ECC Key file */
  11058. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11059. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11060. if (f != XBADFILE) {
  11061. XFCLOSE(f);
  11062. f = XBADFILE;
  11063. }
  11064. flag = 1; /* used by password callback as return code */
  11065. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11066. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11067. /* this next case should fail because of password callback return code */
  11068. flag = 0; /* used by password callback as return code */
  11069. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11070. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11071. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11072. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11073. "yassl123"), 0);
  11074. /* test that error value is returned with a bad password */
  11075. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11076. "bad"), 0);
  11077. /* test loading DER PKCS8 encrypted ECC Key file */
  11078. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11079. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11080. if (f != XBADFILE) {
  11081. XFCLOSE(f);
  11082. f = XBADFILE;
  11083. }
  11084. flag = 1; /* used by password callback as return code */
  11085. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11086. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11087. /* this next case should fail because of password callback return code */
  11088. flag = 0; /* used by password callback as return code */
  11089. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11090. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11091. /* leave flag as "okay" */
  11092. flag = 1;
  11093. #endif /* HAVE_ECC && !NO_SHA */
  11094. #endif /* TEST_PKCS8_ENC */
  11095. #ifndef NO_RSA
  11096. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11097. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11098. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11099. if (f != XBADFILE) {
  11100. XFCLOSE(f);
  11101. f = XBADFILE;
  11102. }
  11103. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11104. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11105. /* test loading PEM PKCS8 private key file (not encrypted) */
  11106. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11107. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11108. if (f != XBADFILE) {
  11109. XFCLOSE(f);
  11110. f = XBADFILE;
  11111. }
  11112. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11113. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11114. #endif /* !NO_RSA */
  11115. /* Test PKCS8 PEM ECC key no crypt */
  11116. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11117. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11118. if (f != XBADFILE) {
  11119. XFCLOSE(f);
  11120. f = XBADFILE;
  11121. }
  11122. #ifdef HAVE_ECC
  11123. /* Test PKCS8 PEM ECC key no crypt */
  11124. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11125. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11126. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11127. /* decrypt PKCS8 PEM to key in DER format */
  11128. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11129. (word32)sizeof(der), NULL)), 0);
  11130. ret = wc_ecc_init(&key);
  11131. if (ret == 0) {
  11132. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11133. wc_ecc_free(&key);
  11134. }
  11135. ExpectIntEQ(ret, 0);
  11136. #endif
  11137. /* Test PKCS8 DER ECC key no crypt */
  11138. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11139. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11140. if (f != XBADFILE)
  11141. XFCLOSE(f);
  11142. /* Test using a PKCS8 ECC PEM */
  11143. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11144. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11145. #else
  11146. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11147. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11148. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11149. #endif /* HAVE_ECC */
  11150. wolfSSL_CTX_free(ctx);
  11151. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11152. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11153. return EXPECT_RESULT();
  11154. }
  11155. static int test_wolfSSL_PKCS8_ED25519(void)
  11156. {
  11157. EXPECT_DECLS;
  11158. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11159. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11160. defined(HAVE_ED25519_KEY_IMPORT)
  11161. const byte encPrivKey[] = \
  11162. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11163. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11164. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11165. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11166. "2L9W4v7swXkV+X+a1ww=\n"
  11167. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11168. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11169. byte der[FOURK_BUF];
  11170. WOLFSSL_CTX* ctx = NULL;
  11171. int bytes;
  11172. XMEMSET(der, 0, sizeof(der));
  11173. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11174. (word32)sizeof(der), password)), 0);
  11175. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11176. #ifndef NO_WOLFSSL_SERVER
  11177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11178. #else
  11179. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11180. #endif
  11181. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11182. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11183. wolfSSL_CTX_free(ctx);
  11184. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11185. #endif
  11186. return EXPECT_RESULT();
  11187. }
  11188. static int test_wolfSSL_PKCS8_ED448(void)
  11189. {
  11190. EXPECT_DECLS;
  11191. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11192. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11193. defined(HAVE_ED448_KEY_IMPORT)
  11194. const byte encPrivKey[] = \
  11195. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11196. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11197. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11198. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11199. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11200. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11201. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11202. byte der[FOURK_BUF];
  11203. WOLFSSL_CTX* ctx = NULL;
  11204. int bytes;
  11205. XMEMSET(der, 0, sizeof(der));
  11206. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11207. (word32)sizeof(der), password)), 0);
  11208. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11209. #ifndef NO_WOLFSSL_SERVER
  11210. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11211. #else
  11212. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11213. #endif
  11214. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11215. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11216. wolfSSL_CTX_free(ctx);
  11217. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11218. #endif
  11219. return EXPECT_RESULT();
  11220. }
  11221. /* Testing functions dealing with PKCS5 */
  11222. static int test_wolfSSL_PKCS5(void)
  11223. {
  11224. EXPECT_DECLS;
  11225. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11226. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11227. const char* passwd = "myfipsPa$$W0rd";
  11228. #else
  11229. const char *passwd = "pass1234";
  11230. #endif
  11231. const unsigned char *salt = (unsigned char *)"salt1234";
  11232. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11233. DYNAMIC_TYPE_TMP_BUFFER);
  11234. int ret = 0;
  11235. ExpectNotNull(out);
  11236. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11237. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11238. WOLFSSL_SUCCESS);
  11239. #ifdef WOLFSSL_SHA512
  11240. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11241. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11242. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11243. #endif
  11244. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11245. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11246. return EXPECT_RESULT();
  11247. }
  11248. /* test parsing URI from certificate */
  11249. static int test_wolfSSL_URI(void)
  11250. {
  11251. EXPECT_DECLS;
  11252. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11253. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11254. defined(OPENSSL_EXTRA))
  11255. WOLFSSL_X509* x509 = NULL;
  11256. const char uri[] = "./certs/client-uri-cert.pem";
  11257. const char urn[] = "./certs/client-absolute-urn.pem";
  11258. const char badUri[] = "./certs/client-relative-uri.pem";
  11259. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11260. WOLFSSL_FILETYPE_PEM));
  11261. wolfSSL_FreeX509(x509);
  11262. x509 = NULL;
  11263. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11264. WOLFSSL_FILETYPE_PEM));
  11265. wolfSSL_FreeX509(x509);
  11266. x509 = NULL;
  11267. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11268. && !defined(WOLFSSL_FPKI)
  11269. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11270. WOLFSSL_FILETYPE_PEM));
  11271. #else
  11272. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11273. WOLFSSL_FILETYPE_PEM));
  11274. #endif
  11275. wolfSSL_FreeX509(x509);
  11276. #endif
  11277. return EXPECT_RESULT();
  11278. }
  11279. static int test_wolfSSL_TBS(void)
  11280. {
  11281. EXPECT_DECLS;
  11282. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11283. && defined(OPENSSL_EXTRA)
  11284. WOLFSSL_X509* x509 = NULL;
  11285. const unsigned char* tbs;
  11286. int tbsSz;
  11287. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11288. WOLFSSL_FILETYPE_PEM));
  11289. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11290. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11291. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11292. ExpectIntEQ(tbsSz, 1003);
  11293. wolfSSL_FreeX509(x509);
  11294. #endif
  11295. return EXPECT_RESULT();
  11296. }
  11297. static int test_wolfSSL_X509_verify(void)
  11298. {
  11299. EXPECT_DECLS;
  11300. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11301. defined(OPENSSL_EXTRA)
  11302. WOLFSSL_X509* ca = NULL;
  11303. WOLFSSL_X509* serv = NULL;
  11304. WOLFSSL_EVP_PKEY* pkey = NULL;
  11305. unsigned char buf[2048];
  11306. const unsigned char* pt = NULL;
  11307. int bufSz;
  11308. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11309. WOLFSSL_FILETYPE_PEM));
  11310. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11311. WOLFSSL_SUCCESS);
  11312. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11313. WOLFSSL_SUCCESS);
  11314. ExpectIntEQ(bufSz, 294);
  11315. bufSz = 2048;
  11316. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11317. WOLFSSL_SUCCESS);
  11318. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11319. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11320. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11321. WOLFSSL_FILETYPE_PEM));
  11322. /* success case */
  11323. pt = buf;
  11324. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11325. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11326. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11327. wolfSSL_EVP_PKEY_free(pkey);
  11328. pkey = NULL;
  11329. /* fail case */
  11330. bufSz = 2048;
  11331. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11332. WOLFSSL_SUCCESS);
  11333. pt = buf;
  11334. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11335. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11336. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11337. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11338. wolfSSL_EVP_PKEY_free(pkey);
  11339. wolfSSL_FreeX509(ca);
  11340. wolfSSL_FreeX509(serv);
  11341. #endif
  11342. return EXPECT_RESULT();
  11343. }
  11344. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11345. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11346. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11347. /* create certificate with version 2 */
  11348. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11349. {
  11350. EXPECT_DECLS;
  11351. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11352. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11353. unsigned char *der = NULL, *key = NULL, *pt;
  11354. char *header = NULL, *name = NULL;
  11355. int derSz;
  11356. long keySz;
  11357. XFILE fp = XBADFILE;
  11358. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11359. time_t t;
  11360. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11361. WOLFSSL_FILETYPE_PEM));
  11362. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11363. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11364. WOLFSSL_SUCCESS);
  11365. if (fp != XBADFILE)
  11366. XFCLOSE(fp);
  11367. pt = key;
  11368. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11369. (const unsigned char**)&pt, keySz));
  11370. /* create the version 2 certificate */
  11371. ExpectNotNull(x509v2 = X509_new());
  11372. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11373. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11374. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11375. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11376. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11377. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11378. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11379. t = time(NULL);
  11380. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11381. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11382. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11383. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11384. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11385. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11386. ExpectIntGT(derSz, 0);
  11387. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11388. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11389. /* TODO: Replace with API call */
  11390. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11391. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11392. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11393. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11394. wolfSSL_X509_free(x509);
  11395. wolfSSL_X509_free(x509v2);
  11396. wolfSSL_EVP_PKEY_free(priv);
  11397. wolfSSL_EVP_PKEY_free(pub);
  11398. wolfSSL_ASN1_TIME_free(notBefore);
  11399. wolfSSL_ASN1_TIME_free(notAfter);
  11400. return EXPECT_RESULT();
  11401. }
  11402. /* override certificate version error */
  11403. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11404. {
  11405. EXPECT_DECLS;
  11406. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11407. ExpectIntEQ(store->error, ASN_VERSION_E);
  11408. #else
  11409. ExpectIntEQ(store->error, 0);
  11410. #endif
  11411. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11412. (void)preverify;
  11413. return EXPECT_RESULT() == TEST_SUCCESS;
  11414. }
  11415. /* set verify callback that will override bad certificate version */
  11416. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11417. {
  11418. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11419. return TEST_SUCCESS;
  11420. }
  11421. #endif
  11422. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11423. {
  11424. EXPECT_DECLS;
  11425. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11426. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11427. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11428. test_ssl_cbf func_cb_client;
  11429. test_ssl_cbf func_cb_server;
  11430. /* test server rejects a client certificate that is not version 3 */
  11431. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11432. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11433. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11434. #ifndef WOLFSSL_NO_TLS12
  11435. func_cb_client.method = wolfTLSv1_2_client_method;
  11436. #else
  11437. func_cb_client.method = wolfTLSv1_3_client_method;
  11438. #endif
  11439. #ifndef WOLFSSL_NO_TLS12
  11440. func_cb_server.method = wolfTLSv1_2_server_method;
  11441. #else
  11442. func_cb_server.method = wolfTLSv1_3_server_method;
  11443. #endif
  11444. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11445. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11446. &func_cb_server, NULL), TEST_FAIL);
  11447. #else
  11448. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11449. &func_cb_server, NULL), TEST_SUCCESS);
  11450. #endif
  11451. #endif
  11452. return EXPECT_RESULT();
  11453. }
  11454. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11455. {
  11456. EXPECT_DECLS;
  11457. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11458. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11459. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11460. test_ssl_cbf func_cb_client;
  11461. test_ssl_cbf func_cb_server;
  11462. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11463. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11464. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11465. func_cb_server.ctx_ready = &test_set_override_x509;
  11466. #ifndef WOLFSSL_NO_TLS12
  11467. func_cb_client.method = wolfTLSv1_2_client_method;
  11468. #else
  11469. func_cb_client.method = wolfTLSv1_3_client_method;
  11470. #endif
  11471. #ifndef WOLFSSL_NO_TLS12
  11472. func_cb_server.method = wolfTLSv1_2_server_method;
  11473. #else
  11474. func_cb_server.method = wolfTLSv1_3_server_method;
  11475. #endif
  11476. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11477. &func_cb_server, NULL), TEST_SUCCESS);
  11478. #endif
  11479. return EXPECT_RESULT();
  11480. }
  11481. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11482. * version allowed.
  11483. * POST: 1 on success.
  11484. */
  11485. static int test_wolfSSL_CTX_SetMinVersion(void)
  11486. {
  11487. int res = TEST_SKIPPED;
  11488. #ifndef NO_WOLFSSL_CLIENT
  11489. int failFlag = WOLFSSL_SUCCESS;
  11490. WOLFSSL_CTX* ctx;
  11491. int itr;
  11492. #ifndef NO_OLD_TLS
  11493. const int versions[] = {
  11494. #ifdef WOLFSSL_ALLOW_TLSV10
  11495. WOLFSSL_TLSV1,
  11496. #endif
  11497. WOLFSSL_TLSV1_1,
  11498. WOLFSSL_TLSV1_2 };
  11499. #elif !defined(WOLFSSL_NO_TLS12)
  11500. const int versions[] = { WOLFSSL_TLSV1_2 };
  11501. #elif defined(WOLFSSL_TLS13)
  11502. const int versions[] = { WOLFSSL_TLSV1_3 };
  11503. #else
  11504. const int versions[0];
  11505. #endif
  11506. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11507. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11508. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11509. != WOLFSSL_SUCCESS) {
  11510. failFlag = WOLFSSL_FAILURE;
  11511. }
  11512. }
  11513. wolfSSL_CTX_free(ctx);
  11514. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11515. #endif
  11516. return res;
  11517. } /* END test_wolfSSL_CTX_SetMinVersion */
  11518. /*----------------------------------------------------------------------------*
  11519. | OCSP Stapling
  11520. *----------------------------------------------------------------------------*/
  11521. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11522. * need to contact the CA, lowering the cost of cert revocation checking.
  11523. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11524. * POST: 1 returned for success.
  11525. */
  11526. static int test_wolfSSL_UseOCSPStapling(void)
  11527. {
  11528. EXPECT_DECLS;
  11529. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11530. !defined(NO_WOLFSSL_CLIENT)
  11531. WOLFSSL_CTX* ctx = NULL;
  11532. WOLFSSL* ssl = NULL;
  11533. #ifndef NO_WOLFSSL_CLIENT
  11534. #ifndef WOLFSSL_NO_TLS12
  11535. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11536. #else
  11537. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11538. #endif
  11539. #else
  11540. #ifndef WOLFSSL_NO_TLS12
  11541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11542. #else
  11543. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11544. #endif
  11545. #endif
  11546. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11547. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11548. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11549. #ifndef NO_WOLFSSL_CLIENT
  11550. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11551. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11552. #else
  11553. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11554. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11555. #endif
  11556. wolfSSL_free(ssl);
  11557. wolfSSL_CTX_free(ctx);
  11558. #endif
  11559. return EXPECT_RESULT();
  11560. } /* END test_wolfSSL_UseOCSPStapling */
  11561. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11562. * stapling eliminates the need to contact the CA and lowers cert revocation
  11563. * check.
  11564. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11565. */
  11566. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11567. {
  11568. EXPECT_DECLS;
  11569. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11570. !defined(NO_WOLFSSL_CLIENT)
  11571. WOLFSSL_CTX* ctx = NULL;
  11572. WOLFSSL* ssl = NULL;
  11573. #ifndef NO_WOLFSSL_CLIENT
  11574. #ifndef WOLFSSL_NO_TLS12
  11575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11576. #else
  11577. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11578. #endif
  11579. #else
  11580. #ifndef WOLFSSL_NO_TLS12
  11581. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11582. #else
  11583. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11584. #endif
  11585. #endif
  11586. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11587. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11588. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11589. #ifndef NO_WOLFSSL_CLIENT
  11590. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11591. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11592. #else
  11593. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11594. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11595. #endif
  11596. wolfSSL_free(ssl);
  11597. wolfSSL_CTX_free(ctx);
  11598. #endif
  11599. return EXPECT_RESULT();
  11600. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11601. /*----------------------------------------------------------------------------*
  11602. | Multicast Tests
  11603. *----------------------------------------------------------------------------*/
  11604. static int test_wolfSSL_mcast(void)
  11605. {
  11606. EXPECT_DECLS;
  11607. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11608. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11609. WOLFSSL_CTX* ctx = NULL;
  11610. WOLFSSL* ssl = NULL;
  11611. byte preMasterSecret[512];
  11612. byte clientRandom[32];
  11613. byte serverRandom[32];
  11614. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11615. byte buf[256];
  11616. word16 newId;
  11617. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11618. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11619. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11620. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11621. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11622. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11623. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11624. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11625. WOLFSSL_SUCCESS);
  11626. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11627. ExpectIntLE(newId, 100);
  11628. wolfSSL_free(ssl);
  11629. wolfSSL_CTX_free(ctx);
  11630. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11631. * WOLFSSL_SNIFFER) */
  11632. return EXPECT_RESULT();
  11633. }
  11634. /*----------------------------------------------------------------------------*
  11635. | Wolfcrypt
  11636. *----------------------------------------------------------------------------*/
  11637. /*
  11638. * Unit test for the wc_InitBlake2b()
  11639. */
  11640. static int test_wc_InitBlake2b(void)
  11641. {
  11642. EXPECT_DECLS;
  11643. #ifdef HAVE_BLAKE2
  11644. Blake2b blake;
  11645. /* Test good arg. */
  11646. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11647. /* Test bad arg. */
  11648. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11649. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11650. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11651. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11652. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11653. #endif
  11654. return EXPECT_RESULT();
  11655. } /* END test_wc_InitBlake2b*/
  11656. /*
  11657. * Unit test for the wc_InitBlake2b_WithKey()
  11658. */
  11659. static int test_wc_InitBlake2b_WithKey(void)
  11660. {
  11661. EXPECT_DECLS;
  11662. #ifdef HAVE_BLAKE2
  11663. Blake2b blake;
  11664. word32 digestSz = BLAKE2B_KEYBYTES;
  11665. byte key[BLAKE2B_KEYBYTES];
  11666. word32 keylen = BLAKE2B_KEYBYTES;
  11667. XMEMSET(key, 0, sizeof(key));
  11668. /* Test good arg. */
  11669. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11670. /* Test bad args. */
  11671. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11672. BAD_FUNC_ARG);
  11673. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11674. BAD_FUNC_ARG);
  11675. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11676. #endif
  11677. return EXPECT_RESULT();
  11678. } /* END wc_InitBlake2b_WithKey*/
  11679. /*
  11680. * Unit test for the wc_InitBlake2s_WithKey()
  11681. */
  11682. static int test_wc_InitBlake2s_WithKey(void)
  11683. {
  11684. EXPECT_DECLS;
  11685. #ifdef HAVE_BLAKE2S
  11686. Blake2s blake;
  11687. word32 digestSz = BLAKE2S_KEYBYTES;
  11688. byte *key = (byte*)"01234567890123456789012345678901";
  11689. word32 keylen = BLAKE2S_KEYBYTES;
  11690. /* Test good arg. */
  11691. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11692. /* Test bad args. */
  11693. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11694. BAD_FUNC_ARG);
  11695. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11696. BAD_FUNC_ARG);
  11697. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11698. #endif
  11699. return EXPECT_RESULT();
  11700. } /* END wc_InitBlake2s_WithKey*/
  11701. /*
  11702. * Unit test for the wc_InitMd5()
  11703. */
  11704. static int test_wc_InitMd5(void)
  11705. {
  11706. EXPECT_DECLS;
  11707. #ifndef NO_MD5
  11708. wc_Md5 md5;
  11709. /* Test good arg. */
  11710. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11711. /* Test bad arg. */
  11712. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11713. wc_Md5Free(&md5);
  11714. #endif
  11715. return EXPECT_RESULT();
  11716. } /* END test_wc_InitMd5 */
  11717. /*
  11718. * Testing wc_UpdateMd5()
  11719. */
  11720. static int test_wc_Md5Update(void)
  11721. {
  11722. EXPECT_DECLS;
  11723. #ifndef NO_MD5
  11724. wc_Md5 md5;
  11725. byte hash[WC_MD5_DIGEST_SIZE];
  11726. testVector a, b, c;
  11727. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11728. /* Input */
  11729. a.input = "a";
  11730. a.inLen = XSTRLEN(a.input);
  11731. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11732. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11733. /* Update input. */
  11734. a.input = "abc";
  11735. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11736. "\x72";
  11737. a.inLen = XSTRLEN(a.input);
  11738. a.outLen = XSTRLEN(a.output);
  11739. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11740. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11741. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11742. /* Pass in bad values. */
  11743. b.input = NULL;
  11744. b.inLen = 0;
  11745. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11746. c.input = NULL;
  11747. c.inLen = WC_MD5_DIGEST_SIZE;
  11748. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11749. BAD_FUNC_ARG);
  11750. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11751. BAD_FUNC_ARG);
  11752. wc_Md5Free(&md5);
  11753. #endif
  11754. return EXPECT_RESULT();
  11755. } /* END test_wc_Md5Update() */
  11756. /*
  11757. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11758. */
  11759. static int test_wc_Md5Final(void)
  11760. {
  11761. EXPECT_DECLS;
  11762. #ifndef NO_MD5
  11763. /* Instantiate */
  11764. wc_Md5 md5;
  11765. byte* hash_test[3];
  11766. byte hash1[WC_MD5_DIGEST_SIZE];
  11767. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11768. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11769. int times, i;
  11770. /* Initialize */
  11771. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11772. hash_test[0] = hash1;
  11773. hash_test[1] = hash2;
  11774. hash_test[2] = hash3;
  11775. times = sizeof(hash_test)/sizeof(byte*);
  11776. for (i = 0; i < times; i++) {
  11777. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11778. }
  11779. /* Test bad args. */
  11780. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11781. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11782. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11783. wc_Md5Free(&md5);
  11784. #endif
  11785. return EXPECT_RESULT();
  11786. }
  11787. /*
  11788. * Unit test for the wc_InitSha()
  11789. */
  11790. static int test_wc_InitSha(void)
  11791. {
  11792. EXPECT_DECLS;
  11793. #ifndef NO_SHA
  11794. wc_Sha sha;
  11795. /* Test good arg. */
  11796. ExpectIntEQ(wc_InitSha(&sha), 0);
  11797. /* Test bad arg. */
  11798. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11799. wc_ShaFree(&sha);
  11800. #endif
  11801. return EXPECT_RESULT();
  11802. } /* END test_wc_InitSha */
  11803. /*
  11804. * Tesing wc_ShaUpdate()
  11805. */
  11806. static int test_wc_ShaUpdate(void)
  11807. {
  11808. EXPECT_DECLS;
  11809. #ifndef NO_SHA
  11810. wc_Sha sha;
  11811. byte hash[WC_SHA_DIGEST_SIZE];
  11812. testVector a, b, c;
  11813. ExpectIntEQ(wc_InitSha(&sha), 0);
  11814. /* Input. */
  11815. a.input = "a";
  11816. a.inLen = XSTRLEN(a.input);
  11817. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11818. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11819. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11820. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11821. /* Update input. */
  11822. a.input = "abc";
  11823. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11824. "\x6C\x9C\xD0\xD8\x9D";
  11825. a.inLen = XSTRLEN(a.input);
  11826. a.outLen = XSTRLEN(a.output);
  11827. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11828. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11829. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11830. /* Try passing in bad values. */
  11831. b.input = NULL;
  11832. b.inLen = 0;
  11833. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11834. c.input = NULL;
  11835. c.inLen = WC_SHA_DIGEST_SIZE;
  11836. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11837. BAD_FUNC_ARG);
  11838. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11839. BAD_FUNC_ARG);
  11840. wc_ShaFree(&sha);
  11841. #endif
  11842. return EXPECT_RESULT();
  11843. } /* END test_wc_ShaUpdate() */
  11844. /*
  11845. * Unit test on wc_ShaFinal
  11846. */
  11847. static int test_wc_ShaFinal(void)
  11848. {
  11849. EXPECT_DECLS;
  11850. #ifndef NO_SHA
  11851. wc_Sha sha;
  11852. byte* hash_test[3];
  11853. byte hash1[WC_SHA_DIGEST_SIZE];
  11854. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11855. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11856. int times, i;
  11857. /* Initialize*/
  11858. ExpectIntEQ(wc_InitSha(&sha), 0);
  11859. hash_test[0] = hash1;
  11860. hash_test[1] = hash2;
  11861. hash_test[2] = hash3;
  11862. times = sizeof(hash_test)/sizeof(byte*);
  11863. for (i = 0; i < times; i++) {
  11864. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11865. }
  11866. /* Test bad args. */
  11867. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11868. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11869. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11870. wc_ShaFree(&sha);
  11871. #endif
  11872. return EXPECT_RESULT();
  11873. } /* END test_wc_ShaFinal */
  11874. /*
  11875. * Unit test for wc_InitSha256()
  11876. */
  11877. static int test_wc_InitSha256(void)
  11878. {
  11879. EXPECT_DECLS;
  11880. #ifndef NO_SHA256
  11881. wc_Sha256 sha256;
  11882. /* Test good arg. */
  11883. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11884. /* Test bad arg. */
  11885. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11886. wc_Sha256Free(&sha256);
  11887. #endif
  11888. return EXPECT_RESULT();
  11889. } /* END test_wc_InitSha256 */
  11890. /*
  11891. * Unit test for wc_Sha256Update()
  11892. */
  11893. static int test_wc_Sha256Update(void)
  11894. {
  11895. EXPECT_DECLS;
  11896. #ifndef NO_SHA256
  11897. wc_Sha256 sha256;
  11898. byte hash[WC_SHA256_DIGEST_SIZE];
  11899. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11900. testVector a, b, c;
  11901. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11902. /* Input. */
  11903. a.input = "a";
  11904. a.inLen = XSTRLEN(a.input);
  11905. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11906. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11907. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11908. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11909. /* Update input. */
  11910. a.input = "abc";
  11911. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11912. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11913. "\x15\xAD";
  11914. a.inLen = XSTRLEN(a.input);
  11915. a.outLen = XSTRLEN(a.output);
  11916. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11917. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11918. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11919. /* Unaligned check. */
  11920. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11921. 0);
  11922. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11923. /* Try passing in bad values */
  11924. b.input = NULL;
  11925. b.inLen = 0;
  11926. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11927. c.input = NULL;
  11928. c.inLen = WC_SHA256_DIGEST_SIZE;
  11929. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11930. BAD_FUNC_ARG);
  11931. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11932. BAD_FUNC_ARG);
  11933. wc_Sha256Free(&sha256);
  11934. #endif
  11935. return EXPECT_RESULT();
  11936. } /* END test_wc_Sha256Update */
  11937. /*
  11938. * Unit test function for wc_Sha256Final()
  11939. */
  11940. static int test_wc_Sha256Final(void)
  11941. {
  11942. EXPECT_DECLS;
  11943. #ifndef NO_SHA256
  11944. wc_Sha256 sha256;
  11945. byte* hash_test[3];
  11946. byte hash1[WC_SHA256_DIGEST_SIZE];
  11947. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11948. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11949. int times, i;
  11950. /* Initialize */
  11951. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11952. hash_test[0] = hash1;
  11953. hash_test[1] = hash2;
  11954. hash_test[2] = hash3;
  11955. times = sizeof(hash_test) / sizeof(byte*);
  11956. for (i = 0; i < times; i++) {
  11957. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11958. }
  11959. /* Test bad args. */
  11960. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11961. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11962. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11963. wc_Sha256Free(&sha256);
  11964. #endif
  11965. return EXPECT_RESULT();
  11966. } /* END test_wc_Sha256Final */
  11967. /*
  11968. * Unit test function for wc_Sha256FinalRaw()
  11969. */
  11970. static int test_wc_Sha256FinalRaw(void)
  11971. {
  11972. EXPECT_DECLS;
  11973. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11974. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11975. !defined(WOLFSSL_NO_HASH_RAW)
  11976. wc_Sha256 sha256;
  11977. byte* hash_test[3];
  11978. byte hash1[WC_SHA256_DIGEST_SIZE];
  11979. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11980. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11981. int times, i;
  11982. /* Initialize */
  11983. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11984. hash_test[0] = hash1;
  11985. hash_test[1] = hash2;
  11986. hash_test[2] = hash3;
  11987. times = sizeof(hash_test) / sizeof(byte*);
  11988. for (i = 0; i < times; i++) {
  11989. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11990. }
  11991. /* Test bad args. */
  11992. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11993. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11994. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11995. wc_Sha256Free(&sha256);
  11996. #endif
  11997. return EXPECT_RESULT();
  11998. } /* END test_wc_Sha256FinalRaw */
  11999. /*
  12000. * Unit test function for wc_Sha256GetFlags()
  12001. */
  12002. static int test_wc_Sha256GetFlags(void)
  12003. {
  12004. EXPECT_DECLS;
  12005. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  12006. wc_Sha256 sha256;
  12007. word32 flags = 0;
  12008. /* Initialize */
  12009. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12010. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  12011. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12012. wc_Sha256Free(&sha256);
  12013. #endif
  12014. return EXPECT_RESULT();
  12015. } /* END test_wc_Sha256GetFlags */
  12016. /*
  12017. * Unit test function for wc_Sha256Free()
  12018. */
  12019. static int test_wc_Sha256Free(void)
  12020. {
  12021. EXPECT_DECLS;
  12022. #ifndef NO_SHA256
  12023. wc_Sha256Free(NULL);
  12024. /* Set result to SUCCESS. */
  12025. ExpectTrue(1);
  12026. #endif
  12027. return EXPECT_RESULT();
  12028. } /* END test_wc_Sha256Free */
  12029. /*
  12030. * Unit test function for wc_Sha256GetHash()
  12031. */
  12032. static int test_wc_Sha256GetHash(void)
  12033. {
  12034. EXPECT_DECLS;
  12035. #ifndef NO_SHA256
  12036. wc_Sha256 sha256;
  12037. byte hash1[WC_SHA256_DIGEST_SIZE];
  12038. /* Initialize */
  12039. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12040. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12041. /* test bad arguments*/
  12042. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12043. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12044. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12045. wc_Sha256Free(&sha256);
  12046. #endif
  12047. return EXPECT_RESULT();
  12048. } /* END test_wc_Sha256GetHash */
  12049. /*
  12050. * Unit test function for wc_Sha256Copy()
  12051. */
  12052. static int test_wc_Sha256Copy(void)
  12053. {
  12054. EXPECT_DECLS;
  12055. #ifndef NO_SHA256
  12056. wc_Sha256 sha256;
  12057. wc_Sha256 temp;
  12058. XMEMSET(&sha256, 0, sizeof(sha256));
  12059. XMEMSET(&temp, 0, sizeof(temp));
  12060. /* Initialize */
  12061. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12062. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12063. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12064. /* test bad arguments*/
  12065. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12066. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12067. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12068. wc_Sha256Free(&sha256);
  12069. wc_Sha256Free(&temp);
  12070. #endif
  12071. return EXPECT_RESULT();
  12072. } /* END test_wc_Sha256Copy */
  12073. /*
  12074. * Testing wc_InitSha512()
  12075. */
  12076. static int test_wc_InitSha512(void)
  12077. {
  12078. EXPECT_DECLS;
  12079. #ifdef WOLFSSL_SHA512
  12080. wc_Sha512 sha512;
  12081. /* Test good arg. */
  12082. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12083. /* Test bad arg. */
  12084. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12085. wc_Sha512Free(&sha512);
  12086. #endif
  12087. return EXPECT_RESULT();
  12088. } /* END test_wc_InitSha512 */
  12089. /*
  12090. * wc_Sha512Update() test.
  12091. */
  12092. static int test_wc_Sha512Update(void)
  12093. {
  12094. EXPECT_DECLS;
  12095. #ifdef WOLFSSL_SHA512
  12096. wc_Sha512 sha512;
  12097. byte hash[WC_SHA512_DIGEST_SIZE];
  12098. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12099. testVector a, b, c;
  12100. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12101. /* Input. */
  12102. a.input = "a";
  12103. a.inLen = XSTRLEN(a.input);
  12104. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12105. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12106. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12107. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12108. /* Update input. */
  12109. a.input = "abc";
  12110. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12111. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12112. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12113. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12114. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12115. a.inLen = XSTRLEN(a.input);
  12116. a.outLen = XSTRLEN(a.output);
  12117. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12118. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12119. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12120. /* Unaligned check. */
  12121. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12122. 0);
  12123. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12124. /* Try passing in bad values */
  12125. b.input = NULL;
  12126. b.inLen = 0;
  12127. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12128. c.input = NULL;
  12129. c.inLen = WC_SHA512_DIGEST_SIZE;
  12130. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12131. BAD_FUNC_ARG);
  12132. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12133. BAD_FUNC_ARG);
  12134. wc_Sha512Free(&sha512);
  12135. #endif
  12136. return EXPECT_RESULT();
  12137. } /* END test_wc_Sha512Update */
  12138. #ifdef WOLFSSL_SHA512
  12139. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12140. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12141. /* Performs test for
  12142. * - wc_Sha512Final/wc_Sha512FinalRaw
  12143. * - wc_Sha512_224Final/wc_Sha512_224Final
  12144. * - wc_Sha512_256Final/wc_Sha512_256Final
  12145. * parameter:
  12146. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12147. * WC_HASH_TYPE_SHA512_256
  12148. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12149. *return 0 on success
  12150. */
  12151. static int test_Sha512_Family_Final(int type, int isRaw)
  12152. {
  12153. EXPECT_DECLS;
  12154. wc_Sha512 sha512;
  12155. byte* hash_test[3];
  12156. byte hash1[WC_SHA512_DIGEST_SIZE];
  12157. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12158. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12159. int times, i;
  12160. int(*initFp)(wc_Sha512*);
  12161. int(*finalFp)(wc_Sha512*, byte*);
  12162. void(*freeFp)(wc_Sha512*);
  12163. if (type == WC_HASH_TYPE_SHA512) {
  12164. initFp = wc_InitSha512;
  12165. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12166. !defined(WOLFSSL_NO_HASH_RAW)
  12167. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12168. #else
  12169. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12170. #endif
  12171. freeFp = wc_Sha512Free;
  12172. }
  12173. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12174. #if !defined(WOLFSSL_NOSHA512_224)
  12175. else if (type == WC_HASH_TYPE_SHA512_224) {
  12176. initFp = wc_InitSha512_224;
  12177. #if !defined(WOLFSSL_NO_HASH_RAW)
  12178. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12179. #else
  12180. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12181. #endif
  12182. freeFp = wc_Sha512_224Free;
  12183. }
  12184. #endif
  12185. #if !defined(WOLFSSL_NOSHA512_256)
  12186. else if (type == WC_HASH_TYPE_SHA512_256) {
  12187. initFp = wc_InitSha512_256;
  12188. #if !defined(WOLFSSL_NO_HASH_RAW)
  12189. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12190. #else
  12191. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12192. #endif
  12193. freeFp = wc_Sha512_256Free;
  12194. }
  12195. #endif
  12196. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12197. else
  12198. return TEST_FAIL;
  12199. /* Initialize */
  12200. ExpectIntEQ(initFp(&sha512), 0);
  12201. hash_test[0] = hash1;
  12202. hash_test[1] = hash2;
  12203. hash_test[2] = hash3;
  12204. times = sizeof(hash_test) / sizeof(byte *);
  12205. /* Good test args. */
  12206. for (i = 0; i < times; i++) {
  12207. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12208. }
  12209. /* Test bad args. */
  12210. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12211. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12212. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12213. freeFp(&sha512);
  12214. return EXPECT_RESULT();
  12215. }
  12216. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12217. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12218. #endif /* WOLFSSL_SHA512 */
  12219. /*
  12220. * Unit test function for wc_Sha512Final()
  12221. */
  12222. static int test_wc_Sha512Final(void)
  12223. {
  12224. EXPECT_DECLS;
  12225. #ifdef WOLFSSL_SHA512
  12226. wc_Sha512 sha512;
  12227. byte* hash_test[3];
  12228. byte hash1[WC_SHA512_DIGEST_SIZE];
  12229. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12230. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12231. int times, i;
  12232. /* Initialize */
  12233. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12234. hash_test[0] = hash1;
  12235. hash_test[1] = hash2;
  12236. hash_test[2] = hash3;
  12237. times = sizeof(hash_test) / sizeof(byte *);
  12238. for (i = 0; i < times; i++) {
  12239. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12240. }
  12241. /* Test bad args. */
  12242. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12243. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12245. wc_Sha512Free(&sha512);
  12246. #endif
  12247. return EXPECT_RESULT();
  12248. } /* END test_wc_Sha512Final */
  12249. /*
  12250. * Unit test function for wc_Sha512GetFlags()
  12251. */
  12252. static int test_wc_Sha512GetFlags(void)
  12253. {
  12254. EXPECT_DECLS;
  12255. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12256. wc_Sha512 sha512;
  12257. word32 flags = 0;
  12258. /* Initialize */
  12259. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12260. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12261. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12262. wc_Sha512Free(&sha512);
  12263. #endif
  12264. return EXPECT_RESULT();
  12265. } /* END test_wc_Sha512GetFlags */
  12266. /*
  12267. * Unit test function for wc_Sha512FinalRaw()
  12268. */
  12269. static int test_wc_Sha512FinalRaw(void)
  12270. {
  12271. EXPECT_DECLS;
  12272. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12273. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12274. !defined(WOLFSSL_NO_HASH_RAW)
  12275. wc_Sha512 sha512;
  12276. byte* hash_test[3];
  12277. byte hash1[WC_SHA512_DIGEST_SIZE];
  12278. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12279. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12280. int times, i;
  12281. /* Initialize */
  12282. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12283. hash_test[0] = hash1;
  12284. hash_test[1] = hash2;
  12285. hash_test[2] = hash3;
  12286. times = sizeof(hash_test) / sizeof(byte*);
  12287. /* Good test args. */
  12288. for (i = 0; i < times; i++) {
  12289. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12290. }
  12291. /* Test bad args. */
  12292. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12293. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12294. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12295. wc_Sha512Free(&sha512);
  12296. #endif
  12297. return EXPECT_RESULT();
  12298. } /* END test_wc_Sha512FinalRaw */
  12299. /*
  12300. * Unit test function for wc_Sha512Free()
  12301. */
  12302. static int test_wc_Sha512Free(void)
  12303. {
  12304. EXPECT_DECLS;
  12305. #ifdef WOLFSSL_SHA512
  12306. wc_Sha512Free(NULL);
  12307. /* Set result to SUCCESS. */
  12308. ExpectTrue(1);
  12309. #endif
  12310. return EXPECT_RESULT();
  12311. } /* END test_wc_Sha512Free */
  12312. #ifdef WOLFSSL_SHA512
  12313. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12314. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12315. static int test_Sha512_Family_GetHash(int type )
  12316. {
  12317. EXPECT_DECLS;
  12318. int(*initFp)(wc_Sha512*);
  12319. int(*ghashFp)(wc_Sha512*, byte*);
  12320. wc_Sha512 sha512;
  12321. byte hash1[WC_SHA512_DIGEST_SIZE];
  12322. if (type == WC_HASH_TYPE_SHA512) {
  12323. initFp = wc_InitSha512;
  12324. ghashFp = wc_Sha512GetHash;
  12325. }
  12326. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12327. #if !defined(WOLFSSL_NOSHA512_224)
  12328. else if (type == WC_HASH_TYPE_SHA512_224) {
  12329. initFp = wc_InitSha512_224;
  12330. ghashFp = wc_Sha512_224GetHash;
  12331. }
  12332. #endif
  12333. #if !defined(WOLFSSL_NOSHA512_256)
  12334. else if (type == WC_HASH_TYPE_SHA512_256) {
  12335. initFp = wc_InitSha512_256;
  12336. ghashFp = wc_Sha512_256GetHash;
  12337. }
  12338. #endif
  12339. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12340. else {
  12341. initFp = NULL;
  12342. ghashFp = NULL;
  12343. }
  12344. if (initFp == NULL || ghashFp == NULL)
  12345. return TEST_FAIL;
  12346. ExpectIntEQ(initFp(&sha512), 0);
  12347. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12348. /* test bad arguments*/
  12349. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12350. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12351. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12352. wc_Sha512Free(&sha512);
  12353. return EXPECT_RESULT();
  12354. }
  12355. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12356. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12357. #endif /* WOLFSSL_SHA512 */
  12358. /*
  12359. * Unit test function for wc_Sha512GetHash()
  12360. */
  12361. static int test_wc_Sha512GetHash(void)
  12362. {
  12363. EXPECT_DECLS;
  12364. #ifdef WOLFSSL_SHA512
  12365. wc_Sha512 sha512;
  12366. byte hash1[WC_SHA512_DIGEST_SIZE];
  12367. /* Initialize */
  12368. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12369. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12370. /* test bad arguments*/
  12371. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12372. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12373. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12374. wc_Sha512Free(&sha512);
  12375. #endif
  12376. return EXPECT_RESULT();
  12377. } /* END test_wc_Sha512GetHash */
  12378. /*
  12379. * Unit test function for wc_Sha512Copy()
  12380. */
  12381. static int test_wc_Sha512Copy(void)
  12382. {
  12383. EXPECT_DECLS;
  12384. #ifdef WOLFSSL_SHA512
  12385. wc_Sha512 sha512;
  12386. wc_Sha512 temp;
  12387. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12388. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12389. /* Initialize */
  12390. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12391. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12392. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12393. /* test bad arguments*/
  12394. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12395. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12396. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12397. wc_Sha512Free(&sha512);
  12398. wc_Sha512Free(&temp);
  12399. #endif
  12400. return EXPECT_RESULT();
  12401. } /* END test_wc_Sha512Copy */
  12402. static int test_wc_InitSha512_224(void)
  12403. {
  12404. EXPECT_DECLS;
  12405. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12406. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12407. wc_Sha512 sha512;
  12408. /* Test good arg. */
  12409. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12410. /* Test bad arg. */
  12411. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12412. wc_Sha512_224Free(&sha512);
  12413. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12414. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12415. return EXPECT_RESULT();
  12416. }
  12417. static int test_wc_Sha512_224Update(void)
  12418. {
  12419. EXPECT_DECLS;
  12420. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12421. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12422. wc_Sha512 sha512;
  12423. byte hash[WC_SHA512_DIGEST_SIZE];
  12424. testVector a, c;
  12425. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12426. /* Input. */
  12427. a.input = "a";
  12428. a.inLen = XSTRLEN(a.input);
  12429. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12430. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12431. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12432. 0);
  12433. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12434. /* Update input. */
  12435. a.input = "abc";
  12436. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12437. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12438. a.inLen = XSTRLEN(a.input);
  12439. a.outLen = XSTRLEN(a.output);
  12440. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12441. 0);
  12442. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12443. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12444. c.input = NULL;
  12445. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12446. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12447. BAD_FUNC_ARG);
  12448. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12449. BAD_FUNC_ARG);
  12450. wc_Sha512_224Free(&sha512);
  12451. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12452. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12453. return EXPECT_RESULT();
  12454. }
  12455. static int test_wc_Sha512_224Final(void)
  12456. {
  12457. EXPECT_DECLS;
  12458. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12459. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12460. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12461. TEST_SUCCESS);
  12462. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12463. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12464. return EXPECT_RESULT();
  12465. }
  12466. static int test_wc_Sha512_224GetFlags(void)
  12467. {
  12468. EXPECT_DECLS;
  12469. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12470. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12471. wc_Sha512 sha512;
  12472. wc_Sha512 copy;
  12473. word32 flags = 0;
  12474. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12475. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12476. /* Initialize */
  12477. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12478. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12479. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12480. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12481. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12482. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12483. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12484. wc_Sha512_224Free(&copy);
  12485. wc_Sha512_224Free(&sha512);
  12486. #endif
  12487. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12488. return EXPECT_RESULT();
  12489. }
  12490. static int test_wc_Sha512_224FinalRaw(void)
  12491. {
  12492. EXPECT_DECLS;
  12493. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12494. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12495. !defined(WOLFSSL_NO_HASH_RAW)
  12496. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12497. TEST_SUCCESS);
  12498. #endif
  12499. return EXPECT_RESULT();
  12500. }
  12501. static int test_wc_Sha512_224Free(void)
  12502. {
  12503. EXPECT_DECLS;
  12504. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12505. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12506. wc_Sha512_224Free(NULL);
  12507. /* Set result to SUCCESS. */
  12508. ExpectTrue(1);
  12509. #endif
  12510. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12511. return EXPECT_RESULT();
  12512. }
  12513. static int test_wc_Sha512_224GetHash(void)
  12514. {
  12515. EXPECT_DECLS;
  12516. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12517. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12518. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12519. TEST_SUCCESS);
  12520. #endif
  12521. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12522. return EXPECT_RESULT();
  12523. }
  12524. static int test_wc_Sha512_224Copy(void)
  12525. {
  12526. EXPECT_DECLS;
  12527. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12528. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12529. wc_Sha512 sha512;
  12530. wc_Sha512 temp;
  12531. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12532. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12533. /* Initialize */
  12534. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12535. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12536. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12537. /* test bad arguments*/
  12538. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12539. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12540. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12541. wc_Sha512_224Free(&sha512);
  12542. wc_Sha512_224Free(&temp);
  12543. #endif
  12544. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12545. return EXPECT_RESULT();
  12546. }
  12547. static int test_wc_InitSha512_256(void)
  12548. {
  12549. EXPECT_DECLS;
  12550. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12551. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12552. wc_Sha512 sha512;
  12553. /* Test good arg. */
  12554. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12555. /* Test bad arg. */
  12556. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12557. wc_Sha512_256Free(&sha512);
  12558. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12559. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12560. return EXPECT_RESULT();
  12561. }
  12562. static int test_wc_Sha512_256Update(void)
  12563. {
  12564. EXPECT_DECLS;
  12565. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12566. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12567. wc_Sha512 sha512;
  12568. byte hash[WC_SHA512_DIGEST_SIZE];
  12569. testVector a, c;
  12570. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12571. /* Input. */
  12572. a.input = "a";
  12573. a.inLen = XSTRLEN(a.input);
  12574. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12575. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12576. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12577. 0);
  12578. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12579. /* Update input. */
  12580. a.input = "abc";
  12581. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12582. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12583. "\x07\xe7\xaf\x23";
  12584. a.inLen = XSTRLEN(a.input);
  12585. a.outLen = XSTRLEN(a.output);
  12586. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12587. 0);
  12588. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12589. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12590. c.input = NULL;
  12591. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12592. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12593. BAD_FUNC_ARG);
  12594. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12595. BAD_FUNC_ARG);
  12596. wc_Sha512_256Free(&sha512);
  12597. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12598. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12599. return EXPECT_RESULT();
  12600. }
  12601. static int test_wc_Sha512_256Final(void)
  12602. {
  12603. EXPECT_DECLS;
  12604. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12605. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12606. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12607. TEST_SUCCESS);
  12608. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12609. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12610. return EXPECT_RESULT();
  12611. }
  12612. static int test_wc_Sha512_256GetFlags(void)
  12613. {
  12614. EXPECT_DECLS;
  12615. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12616. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12617. wc_Sha512 sha512, copy;
  12618. word32 flags = 0;
  12619. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12620. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12621. /* Initialize */
  12622. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12623. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12624. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12625. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12626. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12627. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12628. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12629. wc_Sha512_256Free(&sha512);
  12630. #endif
  12631. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12632. return EXPECT_RESULT();
  12633. }
  12634. static int test_wc_Sha512_256FinalRaw(void)
  12635. {
  12636. EXPECT_DECLS;
  12637. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12638. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12639. !defined(WOLFSSL_NO_HASH_RAW)
  12640. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12641. TEST_SUCCESS);
  12642. #endif
  12643. return EXPECT_RESULT();
  12644. }
  12645. static int test_wc_Sha512_256Free(void)
  12646. {
  12647. EXPECT_DECLS;
  12648. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12649. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12650. wc_Sha512_256Free(NULL);
  12651. /* Set result to SUCCESS. */
  12652. ExpectTrue(1);
  12653. #endif
  12654. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12655. return EXPECT_RESULT();
  12656. }
  12657. static int test_wc_Sha512_256GetHash(void)
  12658. {
  12659. EXPECT_DECLS;
  12660. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12661. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12662. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12663. TEST_SUCCESS);
  12664. #endif
  12665. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12666. return EXPECT_RESULT();
  12667. }
  12668. static int test_wc_Sha512_256Copy(void)
  12669. {
  12670. EXPECT_DECLS;
  12671. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12672. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12673. wc_Sha512 sha512;
  12674. wc_Sha512 temp;
  12675. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12676. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12677. /* Initialize */
  12678. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12679. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12680. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12681. /* test bad arguments*/
  12682. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12683. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12684. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12685. wc_Sha512_256Free(&sha512);
  12686. wc_Sha512_256Free(&temp);
  12687. #endif
  12688. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12689. return EXPECT_RESULT();
  12690. }
  12691. /*
  12692. * Testing wc_InitSha384()
  12693. */
  12694. static int test_wc_InitSha384(void)
  12695. {
  12696. EXPECT_DECLS;
  12697. #ifdef WOLFSSL_SHA384
  12698. wc_Sha384 sha384;
  12699. /* Test good arg. */
  12700. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12701. /* Test bad arg. */
  12702. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12703. wc_Sha384Free(&sha384);
  12704. #endif
  12705. return EXPECT_RESULT();
  12706. } /* END test_wc_InitSha384 */
  12707. /*
  12708. * test wc_Sha384Update()
  12709. */
  12710. static int test_wc_Sha384Update(void)
  12711. {
  12712. EXPECT_DECLS;
  12713. #ifdef WOLFSSL_SHA384
  12714. wc_Sha384 sha384;
  12715. byte hash[WC_SHA384_DIGEST_SIZE];
  12716. testVector a, b, c;
  12717. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12718. /* Input */
  12719. a.input = "a";
  12720. a.inLen = XSTRLEN(a.input);
  12721. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12722. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12723. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12724. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12725. /* Update input. */
  12726. a.input = "abc";
  12727. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12728. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12729. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12730. "\xc8\x25\xa7";
  12731. a.inLen = XSTRLEN(a.input);
  12732. a.outLen = XSTRLEN(a.output);
  12733. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12734. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12735. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12736. /* Pass in bad values. */
  12737. b.input = NULL;
  12738. b.inLen = 0;
  12739. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12740. c.input = NULL;
  12741. c.inLen = WC_SHA384_DIGEST_SIZE;
  12742. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12743. BAD_FUNC_ARG);
  12744. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12745. BAD_FUNC_ARG);
  12746. wc_Sha384Free(&sha384);
  12747. #endif
  12748. return EXPECT_RESULT();
  12749. } /* END test_wc_Sha384Update */
  12750. /*
  12751. * Unit test function for wc_Sha384Final();
  12752. */
  12753. static int test_wc_Sha384Final(void)
  12754. {
  12755. EXPECT_DECLS;
  12756. #ifdef WOLFSSL_SHA384
  12757. wc_Sha384 sha384;
  12758. byte* hash_test[3];
  12759. byte hash1[WC_SHA384_DIGEST_SIZE];
  12760. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12761. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12762. int times, i;
  12763. /* Initialize */
  12764. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12765. hash_test[0] = hash1;
  12766. hash_test[1] = hash2;
  12767. hash_test[2] = hash3;
  12768. times = sizeof(hash_test) / sizeof(byte*);
  12769. /* Good test args. */
  12770. for (i = 0; i < times; i++) {
  12771. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12772. }
  12773. /* Test bad args. */
  12774. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12775. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12776. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12777. wc_Sha384Free(&sha384);
  12778. #endif
  12779. return EXPECT_RESULT();
  12780. } /* END test_wc_Sha384Final */
  12781. /*
  12782. * Unit test function for wc_Sha384GetFlags()
  12783. */
  12784. static int test_wc_Sha384GetFlags(void)
  12785. {
  12786. EXPECT_DECLS;
  12787. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12788. wc_Sha384 sha384;
  12789. word32 flags = 0;
  12790. /* Initialize */
  12791. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12792. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12793. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12794. wc_Sha384Free(&sha384);
  12795. #endif
  12796. return EXPECT_RESULT();
  12797. } /* END test_wc_Sha384GetFlags */
  12798. /*
  12799. * Unit test function for wc_Sha384FinalRaw()
  12800. */
  12801. static int test_wc_Sha384FinalRaw(void)
  12802. {
  12803. EXPECT_DECLS;
  12804. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12805. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12806. !defined(WOLFSSL_NO_HASH_RAW)
  12807. wc_Sha384 sha384;
  12808. byte* hash_test[3];
  12809. byte hash1[WC_SHA384_DIGEST_SIZE];
  12810. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12811. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12812. int times, i;
  12813. /* Initialize */
  12814. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12815. hash_test[0] = hash1;
  12816. hash_test[1] = hash2;
  12817. hash_test[2] = hash3;
  12818. times = sizeof(hash_test) / sizeof(byte*);
  12819. /* Good test args. */
  12820. for (i = 0; i < times; i++) {
  12821. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12822. }
  12823. /* Test bad args. */
  12824. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12825. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12826. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12827. wc_Sha384Free(&sha384);
  12828. #endif
  12829. return EXPECT_RESULT();
  12830. } /* END test_wc_Sha384FinalRaw */
  12831. /*
  12832. * Unit test function for wc_Sha384Free()
  12833. */
  12834. static int test_wc_Sha384Free(void)
  12835. {
  12836. EXPECT_DECLS;
  12837. #ifdef WOLFSSL_SHA384
  12838. wc_Sha384Free(NULL);
  12839. /* Set result to SUCCESS. */
  12840. ExpectTrue(1);
  12841. #endif
  12842. return EXPECT_RESULT();
  12843. } /* END test_wc_Sha384Free */
  12844. /*
  12845. * Unit test function for wc_Sha384GetHash()
  12846. */
  12847. static int test_wc_Sha384GetHash(void)
  12848. {
  12849. EXPECT_DECLS;
  12850. #ifdef WOLFSSL_SHA384
  12851. wc_Sha384 sha384;
  12852. byte hash1[WC_SHA384_DIGEST_SIZE];
  12853. /* Initialize */
  12854. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12855. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12856. /* test bad arguments*/
  12857. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12858. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12859. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12860. wc_Sha384Free(&sha384);
  12861. #endif
  12862. return EXPECT_RESULT();
  12863. } /* END test_wc_Sha384GetHash */
  12864. /*
  12865. * Unit test function for wc_Sha384Copy()
  12866. */
  12867. static int test_wc_Sha384Copy(void)
  12868. {
  12869. EXPECT_DECLS;
  12870. #ifdef WOLFSSL_SHA384
  12871. wc_Sha384 sha384;
  12872. wc_Sha384 temp;
  12873. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12874. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12875. /* Initialize */
  12876. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12877. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12878. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12879. /* test bad arguments*/
  12880. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12881. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12882. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12883. wc_Sha384Free(&sha384);
  12884. wc_Sha384Free(&temp);
  12885. #endif
  12886. return EXPECT_RESULT();
  12887. } /* END test_wc_Sha384Copy */
  12888. /*
  12889. * Testing wc_InitSha224();
  12890. */
  12891. static int test_wc_InitSha224(void)
  12892. {
  12893. EXPECT_DECLS;
  12894. #ifdef WOLFSSL_SHA224
  12895. wc_Sha224 sha224;
  12896. /* Test good arg. */
  12897. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12898. /* Test bad arg. */
  12899. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12900. wc_Sha224Free(&sha224);
  12901. #endif
  12902. return EXPECT_RESULT();
  12903. } /* END test_wc_InitSha224 */
  12904. /*
  12905. * Unit test on wc_Sha224Update
  12906. */
  12907. static int test_wc_Sha224Update(void)
  12908. {
  12909. EXPECT_DECLS;
  12910. #ifdef WOLFSSL_SHA224
  12911. wc_Sha224 sha224;
  12912. byte hash[WC_SHA224_DIGEST_SIZE];
  12913. testVector a, b, c;
  12914. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12915. /* Input. */
  12916. a.input = "a";
  12917. a.inLen = XSTRLEN(a.input);
  12918. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12919. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12920. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12921. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12922. /* Update input. */
  12923. a.input = "abc";
  12924. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12925. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12926. a.inLen = XSTRLEN(a.input);
  12927. a.outLen = XSTRLEN(a.output);
  12928. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12929. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12930. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12931. /* Pass in bad values. */
  12932. b.input = NULL;
  12933. b.inLen = 0;
  12934. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12935. c.input = NULL;
  12936. c.inLen = WC_SHA224_DIGEST_SIZE;
  12937. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12938. BAD_FUNC_ARG);
  12939. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12940. BAD_FUNC_ARG);
  12941. wc_Sha224Free(&sha224);
  12942. #endif
  12943. return EXPECT_RESULT();
  12944. } /* END test_wc_Sha224Update */
  12945. /*
  12946. * Unit test for wc_Sha224Final();
  12947. */
  12948. static int test_wc_Sha224Final(void)
  12949. {
  12950. EXPECT_DECLS;
  12951. #ifdef WOLFSSL_SHA224
  12952. wc_Sha224 sha224;
  12953. byte* hash_test[3];
  12954. byte hash1[WC_SHA224_DIGEST_SIZE];
  12955. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12956. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12957. int times, i;
  12958. /* Initialize */
  12959. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12960. hash_test[0] = hash1;
  12961. hash_test[1] = hash2;
  12962. hash_test[2] = hash3;
  12963. times = sizeof(hash_test) / sizeof(byte*);
  12964. /* Good test args. */
  12965. /* Testing oversized buffers. */
  12966. for (i = 0; i < times; i++) {
  12967. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12968. }
  12969. /* Test bad args. */
  12970. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12971. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12972. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12973. wc_Sha224Free(&sha224);
  12974. #endif
  12975. return EXPECT_RESULT();
  12976. } /* END test_wc_Sha224Final */
  12977. /*
  12978. * Unit test function for wc_Sha224SetFlags()
  12979. */
  12980. static int test_wc_Sha224SetFlags(void)
  12981. {
  12982. EXPECT_DECLS;
  12983. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12984. wc_Sha224 sha224;
  12985. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12986. /* Initialize */
  12987. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12988. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12989. flags = 0;
  12990. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12991. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12992. wc_Sha224Free(&sha224);
  12993. #endif
  12994. return EXPECT_RESULT();
  12995. } /* END test_wc_Sha224SetFlags */
  12996. /*
  12997. * Unit test function for wc_Sha224GetFlags()
  12998. */
  12999. static int test_wc_Sha224GetFlags(void)
  13000. {
  13001. EXPECT_DECLS;
  13002. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  13003. wc_Sha224 sha224;
  13004. word32 flags = 0;
  13005. /* Initialize */
  13006. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13007. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  13008. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13009. wc_Sha224Free(&sha224);
  13010. #endif
  13011. return EXPECT_RESULT();
  13012. } /* END test_wc_Sha224GetFlags */
  13013. /*
  13014. * Unit test function for wc_Sha224Free()
  13015. */
  13016. static int test_wc_Sha224Free(void)
  13017. {
  13018. EXPECT_DECLS;
  13019. #ifdef WOLFSSL_SHA224
  13020. wc_Sha224Free(NULL);
  13021. /* Set result to SUCCESS. */
  13022. ExpectTrue(1);
  13023. #endif
  13024. return EXPECT_RESULT();
  13025. } /* END test_wc_Sha224Free */
  13026. /*
  13027. * Unit test function for wc_Sha224GetHash()
  13028. */
  13029. static int test_wc_Sha224GetHash(void)
  13030. {
  13031. EXPECT_DECLS;
  13032. #ifdef WOLFSSL_SHA224
  13033. wc_Sha224 sha224;
  13034. byte hash1[WC_SHA224_DIGEST_SIZE];
  13035. /* Initialize */
  13036. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13037. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13038. /* test bad arguments*/
  13039. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13040. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13041. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13042. wc_Sha224Free(&sha224);
  13043. #endif
  13044. return EXPECT_RESULT();
  13045. } /* END test_wc_Sha224GetHash */
  13046. /*
  13047. * Unit test function for wc_Sha224Copy()
  13048. */
  13049. static int test_wc_Sha224Copy(void)
  13050. {
  13051. EXPECT_DECLS;
  13052. #ifdef WOLFSSL_SHA224
  13053. wc_Sha224 sha224;
  13054. wc_Sha224 temp;
  13055. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13056. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13057. /* Initialize */
  13058. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13059. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13060. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13061. /* test bad arguments*/
  13062. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13063. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13064. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13065. wc_Sha224Free(&sha224);
  13066. wc_Sha224Free(&temp);
  13067. #endif
  13068. return EXPECT_RESULT();
  13069. } /* END test_wc_Sha224Copy */
  13070. /*
  13071. * Testing wc_InitRipeMd()
  13072. */
  13073. static int test_wc_InitRipeMd(void)
  13074. {
  13075. EXPECT_DECLS;
  13076. #ifdef WOLFSSL_RIPEMD
  13077. RipeMd ripemd;
  13078. /* Test good arg. */
  13079. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13080. /* Test bad arg. */
  13081. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13082. #endif
  13083. return EXPECT_RESULT();
  13084. } /* END test_wc_InitRipeMd */
  13085. /*
  13086. * Testing wc_RipeMdUpdate()
  13087. */
  13088. static int test_wc_RipeMdUpdate(void)
  13089. {
  13090. EXPECT_DECLS;
  13091. #ifdef WOLFSSL_RIPEMD
  13092. RipeMd ripemd;
  13093. byte hash[RIPEMD_DIGEST_SIZE];
  13094. testVector a, b, c;
  13095. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13096. /* Input */
  13097. a.input = "a";
  13098. a.inLen = XSTRLEN(a.input);
  13099. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13100. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13101. /* Update input. */
  13102. a.input = "abc";
  13103. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13104. "\xb0\x87\xf1\x5a\x0b\xfc";
  13105. a.inLen = XSTRLEN(a.input);
  13106. a.outLen = XSTRLEN(a.output);
  13107. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13108. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13109. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13110. /* Pass in bad values. */
  13111. b.input = NULL;
  13112. b.inLen = 0;
  13113. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13114. c.input = NULL;
  13115. c.inLen = RIPEMD_DIGEST_SIZE;
  13116. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13117. BAD_FUNC_ARG);
  13118. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13119. BAD_FUNC_ARG);
  13120. #endif
  13121. return EXPECT_RESULT();
  13122. } /* END test_wc_RipeMdUdpate */
  13123. /*
  13124. * Unit test function for wc_RipeMdFinal()
  13125. */
  13126. static int test_wc_RipeMdFinal(void)
  13127. {
  13128. EXPECT_DECLS;
  13129. #ifdef WOLFSSL_RIPEMD
  13130. RipeMd ripemd;
  13131. byte* hash_test[3];
  13132. byte hash1[RIPEMD_DIGEST_SIZE];
  13133. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13134. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13135. int times, i;
  13136. /* Initialize */
  13137. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13138. hash_test[0] = hash1;
  13139. hash_test[1] = hash2;
  13140. hash_test[2] = hash3;
  13141. times = sizeof(hash_test) / sizeof(byte*);
  13142. /* Testing oversized buffers. */
  13143. for (i = 0; i < times; i++) {
  13144. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13145. }
  13146. /* Test bad args. */
  13147. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13148. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13149. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13150. #endif
  13151. return EXPECT_RESULT();
  13152. } /* END test_wc_RipeMdFinal */
  13153. /*
  13154. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13155. * wc_InitSha3_512
  13156. */
  13157. static int test_wc_InitSha3(void)
  13158. {
  13159. EXPECT_DECLS;
  13160. #if defined(WOLFSSL_SHA3)
  13161. wc_Sha3 sha3;
  13162. (void)sha3;
  13163. #if !defined(WOLFSSL_NOSHA3_224)
  13164. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13165. /* Test bad args. */
  13166. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13167. wc_Sha3_224_Free(&sha3);
  13168. #endif /* NOSHA3_224 */
  13169. #if !defined(WOLFSSL_NOSHA3_256)
  13170. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13171. /* Test bad args. */
  13172. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13173. wc_Sha3_256_Free(&sha3);
  13174. #endif /* NOSHA3_256 */
  13175. #if !defined(WOLFSSL_NOSHA3_384)
  13176. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13177. /* Test bad args. */
  13178. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13179. wc_Sha3_384_Free(&sha3);
  13180. #endif /* NOSHA3_384 */
  13181. #if !defined(WOLFSSL_NOSHA3_512)
  13182. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13183. /* Test bad args. */
  13184. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13185. wc_Sha3_512_Free(&sha3);
  13186. #endif /* NOSHA3_512 */
  13187. #endif
  13188. return EXPECT_RESULT();
  13189. } /* END test_wc_InitSha3 */
  13190. /*
  13191. * Testing wc_Sha3_Update()
  13192. */
  13193. static int testing_wc_Sha3_Update(void)
  13194. {
  13195. EXPECT_DECLS;
  13196. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13197. !defined(WOLFSSL_AFALG_XILINX)
  13198. wc_Sha3 sha3;
  13199. byte msg[] = "Everybody's working for the weekend.";
  13200. byte msg2[] = "Everybody gets Friday off.";
  13201. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13202. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13203. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13204. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13205. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13206. word32 msglen = sizeof(msg) - 1;
  13207. word32 msg2len = sizeof(msg2);
  13208. word32 msgCmplen = sizeof(msgCmp);
  13209. #if !defined(WOLFSSL_NOSHA3_224)
  13210. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13211. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13212. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13213. ExpectTrue(sha3.i == msglen);
  13214. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13215. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13216. /* Pass bad args. */
  13217. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13218. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13219. wc_Sha3_224_Free(&sha3);
  13220. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13221. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13222. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13223. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13224. wc_Sha3_224_Free(&sha3);
  13225. #endif /* SHA3_224 */
  13226. #if !defined(WOLFSSL_NOSHA3_256)
  13227. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13228. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13229. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13230. ExpectTrue(sha3.i == msglen);
  13231. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13232. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13233. /* Pass bad args. */
  13234. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13235. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13236. wc_Sha3_256_Free(&sha3);
  13237. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13238. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13239. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13240. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13241. wc_Sha3_256_Free(&sha3);
  13242. #endif /* SHA3_256 */
  13243. #if !defined(WOLFSSL_NOSHA3_384)
  13244. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13245. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13246. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13247. ExpectTrue(sha3.i == msglen);
  13248. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13249. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13250. /* Pass bad args. */
  13251. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13252. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13253. wc_Sha3_384_Free(&sha3);
  13254. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13255. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13256. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13257. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13258. wc_Sha3_384_Free(&sha3);
  13259. #endif /* SHA3_384 */
  13260. #if !defined(WOLFSSL_NOSHA3_512)
  13261. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13262. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13263. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13264. ExpectTrue(sha3.i == msglen);
  13265. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13266. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13267. /* Pass bad args. */
  13268. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13269. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13270. wc_Sha3_512_Free(&sha3);
  13271. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13272. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13273. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13274. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13275. wc_Sha3_512_Free(&sha3);
  13276. #endif /* SHA3_512 */
  13277. #endif /* WOLFSSL_SHA3 */
  13278. return EXPECT_RESULT();
  13279. } /* END testing_wc_Sha3_Update */
  13280. /*
  13281. * Testing wc_Sha3_224_Final()
  13282. */
  13283. static int test_wc_Sha3_224_Final(void)
  13284. {
  13285. EXPECT_DECLS;
  13286. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13287. wc_Sha3 sha3;
  13288. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13289. "nopnopq";
  13290. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13291. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13292. "\x64\xea\xd0\xfc\xce\x33";
  13293. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13294. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13295. /* Init stack variables. */
  13296. XMEMSET(hash, 0, sizeof(hash));
  13297. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13298. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13299. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13300. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13301. /* Test bad args. */
  13302. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13303. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13304. wc_Sha3_224_Free(&sha3);
  13305. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13306. /* Init stack variables. */
  13307. XMEMSET(hash, 0, sizeof(hash));
  13308. XMEMSET(hashRet, 0, sizeof(hashRet));
  13309. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13310. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13311. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13312. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13313. /* Test bad args. */
  13314. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13315. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13316. wc_Sha3_224_Free(&sha3);
  13317. #endif
  13318. return EXPECT_RESULT();
  13319. } /* END test_wc_Sha3_224_Final */
  13320. /*
  13321. * Testing wc_Sha3_256_Final()
  13322. */
  13323. static int test_wc_Sha3_256_Final(void)
  13324. {
  13325. EXPECT_DECLS;
  13326. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13327. wc_Sha3 sha3;
  13328. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13329. "nopnopq";
  13330. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13331. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13332. "\xdd\x97\x49\x6d\x33\x76";
  13333. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13334. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13335. /* Init stack variables. */
  13336. XMEMSET(hash, 0, sizeof(hash));
  13337. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13338. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13339. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13340. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13341. /* Test bad args. */
  13342. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13343. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13344. wc_Sha3_256_Free(&sha3);
  13345. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13346. /* Init stack variables. */
  13347. XMEMSET(hash, 0, sizeof(hash));
  13348. XMEMSET(hashRet, 0, sizeof(hashRet));
  13349. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13350. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13351. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13352. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13353. /* Test bad args. */
  13354. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13355. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13356. wc_Sha3_256_Free(&sha3);
  13357. #endif
  13358. return EXPECT_RESULT();
  13359. } /* END test_wc_Sha3_256_Final */
  13360. /*
  13361. * Testing wc_Sha3_384_Final()
  13362. */
  13363. static int test_wc_Sha3_384_Final(void)
  13364. {
  13365. EXPECT_DECLS;
  13366. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13367. wc_Sha3 sha3;
  13368. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13369. "nopnopq";
  13370. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13371. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13372. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13373. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13374. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13375. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13376. /* Init stack variables. */
  13377. XMEMSET(hash, 0, sizeof(hash));
  13378. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13379. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13380. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13381. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13382. /* Test bad args. */
  13383. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13384. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13385. wc_Sha3_384_Free(&sha3);
  13386. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13387. /* Init stack variables. */
  13388. XMEMSET(hash, 0, sizeof(hash));
  13389. XMEMSET(hashRet, 0, sizeof(hashRet));
  13390. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13391. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13392. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13393. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13394. /* Test bad args. */
  13395. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13396. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13397. wc_Sha3_384_Free(&sha3);
  13398. #endif
  13399. return EXPECT_RESULT();
  13400. } /* END test_wc_Sha3_384_Final */
  13401. /*
  13402. * Testing wc_Sha3_512_Final()
  13403. */
  13404. static int test_wc_Sha3_512_Final(void)
  13405. {
  13406. EXPECT_DECLS;
  13407. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13408. !defined(WOLFSSL_NOSHA3_384)
  13409. wc_Sha3 sha3;
  13410. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13411. "nopnopq";
  13412. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13413. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13414. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13415. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13416. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13417. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13418. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13419. /* Init stack variables. */
  13420. XMEMSET(hash, 0, sizeof(hash));
  13421. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13422. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13423. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13424. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13425. /* Test bad args. */
  13426. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13427. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13428. wc_Sha3_512_Free(&sha3);
  13429. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13430. /* Init stack variables. */
  13431. XMEMSET(hash, 0, sizeof(hash));
  13432. XMEMSET(hashRet, 0, sizeof(hashRet));
  13433. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13434. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13435. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13436. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13437. /* Test bad args. */
  13438. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13439. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13440. wc_Sha3_512_Free(&sha3);
  13441. #endif
  13442. return EXPECT_RESULT();
  13443. } /* END test_wc_Sha3_512_Final */
  13444. /*
  13445. * Testing wc_Sha3_224_Copy()
  13446. */
  13447. static int test_wc_Sha3_224_Copy(void)
  13448. {
  13449. EXPECT_DECLS;
  13450. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13451. wc_Sha3 sha3, sha3Cpy;
  13452. const char* msg = TEST_STRING;
  13453. word32 msglen = (word32)TEST_STRING_SZ;
  13454. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13455. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13456. XMEMSET(hash, 0, sizeof(hash));
  13457. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13458. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13459. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13460. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13461. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13462. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13463. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13464. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13465. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13466. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13467. /* Test bad args. */
  13468. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13469. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13470. wc_Sha3_224_Free(&sha3);
  13471. wc_Sha3_224_Free(&sha3Cpy);
  13472. #endif
  13473. return EXPECT_RESULT();
  13474. } /* END test_wc_Sha3_224_Copy */
  13475. /*
  13476. * Testing wc_Sha3_256_Copy()
  13477. */
  13478. static int test_wc_Sha3_256_Copy(void)
  13479. {
  13480. EXPECT_DECLS;
  13481. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13482. wc_Sha3 sha3, sha3Cpy;
  13483. const char* msg = TEST_STRING;
  13484. word32 msglen = (word32)TEST_STRING_SZ;
  13485. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13486. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13487. XMEMSET(hash, 0, sizeof(hash));
  13488. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13489. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13490. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13491. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13492. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13493. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13494. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13495. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13496. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13497. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13498. /* Test bad args. */
  13499. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13500. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13501. wc_Sha3_256_Free(&sha3);
  13502. wc_Sha3_256_Free(&sha3Cpy);
  13503. #endif
  13504. return EXPECT_RESULT();
  13505. } /* END test_wc_Sha3_256_Copy */
  13506. /*
  13507. * Testing wc_Sha3_384_Copy()
  13508. */
  13509. static int test_wc_Sha3_384_Copy(void)
  13510. {
  13511. EXPECT_DECLS;
  13512. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13513. wc_Sha3 sha3, sha3Cpy;
  13514. const char* msg = TEST_STRING;
  13515. word32 msglen = (word32)TEST_STRING_SZ;
  13516. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13517. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13518. XMEMSET(hash, 0, sizeof(hash));
  13519. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13520. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13521. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13522. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13523. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13524. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13525. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13526. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13527. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13528. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13529. /* Test bad args. */
  13530. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13531. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13532. wc_Sha3_384_Free(&sha3);
  13533. wc_Sha3_384_Free(&sha3Cpy);
  13534. #endif
  13535. return EXPECT_RESULT();
  13536. } /* END test_wc_Sha3_384_Copy */
  13537. /*
  13538. * Testing wc_Sha3_512_Copy()
  13539. */
  13540. static int test_wc_Sha3_512_Copy(void)
  13541. {
  13542. EXPECT_DECLS;
  13543. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13544. wc_Sha3 sha3, sha3Cpy;
  13545. const char* msg = TEST_STRING;
  13546. word32 msglen = (word32)TEST_STRING_SZ;
  13547. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13548. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13549. XMEMSET(hash, 0, sizeof(hash));
  13550. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13551. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13552. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13553. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13554. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13555. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13556. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13557. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13558. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13559. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13560. /* Test bad args. */
  13561. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13562. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13563. wc_Sha3_512_Free(&sha3);
  13564. wc_Sha3_512_Free(&sha3Cpy);
  13565. #endif
  13566. return EXPECT_RESULT();
  13567. } /* END test_wc_Sha3_512_Copy */
  13568. /*
  13569. * Unit test function for wc_Sha3_GetFlags()
  13570. */
  13571. static int test_wc_Sha3_GetFlags(void)
  13572. {
  13573. EXPECT_DECLS;
  13574. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13575. wc_Sha3 sha3;
  13576. word32 flags = 0;
  13577. /* Initialize */
  13578. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13579. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13580. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13581. wc_Sha3_224_Free(&sha3);
  13582. #endif
  13583. return EXPECT_RESULT();
  13584. } /* END test_wc_Sha3_GetFlags */
  13585. static int test_wc_InitShake256(void)
  13586. {
  13587. EXPECT_DECLS;
  13588. #ifdef WOLFSSL_SHAKE256
  13589. wc_Shake shake;
  13590. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13591. /* Test bad args. */
  13592. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13593. wc_Shake256_Free(&shake);
  13594. #endif
  13595. return EXPECT_RESULT();
  13596. }
  13597. static int testing_wc_Shake256_Update(void)
  13598. {
  13599. EXPECT_DECLS;
  13600. #ifdef WOLFSSL_SHAKE256
  13601. wc_Shake shake;
  13602. byte msg[] = "Everybody's working for the weekend.";
  13603. byte msg2[] = "Everybody gets Friday off.";
  13604. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13605. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13606. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13607. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13608. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13609. word32 msglen = sizeof(msg) - 1;
  13610. word32 msg2len = sizeof(msg2);
  13611. word32 msgCmplen = sizeof(msgCmp);
  13612. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13613. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13614. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13615. ExpectTrue(shake.i == msglen);
  13616. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13617. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13618. /* Pass bad args. */
  13619. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13620. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13621. wc_Shake256_Free(&shake);
  13622. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13623. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13624. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13625. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13626. wc_Shake256_Free(&shake);
  13627. #endif /* WOLFSSL_SHAKE256 */
  13628. return EXPECT_RESULT();
  13629. }
  13630. static int test_wc_Shake256_Final(void)
  13631. {
  13632. EXPECT_DECLS;
  13633. #ifdef WOLFSSL_SHAKE256
  13634. wc_Shake shake;
  13635. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13636. "nopnopq";
  13637. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13638. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13639. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13640. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13641. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13642. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13643. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13644. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13645. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13646. byte hash[114];
  13647. /* Init stack variables. */
  13648. XMEMSET(hash, 0, sizeof(hash));
  13649. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13650. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13651. 0);
  13652. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13653. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13654. /* Test bad args. */
  13655. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13656. BAD_FUNC_ARG);
  13657. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13658. BAD_FUNC_ARG);
  13659. wc_Shake256_Free(&shake);
  13660. #endif
  13661. return EXPECT_RESULT();
  13662. }
  13663. /*
  13664. * Testing wc_Shake256_Copy()
  13665. */
  13666. static int test_wc_Shake256_Copy(void)
  13667. {
  13668. EXPECT_DECLS;
  13669. #ifdef WOLFSSL_SHAKE256
  13670. wc_Shake shake, shakeCpy;
  13671. const char* msg = TEST_STRING;
  13672. word32 msglen = (word32)TEST_STRING_SZ;
  13673. byte hash[144];
  13674. byte hashCpy[144];
  13675. word32 hashLen = sizeof(hash);
  13676. word32 hashLenCpy = sizeof(hashCpy);
  13677. XMEMSET(hash, 0, sizeof(hash));
  13678. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13679. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13680. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13681. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13682. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13683. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13684. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13685. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13686. /* Test bad args. */
  13687. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13688. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13689. wc_Shake256_Free(&shake);
  13690. wc_Shake256_Free(&shakeCpy);
  13691. #endif
  13692. return EXPECT_RESULT();
  13693. } /* END test_wc_Shake256_Copy */
  13694. /*
  13695. * Unit test function for wc_Shake256Hash()
  13696. */
  13697. static int test_wc_Shake256Hash(void)
  13698. {
  13699. EXPECT_DECLS;
  13700. #ifdef WOLFSSL_SHAKE256
  13701. const byte data[] = { /* Hello World */
  13702. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13703. 0x72,0x6c,0x64
  13704. };
  13705. word32 len = sizeof(data);
  13706. byte hash[144];
  13707. word32 hashLen = sizeof(hash);
  13708. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13709. #endif
  13710. return EXPECT_RESULT();
  13711. } /* END test_wc_Shake256Hash */
  13712. /*
  13713. * Testing wc_InitSm3(), wc_Sm3Free()
  13714. */
  13715. static int test_wc_InitSm3Free(void)
  13716. {
  13717. EXPECT_DECLS;
  13718. #ifdef WOLFSSL_SM3
  13719. wc_Sm3 sm3;
  13720. /* Invalid Parameters */
  13721. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13722. /* Valid Parameters */
  13723. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13724. wc_Sm3Free(NULL);
  13725. wc_Sm3Free(&sm3);
  13726. #endif
  13727. return EXPECT_RESULT();
  13728. } /* END test_wc_InitSm3 */
  13729. /*
  13730. * Testing wc_Sm3Update(), wc_Sm3Final()
  13731. */
  13732. static int test_wc_Sm3UpdateFinal(void)
  13733. {
  13734. EXPECT_DECLS;
  13735. #ifdef WOLFSSL_SM3
  13736. wc_Sm3 sm3;
  13737. byte data[WC_SM3_BLOCK_SIZE * 4];
  13738. byte hash[WC_SM3_DIGEST_SIZE];
  13739. byte calcHash[WC_SM3_DIGEST_SIZE];
  13740. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13741. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13742. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13743. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13744. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13745. };
  13746. word32 chunk;
  13747. word32 i;
  13748. XMEMSET(data, 0, sizeof(data));
  13749. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13750. /* Invalid Parameters */
  13751. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13752. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13753. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13754. /* Valid Parameters */
  13755. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13756. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13757. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13758. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13759. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13760. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13761. /* Ensure too many bytes for lengths. */
  13762. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13763. /* Invalid Parameters */
  13764. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13765. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13766. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13767. /* Valid Parameters */
  13768. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13769. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13770. /* Chunk tests. */
  13771. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13772. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13773. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13774. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13775. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13776. }
  13777. if (i < (word32)sizeof(data)) {
  13778. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13779. 0);
  13780. }
  13781. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13782. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13783. }
  13784. /* Not testing when the low 32-bit length overflows. */
  13785. wc_Sm3Free(&sm3);
  13786. #endif
  13787. return EXPECT_RESULT();
  13788. } /* END test_wc_Sm3Update */
  13789. /*
  13790. * Testing wc_Sm3GetHash()
  13791. */
  13792. static int test_wc_Sm3GetHash(void)
  13793. {
  13794. EXPECT_DECLS;
  13795. #ifdef WOLFSSL_SM3
  13796. wc_Sm3 sm3;
  13797. byte hash[WC_SM3_DIGEST_SIZE];
  13798. byte calcHash[WC_SM3_DIGEST_SIZE];
  13799. byte data[WC_SM3_BLOCK_SIZE];
  13800. XMEMSET(data, 0, sizeof(data));
  13801. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13802. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13803. /* Invalid Parameters */
  13804. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13805. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13806. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13807. /* Valid Parameters */
  13808. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13809. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13810. /* With update. */
  13811. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13812. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13813. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13814. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13815. wc_Sm3Free(&sm3);
  13816. #endif
  13817. return EXPECT_RESULT();
  13818. } /* END test_wc_Sm3Update */
  13819. /*
  13820. * Testing wc_Sm3Copy()
  13821. */
  13822. static int test_wc_Sm3Copy(void)
  13823. {
  13824. EXPECT_DECLS;
  13825. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13826. wc_Sm3 sm3;
  13827. wc_Sm3 sm3Copy;
  13828. byte hash[WC_SM3_DIGEST_SIZE];
  13829. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13830. byte data[WC_SM3_BLOCK_SIZE + 1];
  13831. int i;
  13832. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13833. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13834. /* Invalid Parameters */
  13835. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13836. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13837. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13838. /* Valid Parameters */
  13839. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13840. /* Ensure all parts of data updated during hashing are copied. */
  13841. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13842. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13843. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13844. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13845. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13846. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13847. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13848. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13849. }
  13850. wc_Sm3Free(&sm3Copy);
  13851. wc_Sm3Free(&sm3);
  13852. #endif
  13853. return EXPECT_RESULT();
  13854. } /* END test_wc_Sm3Copy */
  13855. /*
  13856. * Testing wc_Sm3FinalRaw()
  13857. */
  13858. static int test_wc_Sm3FinalRaw(void)
  13859. {
  13860. EXPECT_DECLS;
  13861. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13862. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13863. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13864. !defined(WOLFSSL_NO_HASH_RAW)
  13865. wc_Sm3 sm3;
  13866. byte hash1[WC_SM3_DIGEST_SIZE];
  13867. byte hash2[WC_SM3_DIGEST_SIZE];
  13868. byte hash3[WC_SM3_DIGEST_SIZE];
  13869. byte* hash_test[3] = { hash1, hash2, hash3 };
  13870. int times;
  13871. int i;
  13872. XMEMSET(&sm3, 0, sizeof(sm3));
  13873. /* Initialize */
  13874. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13875. /* Invalid Parameters */
  13876. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13877. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13878. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13879. times = sizeof(hash_test) / sizeof(byte*);
  13880. for (i = 0; i < times; i++) {
  13881. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13882. }
  13883. wc_Sm3Free(&sm3);
  13884. #endif
  13885. return EXPECT_RESULT();
  13886. } /* END test_wc_Sm3FinalRaw */
  13887. /*
  13888. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13889. */
  13890. static int test_wc_Sm3GetSetFlags(void)
  13891. {
  13892. EXPECT_DECLS;
  13893. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13894. wc_Sm3 sm3;
  13895. wc_Sm3 sm3Copy;
  13896. word32 flags = 0;
  13897. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13898. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13899. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13900. ExpectIntEQ(flags, 0);
  13901. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13902. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13903. ExpectIntEQ(flags, 0);
  13904. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13905. ExpectIntEQ(flags, 0);
  13906. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13907. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13908. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13909. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13910. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13911. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13912. wc_Sm3Free(&sm3Copy);
  13913. wc_Sm3Free(&sm3);
  13914. #endif
  13915. return EXPECT_RESULT();
  13916. } /* END test_wc_Sm3Update */
  13917. /*
  13918. * Testing wc_Sm3Hash()
  13919. */
  13920. static int test_wc_Sm3Hash(void)
  13921. {
  13922. EXPECT_DECLS;
  13923. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13924. byte data[WC_SM3_BLOCK_SIZE];
  13925. byte hash[WC_SM3_DIGEST_SIZE];
  13926. /* Invalid parameters. */
  13927. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13928. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13929. /* Valid parameters. */
  13930. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13931. #endif
  13932. return EXPECT_RESULT();
  13933. } /* END test_wc_Sm3Hash */
  13934. /*
  13935. * Test function for wc_HmacSetKey
  13936. */
  13937. static int test_wc_Md5HmacSetKey(void)
  13938. {
  13939. EXPECT_DECLS;
  13940. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13941. Hmac hmac;
  13942. int ret, times, itr;
  13943. const char* keys[]=
  13944. {
  13945. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13946. #ifndef HAVE_FIPS
  13947. "Jefe", /* smaller than minimum FIPS key size */
  13948. #endif
  13949. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13950. };
  13951. times = sizeof(keys) / sizeof(char*);
  13952. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13953. for (itr = 0; itr < times; itr++) {
  13954. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13955. (word32)XSTRLEN(keys[itr]));
  13956. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13957. wc_HmacFree(&hmac);
  13958. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13959. #else
  13960. ExpectIntEQ(ret, 0);
  13961. #endif
  13962. }
  13963. /* Bad args. */
  13964. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13965. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13966. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13967. BAD_FUNC_ARG);
  13968. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13969. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13970. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13971. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13972. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13973. #elif defined(HAVE_FIPS)
  13974. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13975. #else
  13976. ExpectIntEQ(ret, 0);
  13977. #endif
  13978. wc_HmacFree(&hmac);
  13979. #endif
  13980. return EXPECT_RESULT();
  13981. } /* END test_wc_Md5HmacSetKey */
  13982. /*
  13983. * testing wc_HmacSetKey() on wc_Sha hash.
  13984. */
  13985. static int test_wc_ShaHmacSetKey(void)
  13986. {
  13987. EXPECT_DECLS;
  13988. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13989. Hmac hmac;
  13990. int ret, times, itr;
  13991. const char* keys[]=
  13992. {
  13993. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13994. "\x0b\x0b\x0b",
  13995. #ifndef HAVE_FIPS
  13996. "Jefe", /* smaller than minimum FIPS key size */
  13997. #endif
  13998. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13999. "\xAA\xAA\xAA"
  14000. };
  14001. times = sizeof(keys) / sizeof(char*);
  14002. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14003. for (itr = 0; itr < times; itr++) {
  14004. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  14005. (word32)XSTRLEN(keys[itr])), 0);
  14006. }
  14007. /* Bad args. */
  14008. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  14009. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14010. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  14011. BAD_FUNC_ARG);
  14012. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14013. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14014. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  14015. #ifdef HAVE_FIPS
  14016. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14017. #else
  14018. ExpectIntEQ(ret, 0);
  14019. #endif
  14020. wc_HmacFree(&hmac);
  14021. #endif
  14022. return EXPECT_RESULT();
  14023. } /* END test_wc_ShaHmacSetKey() */
  14024. /*
  14025. * testing wc_HmacSetKey() on Sha224 hash.
  14026. */
  14027. static int test_wc_Sha224HmacSetKey(void)
  14028. {
  14029. EXPECT_DECLS;
  14030. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14031. Hmac hmac;
  14032. int ret, times, itr;
  14033. const char* keys[]=
  14034. {
  14035. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14036. "\x0b\x0b\x0b",
  14037. #ifndef HAVE_FIPS
  14038. "Jefe", /* smaller than minimum FIPS key size */
  14039. #endif
  14040. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14041. "\xAA\xAA\xAA"
  14042. };
  14043. times = sizeof(keys) / sizeof(char*);
  14044. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14045. for (itr = 0; itr < times; itr++) {
  14046. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14047. (word32)XSTRLEN(keys[itr])), 0);
  14048. }
  14049. /* Bad args. */
  14050. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14051. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14052. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14053. BAD_FUNC_ARG);
  14054. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14055. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14056. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14057. #ifdef HAVE_FIPS
  14058. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14059. #else
  14060. ExpectIntEQ(ret, 0);
  14061. #endif
  14062. wc_HmacFree(&hmac);
  14063. #endif
  14064. return EXPECT_RESULT();
  14065. } /* END test_wc_Sha224HmacSetKey() */
  14066. /*
  14067. * testing wc_HmacSetKey() on Sha256 hash
  14068. */
  14069. static int test_wc_Sha256HmacSetKey(void)
  14070. {
  14071. EXPECT_DECLS;
  14072. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14073. Hmac hmac;
  14074. int ret, times, itr;
  14075. const char* keys[]=
  14076. {
  14077. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14078. "\x0b\x0b\x0b",
  14079. #ifndef HAVE_FIPS
  14080. "Jefe", /* smaller than minimum FIPS key size */
  14081. #endif
  14082. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14083. "\xAA\xAA\xAA"
  14084. };
  14085. times = sizeof(keys) / sizeof(char*);
  14086. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14087. for (itr = 0; itr < times; itr++) {
  14088. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14089. (word32)XSTRLEN(keys[itr])), 0);
  14090. }
  14091. /* Bad args. */
  14092. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14093. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14094. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14095. BAD_FUNC_ARG);
  14096. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14097. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14098. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14099. #ifdef HAVE_FIPS
  14100. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14101. #else
  14102. ExpectIntEQ(ret, 0);
  14103. #endif
  14104. wc_HmacFree(&hmac);
  14105. #endif
  14106. return EXPECT_RESULT();
  14107. } /* END test_wc_Sha256HmacSetKey() */
  14108. /*
  14109. * testing wc_HmacSetKey on Sha384 hash.
  14110. */
  14111. static int test_wc_Sha384HmacSetKey(void)
  14112. {
  14113. EXPECT_DECLS;
  14114. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14115. Hmac hmac;
  14116. int ret, times, itr;
  14117. const char* keys[]=
  14118. {
  14119. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14120. "\x0b\x0b\x0b",
  14121. #ifndef HAVE_FIPS
  14122. "Jefe", /* smaller than minimum FIPS key size */
  14123. #endif
  14124. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14125. "\xAA\xAA\xAA"
  14126. };
  14127. times = sizeof(keys) / sizeof(char*);
  14128. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14129. for (itr = 0; itr < times; itr++) {
  14130. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14131. (word32)XSTRLEN(keys[itr])), 0);
  14132. }
  14133. /* Bad args. */
  14134. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14135. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14136. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14137. BAD_FUNC_ARG);
  14138. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14139. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14140. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14141. #ifdef HAVE_FIPS
  14142. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14143. #else
  14144. ExpectIntEQ(ret, 0);
  14145. #endif
  14146. wc_HmacFree(&hmac);
  14147. #endif
  14148. return EXPECT_RESULT();
  14149. } /* END test_wc_Sha384HmacSetKey() */
  14150. /*
  14151. * testing wc_HmacUpdate on wc_Md5 hash.
  14152. */
  14153. static int test_wc_Md5HmacUpdate(void)
  14154. {
  14155. EXPECT_DECLS;
  14156. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14157. Hmac hmac;
  14158. testVector a, b;
  14159. #ifdef HAVE_FIPS
  14160. const char* keys =
  14161. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14162. #else
  14163. const char* keys = "Jefe";
  14164. #endif
  14165. a.input = "what do ya want for nothing?";
  14166. a.inLen = XSTRLEN(a.input);
  14167. b.input = "Hi There";
  14168. b.inLen = XSTRLEN(b.input);
  14169. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14170. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14171. (word32)XSTRLEN(keys)), 0);
  14172. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14173. /* Update Hmac. */
  14174. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14175. /* Test bad args. */
  14176. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14177. BAD_FUNC_ARG);
  14178. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14179. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14180. wc_HmacFree(&hmac);
  14181. #endif
  14182. return EXPECT_RESULT();
  14183. } /* END test_wc_Md5HmacUpdate */
  14184. /*
  14185. * testing wc_HmacUpdate on SHA hash.
  14186. */
  14187. static int test_wc_ShaHmacUpdate(void)
  14188. {
  14189. EXPECT_DECLS;
  14190. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14191. Hmac hmac;
  14192. testVector a, b;
  14193. #ifdef HAVE_FIPS
  14194. const char* keys =
  14195. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14196. #else
  14197. const char* keys = "Jefe";
  14198. #endif
  14199. a.input = "what do ya want for nothing?";
  14200. a.inLen = XSTRLEN(a.input);
  14201. b.input = "Hi There";
  14202. b.inLen = XSTRLEN(b.input);
  14203. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14204. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14205. (word32)XSTRLEN(keys)), 0);
  14206. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14207. /* Update Hmac. */
  14208. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14209. /* Test bad args. */
  14210. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14211. BAD_FUNC_ARG);
  14212. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14213. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14214. wc_HmacFree(&hmac);
  14215. #endif
  14216. return EXPECT_RESULT();
  14217. } /* END test_wc_ShaHmacUpdate */
  14218. /*
  14219. * testing wc_HmacUpdate on SHA224 hash.
  14220. */
  14221. static int test_wc_Sha224HmacUpdate(void)
  14222. {
  14223. EXPECT_DECLS;
  14224. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14225. Hmac hmac;
  14226. testVector a, b;
  14227. #ifdef HAVE_FIPS
  14228. const char* keys =
  14229. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14230. #else
  14231. const char* keys = "Jefe";
  14232. #endif
  14233. a.input = "what do ya want for nothing?";
  14234. a.inLen = XSTRLEN(a.input);
  14235. b.input = "Hi There";
  14236. b.inLen = XSTRLEN(b.input);
  14237. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14238. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14239. (word32)XSTRLEN(keys)), 0);
  14240. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14241. /* Update Hmac. */
  14242. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14243. /* Test bad args. */
  14244. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14245. BAD_FUNC_ARG);
  14246. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14247. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14248. wc_HmacFree(&hmac);
  14249. #endif
  14250. return EXPECT_RESULT();
  14251. } /* END test_wc_Sha224HmacUpdate */
  14252. /*
  14253. * testing wc_HmacUpdate on SHA256 hash.
  14254. */
  14255. static int test_wc_Sha256HmacUpdate(void)
  14256. {
  14257. EXPECT_DECLS;
  14258. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14259. Hmac hmac;
  14260. testVector a, b;
  14261. #ifdef HAVE_FIPS
  14262. const char* keys =
  14263. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14264. #else
  14265. const char* keys = "Jefe";
  14266. #endif
  14267. a.input = "what do ya want for nothing?";
  14268. a.inLen = XSTRLEN(a.input);
  14269. b.input = "Hi There";
  14270. b.inLen = XSTRLEN(b.input);
  14271. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14272. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14273. (word32)XSTRLEN(keys)), 0);
  14274. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14275. /* Update Hmac. */
  14276. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14277. /* Test bad args. */
  14278. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14279. BAD_FUNC_ARG);
  14280. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14281. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14282. wc_HmacFree(&hmac);
  14283. #endif
  14284. return EXPECT_RESULT();
  14285. } /* END test_wc_Sha256HmacUpdate */
  14286. /*
  14287. * testing wc_HmacUpdate on SHA384 hash.
  14288. */
  14289. static int test_wc_Sha384HmacUpdate(void)
  14290. {
  14291. EXPECT_DECLS;
  14292. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14293. Hmac hmac;
  14294. testVector a, b;
  14295. #ifdef HAVE_FIPS
  14296. const char* keys =
  14297. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14298. #else
  14299. const char* keys = "Jefe";
  14300. #endif
  14301. a.input = "what do ya want for nothing?";
  14302. a.inLen = XSTRLEN(a.input);
  14303. b.input = "Hi There";
  14304. b.inLen = XSTRLEN(b.input);
  14305. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14306. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14307. (word32)XSTRLEN(keys)), 0);
  14308. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14309. /* Update Hmac. */
  14310. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14311. /* Test bad args. */
  14312. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14313. BAD_FUNC_ARG);
  14314. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14315. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14316. wc_HmacFree(&hmac);
  14317. #endif
  14318. return EXPECT_RESULT();
  14319. } /* END test_wc_Sha384HmacUpdate */
  14320. /*
  14321. * Testing wc_HmacFinal() with MD5
  14322. */
  14323. static int test_wc_Md5HmacFinal(void)
  14324. {
  14325. EXPECT_DECLS;
  14326. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14327. Hmac hmac;
  14328. byte hash[WC_MD5_DIGEST_SIZE];
  14329. testVector a;
  14330. const char* key;
  14331. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14332. a.input = "Hi There";
  14333. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14334. "\x9d";
  14335. a.inLen = XSTRLEN(a.input);
  14336. a.outLen = XSTRLEN(a.output);
  14337. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14338. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14339. 0);
  14340. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14341. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14342. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14343. /* Try bad parameters. */
  14344. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14345. #ifndef HAVE_FIPS
  14346. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14347. #endif
  14348. wc_HmacFree(&hmac);
  14349. #endif
  14350. return EXPECT_RESULT();
  14351. } /* END test_wc_Md5HmacFinal */
  14352. /*
  14353. * Testing wc_HmacFinal() with SHA
  14354. */
  14355. static int test_wc_ShaHmacFinal(void)
  14356. {
  14357. EXPECT_DECLS;
  14358. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14359. Hmac hmac;
  14360. byte hash[WC_SHA_DIGEST_SIZE];
  14361. testVector a;
  14362. const char* key;
  14363. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14364. "\x0b\x0b\x0b";
  14365. a.input = "Hi There";
  14366. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14367. "\x8e\xf1\x46\xbe\x00";
  14368. a.inLen = XSTRLEN(a.input);
  14369. a.outLen = XSTRLEN(a.output);
  14370. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14371. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14372. 0);
  14373. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14374. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14375. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14376. /* Try bad parameters. */
  14377. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14378. #ifndef HAVE_FIPS
  14379. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14380. #endif
  14381. wc_HmacFree(&hmac);
  14382. #endif
  14383. return EXPECT_RESULT();
  14384. } /* END test_wc_ShaHmacFinal */
  14385. /*
  14386. * Testing wc_HmacFinal() with SHA224
  14387. */
  14388. static int test_wc_Sha224HmacFinal(void)
  14389. {
  14390. EXPECT_DECLS;
  14391. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14392. Hmac hmac;
  14393. byte hash[WC_SHA224_DIGEST_SIZE];
  14394. testVector a;
  14395. const char* key;
  14396. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14397. "\x0b\x0b\x0b";
  14398. a.input = "Hi There";
  14399. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14400. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14401. a.inLen = XSTRLEN(a.input);
  14402. a.outLen = XSTRLEN(a.output);
  14403. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14404. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14405. (word32)XSTRLEN(key)), 0);
  14406. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14407. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14408. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14409. /* Try bad parameters. */
  14410. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14411. #ifndef HAVE_FIPS
  14412. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14413. #endif
  14414. wc_HmacFree(&hmac);
  14415. #endif
  14416. return EXPECT_RESULT();
  14417. } /* END test_wc_Sha224HmacFinal */
  14418. /*
  14419. * Testing wc_HmacFinal() with SHA256
  14420. */
  14421. static int test_wc_Sha256HmacFinal(void)
  14422. {
  14423. EXPECT_DECLS;
  14424. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14425. Hmac hmac;
  14426. byte hash[WC_SHA256_DIGEST_SIZE];
  14427. testVector a;
  14428. const char* key;
  14429. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14430. "\x0b\x0b\x0b";
  14431. a.input = "Hi There";
  14432. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14433. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14434. "\xcf\xf7";
  14435. a.inLen = XSTRLEN(a.input);
  14436. a.outLen = XSTRLEN(a.output);
  14437. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14438. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14439. (word32)XSTRLEN(key)), 0);
  14440. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14441. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14442. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14443. /* Try bad parameters. */
  14444. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14445. #ifndef HAVE_FIPS
  14446. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14447. #endif
  14448. wc_HmacFree(&hmac);
  14449. #endif
  14450. return EXPECT_RESULT();
  14451. } /* END test_wc_Sha256HmacFinal */
  14452. /*
  14453. * Testing wc_HmacFinal() with SHA384
  14454. */
  14455. static int test_wc_Sha384HmacFinal(void)
  14456. {
  14457. EXPECT_DECLS;
  14458. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14459. Hmac hmac;
  14460. byte hash[WC_SHA384_DIGEST_SIZE];
  14461. testVector a;
  14462. const char* key;
  14463. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14464. "\x0b\x0b\x0b";
  14465. a.input = "Hi There";
  14466. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14467. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14468. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14469. "\xfa\x9c\xb6";
  14470. a.inLen = XSTRLEN(a.input);
  14471. a.outLen = XSTRLEN(a.output);
  14472. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14473. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14474. (word32)XSTRLEN(key)), 0);
  14475. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14476. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14477. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14478. /* Try bad parameters. */
  14479. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14480. #ifndef HAVE_FIPS
  14481. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14482. #endif
  14483. wc_HmacFree(&hmac);
  14484. #endif
  14485. return EXPECT_RESULT();
  14486. } /* END test_wc_Sha384HmacFinal */
  14487. /*
  14488. * Testing wc_InitCmac()
  14489. */
  14490. static int test_wc_InitCmac(void)
  14491. {
  14492. EXPECT_DECLS;
  14493. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14494. Cmac cmac1;
  14495. Cmac cmac2;
  14496. Cmac cmac3;
  14497. /* AES 128 key. */
  14498. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14499. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14500. /* AES 192 key. */
  14501. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14502. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14503. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14504. /* AES 256 key. */
  14505. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14506. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14507. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14508. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14509. word32 key1Sz = (word32)sizeof(key1) - 1;
  14510. word32 key2Sz = (word32)sizeof(key2) - 1;
  14511. word32 key3Sz = (word32)sizeof(key3) - 1;
  14512. int type = WC_CMAC_AES;
  14513. (void)key1;
  14514. (void)key1Sz;
  14515. (void)key2;
  14516. (void)key2Sz;
  14517. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14518. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14519. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14520. #ifdef WOLFSSL_AES_128
  14521. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14522. #endif
  14523. #ifdef WOLFSSL_AES_192
  14524. wc_AesFree(&cmac1.aes);
  14525. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14526. #endif
  14527. #ifdef WOLFSSL_AES_256
  14528. wc_AesFree(&cmac2.aes);
  14529. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14530. #endif
  14531. wc_AesFree(&cmac3.aes);
  14532. /* Test bad args. */
  14533. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14534. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14535. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14536. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14537. #endif
  14538. return EXPECT_RESULT();
  14539. } /* END test_wc_InitCmac */
  14540. /*
  14541. * Testing wc_CmacUpdate()
  14542. */
  14543. static int test_wc_CmacUpdate(void)
  14544. {
  14545. EXPECT_DECLS;
  14546. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14547. Cmac cmac;
  14548. byte key[] = {
  14549. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14550. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14551. };
  14552. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14553. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14554. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14555. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14556. "\xf4";
  14557. word32 inSz = (word32)sizeof(in) - 1;
  14558. word32 keySz = (word32)sizeof(key);
  14559. int type = WC_CMAC_AES;
  14560. XMEMSET(&cmac, 0, sizeof(Cmac));
  14561. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14562. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14563. /* Test bad args. */
  14564. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14565. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14566. wc_AesFree(&cmac.aes);
  14567. #endif
  14568. return EXPECT_RESULT();
  14569. } /* END test_wc_CmacUpdate */
  14570. /*
  14571. * Testing wc_CmacFinal()
  14572. */
  14573. static int test_wc_CmacFinal(void)
  14574. {
  14575. EXPECT_DECLS;
  14576. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14577. Cmac cmac;
  14578. byte key[] = {
  14579. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14580. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14581. };
  14582. byte msg[] = {
  14583. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14584. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14585. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14586. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14587. 0xf4
  14588. };
  14589. /* Test vectors from CMACGenAES128.rsp from
  14590. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14591. * Per RFC4493 truncation of lsb is possible.
  14592. */
  14593. byte expMac[] = {
  14594. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14595. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14596. };
  14597. byte mac[AES_BLOCK_SIZE];
  14598. word32 msgSz = (word32)sizeof(msg);
  14599. word32 keySz = (word32)sizeof(key);
  14600. word32 macSz = sizeof(mac);
  14601. word32 badMacSz = 17;
  14602. int expMacSz = sizeof(expMac);
  14603. int type = WC_CMAC_AES;
  14604. XMEMSET(&cmac, 0, sizeof(Cmac));
  14605. XMEMSET(mac, 0, macSz);
  14606. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14607. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14608. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14609. /* Pass in bad args. */
  14610. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14611. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14612. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14613. /* For the last call, use the API with implicit wc_CmacFree(). */
  14614. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14615. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14616. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14617. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14618. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14619. /* Pass in bad args. */
  14620. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14621. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14622. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14623. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14624. #endif
  14625. return EXPECT_RESULT();
  14626. } /* END test_wc_CmacFinal */
  14627. /*
  14628. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14629. */
  14630. static int test_wc_AesCmacGenerate(void)
  14631. {
  14632. EXPECT_DECLS;
  14633. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14634. byte key[] = {
  14635. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14636. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14637. };
  14638. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14639. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14640. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14641. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14642. byte mac[AES_BLOCK_SIZE];
  14643. word32 keySz = sizeof(key);
  14644. word32 macSz = sizeof(mac);
  14645. word32 msgSz = sizeof(msg) - 1;
  14646. word32 expMacSz = sizeof(expMac) - 1;
  14647. XMEMSET(mac, 0, macSz);
  14648. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14649. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14650. /* Pass in bad args. */
  14651. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14652. BAD_FUNC_ARG);
  14653. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14654. BAD_FUNC_ARG);
  14655. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14656. BAD_FUNC_ARG);
  14657. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14658. BAD_FUNC_ARG);
  14659. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14660. /* Test bad args. */
  14661. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14662. BAD_FUNC_ARG);
  14663. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14664. BAD_FUNC_ARG);
  14665. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14666. BAD_FUNC_ARG);
  14667. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14668. BAD_FUNC_ARG);
  14669. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14670. BAD_FUNC_ARG);
  14671. #endif
  14672. return EXPECT_RESULT();
  14673. } /* END test_wc_AesCmacGenerate */
  14674. /*
  14675. * Testing streaming AES-GCM API.
  14676. */
  14677. static int test_wc_AesGcmStream(void)
  14678. {
  14679. EXPECT_DECLS;
  14680. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14681. defined(WOLFSSL_AESGCM_STREAM)
  14682. int i;
  14683. WC_RNG rng[1];
  14684. Aes aesEnc[1];
  14685. Aes aesDec[1];
  14686. byte tag[AES_BLOCK_SIZE];
  14687. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14688. byte out[AES_BLOCK_SIZE * 3 + 2];
  14689. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14690. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14691. byte key[AES_128_KEY_SIZE] = { 0, };
  14692. byte iv[AES_IV_SIZE] = { 1, };
  14693. byte ivOut[AES_IV_SIZE];
  14694. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14695. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14696. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14697. };
  14698. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14699. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14700. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14701. };
  14702. static const byte expTag[AES_BLOCK_SIZE] = {
  14703. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14704. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14705. };
  14706. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14707. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14708. XMEMSET(&aesDec, 0, sizeof(Aes));
  14709. /* Create a random for generating IV/nonce. */
  14710. ExpectIntEQ(wc_InitRng(rng), 0);
  14711. /* Initialize data structures. */
  14712. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14713. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14714. /* BadParameters to streaming init. */
  14715. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14716. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14717. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14718. BAD_FUNC_ARG);
  14719. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14720. BAD_FUNC_ARG);
  14721. /* Bad parameters to encrypt update. */
  14722. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14723. BAD_FUNC_ARG);
  14724. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14725. BAD_FUNC_ARG);
  14726. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14727. BAD_FUNC_ARG);
  14728. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14729. BAD_FUNC_ARG);
  14730. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14731. BAD_FUNC_ARG);
  14732. /* Bad parameters to decrypt update. */
  14733. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14734. BAD_FUNC_ARG);
  14735. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14736. BAD_FUNC_ARG);
  14737. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14738. BAD_FUNC_ARG);
  14739. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14740. BAD_FUNC_ARG);
  14741. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14742. BAD_FUNC_ARG);
  14743. /* Bad parameters to encrypt final. */
  14744. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14745. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14746. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14747. BAD_FUNC_ARG);
  14748. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14749. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14750. BAD_FUNC_ARG);
  14751. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14752. BAD_FUNC_ARG);
  14753. /* Bad parameters to decrypt final. */
  14754. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14755. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14756. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14757. BAD_FUNC_ARG);
  14758. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14759. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14760. BAD_FUNC_ARG);
  14761. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14762. BAD_FUNC_ARG);
  14763. /* Check calling final before setting key fails. */
  14764. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14765. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14766. /* Check calling update before setting key else fails. */
  14767. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14768. MISSING_KEY);
  14769. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14770. MISSING_KEY);
  14771. /* Set key but not IV. */
  14772. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14773. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14774. /* Check calling final before setting IV fails. */
  14775. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14776. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14777. /* Check calling update before setting IV else fails. */
  14778. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14779. MISSING_IV);
  14780. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14781. MISSING_IV);
  14782. /* Set IV using fixed part IV and external IV APIs. */
  14783. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14784. rng), 0);
  14785. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14786. GCM_NONCE_MID_SZ), 0);
  14787. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14788. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14789. /* Encrypt and decrypt data. */
  14790. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14791. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14792. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14793. /* Finalize and check tag matches. */
  14794. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14795. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14796. /* Set key and IV through streaming init API. */
  14797. wc_AesFree(aesEnc);
  14798. wc_AesFree(aesDec);
  14799. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14800. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14801. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14802. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14803. /* Encrypt/decrypt one block and AAD of one block. */
  14804. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14805. AES_BLOCK_SIZE), 0);
  14806. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14807. AES_BLOCK_SIZE), 0);
  14808. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14809. /* Finalize and check tag matches. */
  14810. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14811. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14812. /* Set key and IV through streaming init API. */
  14813. wc_AesFree(aesEnc);
  14814. wc_AesFree(aesDec);
  14815. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14816. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14817. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14818. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14819. /* No data to encrypt/decrypt one byte of AAD. */
  14820. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14821. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14822. /* Finalize and check tag matches. */
  14823. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14824. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14825. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14826. /* Set key and IV through streaming init API. */
  14827. wc_AesFree(aesEnc);
  14828. wc_AesFree(aesDec);
  14829. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14830. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14831. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14832. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14833. /* Encrypt/decrypt one byte and no AAD. */
  14834. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14835. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14836. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14837. /* Finalize and check tag matches. */
  14838. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14839. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14840. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14841. /* Set key and IV through streaming init API. */
  14842. wc_AesFree(aesEnc);
  14843. wc_AesFree(aesDec);
  14844. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14845. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14846. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14847. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14848. /* Encryption AES is one byte at a time */
  14849. for (i = 0; i < (int)sizeof(aad); i++) {
  14850. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14851. 0);
  14852. }
  14853. for (i = 0; i < (int)sizeof(in); i++) {
  14854. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14855. 0);
  14856. }
  14857. /* Decryption AES is two bytes at a time */
  14858. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14859. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14860. 0);
  14861. }
  14862. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14863. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14864. 0), 0);
  14865. }
  14866. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14867. /* Finalize and check tag matches. */
  14868. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14869. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14870. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14871. /* Check streaming encryption can be decrypted with one shot. */
  14872. wc_AesFree(aesDec);
  14873. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14874. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14875. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14876. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14877. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14878. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14879. wc_AesFree(aesEnc);
  14880. wc_AesFree(aesDec);
  14881. wc_FreeRng(rng);
  14882. #endif
  14883. return EXPECT_RESULT();
  14884. } /* END test_wc_AesGcmStream */
  14885. /*
  14886. * Testing streaming SM4 API.
  14887. */
  14888. static int test_wc_Sm4(void)
  14889. {
  14890. int res = TEST_SKIPPED;
  14891. #ifdef WOLFSSL_SM4
  14892. EXPECT_DECLS;
  14893. wc_Sm4 sm4;
  14894. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14895. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14896. unsigned char key[SM4_KEY_SIZE];
  14897. #endif
  14898. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14899. unsigned char iv[SM4_IV_SIZE];
  14900. #endif
  14901. /* Invalid parameters - wc_Sm4Init */
  14902. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14903. /* Valid cases - wc_Sm4Init */
  14904. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14905. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14906. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14907. XMEMSET(key, 0, sizeof(key));
  14908. /* Invalid parameters - wc_Sm4SetKey. */
  14909. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14910. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14911. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14912. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14913. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14914. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14915. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14916. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14918. /* Valid cases - wc_Sm4SetKey. */
  14919. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14920. #endif
  14921. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14922. XMEMSET(iv, 0, sizeof(iv));
  14923. /* Invalid parameters - wc_Sm4SetIV. */
  14924. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14926. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14927. /* Valid cases - wc_Sm4SetIV. */
  14928. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14929. #endif
  14930. /* Valid cases - wc_Sm4Free */
  14931. wc_Sm4Free(NULL);
  14932. wc_Sm4Free(&sm4);
  14933. res = EXPECT_RESULT();
  14934. #endif
  14935. return res;
  14936. } /* END test_wc_Sm4 */
  14937. /*
  14938. * Testing block based SM4-ECB API.
  14939. */
  14940. static int test_wc_Sm4Ecb(void)
  14941. {
  14942. int res = TEST_SKIPPED;
  14943. #ifdef WOLFSSL_SM4_ECB
  14944. EXPECT_DECLS;
  14945. wc_Sm4 sm4;
  14946. unsigned char key[SM4_KEY_SIZE];
  14947. unsigned char in[SM4_BLOCK_SIZE * 2];
  14948. unsigned char out[SM4_BLOCK_SIZE * 2];
  14949. unsigned char out2[SM4_BLOCK_SIZE];
  14950. XMEMSET(key, 0, sizeof(key));
  14951. XMEMSET(in, 0, sizeof(in));
  14952. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14953. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14954. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14955. /* Tested in test_wc_Sm4. */
  14956. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14957. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14958. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14959. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14960. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14961. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14962. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14963. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14964. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14965. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14966. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14967. /* Valid cases - wc_Sm4EcbEncrypt. */
  14968. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14969. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14970. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14971. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14972. /* In and out are same pointer. */
  14973. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14974. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14975. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14976. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14977. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14978. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14979. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14980. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14981. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14982. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14983. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14984. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14985. /* Valid cases - wc_Sm4EcbDecrypt. */
  14986. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14987. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14988. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14989. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14990. /* In and out are same pointer. */
  14991. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14992. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14993. wc_Sm4Free(&sm4);
  14994. res = EXPECT_RESULT();
  14995. #endif
  14996. return res;
  14997. } /* END test_wc_Sm4Ecb */
  14998. /*
  14999. * Testing block based SM4-CBC API.
  15000. */
  15001. static int test_wc_Sm4Cbc(void)
  15002. {
  15003. int res = TEST_SKIPPED;
  15004. #ifdef WOLFSSL_SM4_CBC
  15005. EXPECT_DECLS;
  15006. wc_Sm4 sm4;
  15007. unsigned char key[SM4_KEY_SIZE];
  15008. unsigned char iv[SM4_IV_SIZE];
  15009. unsigned char in[SM4_BLOCK_SIZE * 2];
  15010. unsigned char out[SM4_BLOCK_SIZE * 2];
  15011. unsigned char out2[SM4_BLOCK_SIZE];
  15012. XMEMSET(key, 0, sizeof(key));
  15013. XMEMSET(iv, 0, sizeof(iv));
  15014. XMEMSET(in, 0, sizeof(in));
  15015. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15016. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15017. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  15018. /* Tested in test_wc_Sm4. */
  15019. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15020. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  15021. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  15022. /* Tested in test_wc_Sm4. */
  15023. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15024. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15025. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15026. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15027. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15028. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15029. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15030. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15031. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15032. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15033. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15034. /* Valid cases - wc_Sm4CbcEncrypt. */
  15035. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15036. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15037. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15038. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15039. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15040. /* In and out are same pointer. */
  15041. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15042. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15043. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15044. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15045. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15046. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15047. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15048. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15049. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15050. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15051. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15052. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15053. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15054. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15055. /* Valid cases - wc_Sm4CbcDecrypt. */
  15056. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15057. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15058. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15059. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15060. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15061. /* In and out are same pointer. */
  15062. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15063. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15064. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15065. wc_Sm4Free(&sm4);
  15066. res = EXPECT_RESULT();
  15067. #endif
  15068. return res;
  15069. } /* END test_wc_Sm4Cbc */
  15070. /*
  15071. * Testing streaming SM4-CTR API.
  15072. */
  15073. static int test_wc_Sm4Ctr(void)
  15074. {
  15075. int res = TEST_SKIPPED;
  15076. #ifdef WOLFSSL_SM4_CTR
  15077. EXPECT_DECLS;
  15078. wc_Sm4 sm4;
  15079. unsigned char key[SM4_KEY_SIZE];
  15080. unsigned char iv[SM4_IV_SIZE];
  15081. unsigned char in[SM4_BLOCK_SIZE * 4];
  15082. unsigned char out[SM4_BLOCK_SIZE * 4];
  15083. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15084. word32 chunk;
  15085. word32 i;
  15086. XMEMSET(key, 0, sizeof(key));
  15087. XMEMSET(iv, 0, sizeof(iv));
  15088. XMEMSET(in, 0, sizeof(in));
  15089. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15090. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15091. /* Tested in test_wc_Sm4. */
  15092. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15093. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15094. /* Tested in test_wc_Sm4. */
  15095. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15096. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15097. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15098. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15099. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15100. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15101. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15102. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15103. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15104. /* Valid cases - wc_Sm4CtrEncrypt. */
  15105. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15106. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15107. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15108. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15109. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15110. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15111. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15112. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15113. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15114. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15115. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15116. /* In and out are same pointer. Also check encrypt of cipher text produces
  15117. * plaintext.
  15118. */
  15119. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15120. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15121. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15122. /* Chunking tests. */
  15123. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15124. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15125. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15126. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15127. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15128. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15129. }
  15130. if (i < (word32)sizeof(in)) {
  15131. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15132. (word32)sizeof(in) - i), 0);
  15133. }
  15134. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15135. }
  15136. for (i = 0; i < (word32)sizeof(iv); i++) {
  15137. iv[i] = 0xff;
  15138. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15139. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15140. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15141. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15142. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15143. }
  15144. wc_Sm4Free(&sm4);
  15145. res = EXPECT_RESULT();
  15146. #endif
  15147. return res;
  15148. } /* END test_wc_Sm4Ctr */
  15149. /*
  15150. * Testing stream SM4-GCM API.
  15151. */
  15152. static int test_wc_Sm4Gcm(void)
  15153. {
  15154. int res = TEST_SKIPPED;
  15155. #ifdef WOLFSSL_SM4_GCM
  15156. EXPECT_DECLS;
  15157. wc_Sm4 sm4;
  15158. unsigned char key[SM4_KEY_SIZE];
  15159. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15160. unsigned char in[SM4_BLOCK_SIZE * 2];
  15161. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15162. unsigned char out[SM4_BLOCK_SIZE * 2];
  15163. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15164. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15165. unsigned char tag[SM4_BLOCK_SIZE];
  15166. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15167. word32 i;
  15168. XMEMSET(key, 0, sizeof(key));
  15169. XMEMSET(nonce, 0, sizeof(nonce));
  15170. XMEMSET(in, 0, sizeof(in));
  15171. XMEMSET(in2, 0, sizeof(in2));
  15172. XMEMSET(aad, 0, sizeof(aad));
  15173. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15174. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15175. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15176. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15177. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15178. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15179. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15180. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15181. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15182. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15183. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15184. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15185. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15186. /* Valid parameters - wc_Sm4GcmSetKey. */
  15187. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15188. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15189. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15190. 0), BAD_FUNC_ARG);
  15191. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15192. 0), BAD_FUNC_ARG);
  15193. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15194. 0), BAD_FUNC_ARG);
  15195. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15196. 0), BAD_FUNC_ARG);
  15197. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15198. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15199. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15200. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15201. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15202. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15204. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15205. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15206. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15207. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15208. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15209. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15210. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15211. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15212. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15213. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15214. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15216. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15217. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15218. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15219. 0), BAD_FUNC_ARG);
  15220. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15221. 0), BAD_FUNC_ARG);
  15222. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15223. 0), BAD_FUNC_ARG);
  15224. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15225. 0), BAD_FUNC_ARG);
  15226. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15227. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15228. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15229. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15230. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15231. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15232. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15233. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15234. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15235. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15237. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15238. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15239. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15240. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15241. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15242. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15243. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15244. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15245. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15246. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15247. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15248. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15249. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15250. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15251. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15252. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15253. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15254. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15255. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15256. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15258. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15259. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15260. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15261. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15262. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15263. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15264. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15265. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15266. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15267. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15268. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15269. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15270. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15271. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15272. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15273. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15274. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15275. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15276. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15277. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15278. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15279. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15280. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15281. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15282. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15283. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15284. SM4_GCM_AUTH_E);
  15285. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15286. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15287. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15288. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15290. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15291. }
  15292. /* Check different in/out sizes. */
  15293. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15294. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15295. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15296. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15297. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15298. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15299. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15300. XMEMCPY(out2, out, i - 1);
  15301. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15302. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15303. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15304. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15305. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15306. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15307. }
  15308. /* Force the counter to roll over in first byte. */
  15309. {
  15310. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15311. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15312. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15313. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15314. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15315. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15316. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15317. }
  15318. wc_Sm4Free(&sm4);
  15319. res = EXPECT_RESULT();
  15320. #endif
  15321. return res;
  15322. } /* END test_wc_Sm4Gcm */
  15323. /*
  15324. * Testing stream SM4-CCM API.
  15325. */
  15326. static int test_wc_Sm4Ccm(void)
  15327. {
  15328. int res = TEST_SKIPPED;
  15329. #ifdef WOLFSSL_SM4_CCM
  15330. EXPECT_DECLS;
  15331. wc_Sm4 sm4;
  15332. unsigned char key[SM4_KEY_SIZE];
  15333. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15334. unsigned char in[SM4_BLOCK_SIZE * 2];
  15335. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15336. unsigned char out[SM4_BLOCK_SIZE * 2];
  15337. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15338. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15339. unsigned char tag[SM4_BLOCK_SIZE];
  15340. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15341. word32 i;
  15342. XMEMSET(key, 0, sizeof(key));
  15343. XMEMSET(nonce, 0, sizeof(nonce));
  15344. XMEMSET(in, 0, sizeof(in));
  15345. XMEMSET(in2, 0, sizeof(in2));
  15346. XMEMSET(aad, 0, sizeof(aad));
  15347. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15348. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15349. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15350. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15351. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15352. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15353. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15354. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15355. 0), BAD_FUNC_ARG);
  15356. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15357. 0), BAD_FUNC_ARG);
  15358. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15359. 0), BAD_FUNC_ARG);
  15360. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15361. 0), BAD_FUNC_ARG);
  15362. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15363. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15364. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15365. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15366. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15367. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15368. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15369. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15370. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15371. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15372. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15373. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15374. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15375. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15376. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15377. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15378. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15379. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15380. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15381. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15382. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15383. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15384. 0), BAD_FUNC_ARG);
  15385. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15386. 0), BAD_FUNC_ARG);
  15387. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15388. 0), BAD_FUNC_ARG);
  15389. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15390. 0), BAD_FUNC_ARG);
  15391. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15392. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15393. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15394. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15395. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15396. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15397. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15398. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15399. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15400. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15402. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15403. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15404. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15406. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15407. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15408. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15409. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15410. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15411. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15412. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15413. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15414. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15415. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15416. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15417. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15418. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15419. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15420. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15421. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15422. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15423. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15424. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15425. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15426. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15427. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15428. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15429. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15430. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15431. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15432. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15433. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15434. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15435. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15436. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15437. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15438. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15439. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15440. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15441. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15442. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15443. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15444. }
  15445. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15446. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15447. SM4_CCM_AUTH_E);
  15448. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15449. for (i = 0; i < 4; i++) {
  15450. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15451. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15452. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15453. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15454. }
  15455. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15456. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15457. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15458. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15459. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15460. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15461. }
  15462. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15463. * Even values in range 4..SM4_BLOCK_SIZE.
  15464. */
  15465. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15466. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15467. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15468. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15469. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15470. }
  15471. /* Check different in/out sizes. */
  15472. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15473. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15474. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15475. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15476. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15477. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15478. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15479. XMEMCPY(out2, out, i - 1);
  15480. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15481. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15482. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15483. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15484. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15485. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15486. }
  15487. /* Force the counter to roll over in first byte. */
  15488. {
  15489. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15490. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15491. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15492. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15493. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15494. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15495. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15496. }
  15497. wc_Sm4Free(&sm4);
  15498. res = EXPECT_RESULT();
  15499. #endif
  15500. return res;
  15501. } /* END test_wc_Sm4Ccm */
  15502. /*
  15503. * unit test for wc_Des3_SetIV()
  15504. */
  15505. static int test_wc_Des3_SetIV(void)
  15506. {
  15507. EXPECT_DECLS;
  15508. #ifndef NO_DES3
  15509. Des3 des;
  15510. const byte key[] = {
  15511. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15512. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15513. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15514. };
  15515. const byte iv[] = {
  15516. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15517. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15518. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15519. };
  15520. XMEMSET(&des, 0, sizeof(Des3));
  15521. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15522. /* DES_ENCRYPTION or DES_DECRYPTION */
  15523. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15524. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15525. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15526. /* Test explicitly wc_Des3_SetIV() */
  15527. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15528. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15529. #endif
  15530. wc_Des3Free(&des);
  15531. #endif
  15532. return EXPECT_RESULT();
  15533. } /* END test_wc_Des3_SetIV */
  15534. /*
  15535. * unit test for wc_Des3_SetKey()
  15536. */
  15537. static int test_wc_Des3_SetKey(void)
  15538. {
  15539. EXPECT_DECLS;
  15540. #ifndef NO_DES3
  15541. Des3 des;
  15542. const byte key[] = {
  15543. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15544. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15545. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15546. };
  15547. const byte iv[] = {
  15548. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15549. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15550. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15551. };
  15552. XMEMSET(&des, 0, sizeof(Des3));
  15553. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15554. /* DES_ENCRYPTION or DES_DECRYPTION */
  15555. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15556. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15557. /* Test bad args. */
  15558. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15559. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15560. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15561. /* Default case. Should return 0. */
  15562. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15563. wc_Des3Free(&des);
  15564. #endif
  15565. return EXPECT_RESULT();
  15566. } /* END test_wc_Des3_SetKey */
  15567. /*
  15568. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15569. */
  15570. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15571. {
  15572. EXPECT_DECLS;
  15573. #ifndef NO_DES3
  15574. Des3 des;
  15575. byte cipher[24];
  15576. byte plain[24];
  15577. const byte key[] = {
  15578. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15579. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15580. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15581. };
  15582. const byte iv[] = {
  15583. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15584. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15585. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15586. };
  15587. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15588. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15589. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15590. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15591. };
  15592. XMEMSET(&des, 0, sizeof(Des3));
  15593. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15594. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15595. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15596. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15597. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15598. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15599. /* Pass in bad args. */
  15600. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15601. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15602. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15603. BAD_FUNC_ARG);
  15604. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15605. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15606. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15607. wc_Des3Free(&des);
  15608. #endif
  15609. return EXPECT_RESULT();
  15610. } /* END wc_Des3_CbcEncrypt */
  15611. /*
  15612. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15613. */
  15614. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15615. {
  15616. EXPECT_DECLS;
  15617. #ifndef NO_DES3
  15618. word32 vectorSz, cipherSz;
  15619. byte cipher[24];
  15620. byte plain[24];
  15621. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15622. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15623. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15624. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15625. };
  15626. byte key[] = {
  15627. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15628. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15629. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15630. };
  15631. byte iv[] = {
  15632. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15633. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15634. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15635. };
  15636. vectorSz = sizeof(byte) * 24;
  15637. cipherSz = sizeof(byte) * 24;
  15638. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15639. 0);
  15640. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15641. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15642. /* pass in bad args. */
  15643. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15644. BAD_FUNC_ARG);
  15645. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15646. BAD_FUNC_ARG);
  15647. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15648. BAD_FUNC_ARG);
  15649. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15650. 0);
  15651. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15652. BAD_FUNC_ARG);
  15653. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15654. BAD_FUNC_ARG);
  15655. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15656. BAD_FUNC_ARG);
  15657. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15658. 0);
  15659. #endif
  15660. return EXPECT_RESULT();
  15661. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15662. /*
  15663. * Unit test for wc_Des3_EcbEncrypt
  15664. */
  15665. static int test_wc_Des3_EcbEncrypt(void)
  15666. {
  15667. EXPECT_DECLS;
  15668. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15669. Des3 des;
  15670. byte cipher[24];
  15671. word32 cipherSz = sizeof(cipher);
  15672. const byte key[] = {
  15673. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15674. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15675. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15676. };
  15677. const byte iv[] = {
  15678. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15679. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15680. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15681. };
  15682. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15683. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15684. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15685. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15686. };
  15687. XMEMSET(&des, 0, sizeof(Des3));
  15688. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15689. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15690. /* Bad Cases */
  15691. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15692. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15693. BAD_FUNC_ARG);
  15694. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15695. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15696. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15697. /* Good Cases */
  15698. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15699. wc_Des3Free(&des);
  15700. #endif
  15701. return EXPECT_RESULT();
  15702. } /* END test_wc_Des3_EcbEncrypt */
  15703. /*
  15704. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15705. */
  15706. static int test_wc_Chacha_SetKey(void)
  15707. {
  15708. EXPECT_DECLS;
  15709. #ifdef HAVE_CHACHA
  15710. ChaCha ctx;
  15711. const byte key[] = {
  15712. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15713. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15716. };
  15717. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15718. byte cipher[128];
  15719. XMEMSET(cipher, 0, sizeof(cipher));
  15720. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15721. /* Test bad args. */
  15722. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15723. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15725. /* Test bad args. */
  15726. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15727. #endif
  15728. return EXPECT_RESULT();
  15729. } /* END test_wc_Chacha_SetKey */
  15730. /*
  15731. * unit test for wc_Poly1305SetKey()
  15732. */
  15733. static int test_wc_Poly1305SetKey(void)
  15734. {
  15735. EXPECT_DECLS;
  15736. #ifdef HAVE_POLY1305
  15737. Poly1305 ctx;
  15738. const byte key[] =
  15739. {
  15740. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15744. };
  15745. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15746. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15747. /* Test bad args. */
  15748. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15749. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15750. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15751. #endif
  15752. return EXPECT_RESULT();
  15753. } /* END test_wc_Poly1305_SetKey() */
  15754. /*
  15755. * Testing wc_Chacha_Process()
  15756. */
  15757. static int test_wc_Chacha_Process(void)
  15758. {
  15759. EXPECT_DECLS;
  15760. #ifdef HAVE_CHACHA
  15761. ChaCha enc, dec;
  15762. byte cipher[128];
  15763. byte plain[128];
  15764. const byte key[] =
  15765. {
  15766. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15767. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15768. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15769. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15770. };
  15771. const char* input = "Everybody gets Friday off.";
  15772. word32 keySz = sizeof(key)/sizeof(byte);
  15773. unsigned long int inlen = XSTRLEN(input);
  15774. /* Initialize stack variables. */
  15775. XMEMSET(cipher, 0, 128);
  15776. XMEMSET(plain, 0, 128);
  15777. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15778. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15779. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15780. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15781. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15782. 0);
  15783. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15784. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15785. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15786. /* test checking and using leftovers, currently just in C code */
  15787. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15788. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15789. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15790. (word32)inlen - 2), 0);
  15791. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15792. (byte*)input + (inlen - 2), 2), 0);
  15793. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15794. (word32)inlen - 2), 0);
  15795. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15796. (byte*)input + (inlen - 2), 2), 0);
  15797. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15798. /* check edge cases with counter increment */
  15799. {
  15800. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15801. const byte expected[] = {
  15802. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15803. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15804. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15805. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15806. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15807. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15808. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15809. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15810. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15811. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15812. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15813. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15814. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15815. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15816. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15817. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15818. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15819. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15820. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15821. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15822. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15823. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15824. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15825. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15826. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15827. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15828. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15829. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15830. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15831. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15832. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15833. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15834. };
  15835. const byte iv2[] = {
  15836. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15837. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15838. };
  15839. byte input2[256];
  15840. int i;
  15841. for (i = 0; i < 256; i++)
  15842. input2[i] = i;
  15843. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15844. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15845. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15846. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15847. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15848. /* partial */
  15849. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15850. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15851. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15852. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15853. }
  15854. #endif
  15855. /* Test bad args. */
  15856. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15857. BAD_FUNC_ARG);
  15858. #endif
  15859. return EXPECT_RESULT();
  15860. } /* END test_wc_Chacha_Process */
  15861. /*
  15862. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15863. */
  15864. static int test_wc_ChaCha20Poly1305_aead(void)
  15865. {
  15866. EXPECT_DECLS;
  15867. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15868. const byte key[] = {
  15869. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15870. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15871. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15872. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15873. };
  15874. const byte plaintext[] = {
  15875. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15876. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15877. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15878. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15879. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15880. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15881. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15882. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15883. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15884. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15885. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15886. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15887. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15888. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15889. 0x74, 0x2e
  15890. };
  15891. const byte iv[] = {
  15892. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15893. 0x44, 0x45, 0x46, 0x47
  15894. };
  15895. const byte aad[] = { /* additional data */
  15896. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15897. 0xc4, 0xc5, 0xc6, 0xc7
  15898. };
  15899. const byte cipher[] = { /* expected output from operation */
  15900. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15901. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15902. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15903. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15904. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15905. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15906. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15907. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15908. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15909. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15910. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15911. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15912. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15913. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15914. 0x61, 0x16
  15915. };
  15916. const byte authTag[] = { /* expected output from operation */
  15917. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15918. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15919. };
  15920. byte generatedCiphertext[272];
  15921. byte generatedPlaintext[272];
  15922. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15923. /* Initialize stack variables. */
  15924. XMEMSET(generatedCiphertext, 0, 272);
  15925. XMEMSET(generatedPlaintext, 0, 272);
  15926. /* Test Encrypt */
  15927. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15928. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15929. 0);
  15930. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15931. sizeof(cipher)/sizeof(byte)), 0);
  15932. /* Test bad args. */
  15933. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15934. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15935. BAD_FUNC_ARG);
  15936. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15937. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15938. BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15940. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15941. BAD_FUNC_ARG);
  15942. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15943. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15944. BAD_FUNC_ARG);
  15945. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15946. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15947. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15948. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15949. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15950. sizeof(cipher), authTag, generatedPlaintext), 0);
  15951. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15952. sizeof(plaintext)/sizeof(byte)), 0);
  15953. /* Test bad args. */
  15954. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15955. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15956. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15957. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15958. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15959. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15960. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15961. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15962. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15963. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15964. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15965. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15966. #endif
  15967. return EXPECT_RESULT();
  15968. } /* END test_wc_ChaCha20Poly1305_aead */
  15969. /*
  15970. * Testing function for wc_Rc2SetKey().
  15971. */
  15972. static int test_wc_Rc2SetKey(void)
  15973. {
  15974. EXPECT_DECLS;
  15975. #ifdef WC_RC2
  15976. Rc2 rc2;
  15977. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15978. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15979. /* valid key and IV */
  15980. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15981. iv, 40), 0);
  15982. /* valid key, no IV */
  15983. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15984. NULL, 40), 0);
  15985. /* bad arguments */
  15986. /* null Rc2 struct */
  15987. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15988. iv, 40), BAD_FUNC_ARG);
  15989. /* null key */
  15990. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15991. iv, 40), BAD_FUNC_ARG);
  15992. /* key size == 0 */
  15993. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15994. /* key size > 128 */
  15995. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15996. /* effective bits == 0 */
  15997. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15998. iv, 0), WC_KEY_SIZE_E);
  15999. /* effective bits > 1024 */
  16000. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  16001. iv, 1025), WC_KEY_SIZE_E);
  16002. #endif
  16003. return EXPECT_RESULT();
  16004. } /* END test_wc_Rc2SetKey */
  16005. /*
  16006. * Testing function for wc_Rc2SetIV().
  16007. */
  16008. static int test_wc_Rc2SetIV(void)
  16009. {
  16010. EXPECT_DECLS;
  16011. #ifdef WC_RC2
  16012. Rc2 rc2;
  16013. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  16014. /* valid IV */
  16015. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16016. /* valid NULL IV */
  16017. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  16018. /* bad arguments */
  16019. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  16020. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  16021. #endif
  16022. return EXPECT_RESULT();
  16023. } /* END test_wc_Rc2SetIV */
  16024. /*
  16025. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16026. */
  16027. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16028. {
  16029. EXPECT_DECLS;
  16030. #ifdef WC_RC2
  16031. Rc2 rc2;
  16032. int effectiveKeyBits = 63;
  16033. byte cipher[RC2_BLOCK_SIZE];
  16034. byte plain[RC2_BLOCK_SIZE];
  16035. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16036. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16037. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16038. XMEMSET(cipher, 0, sizeof(cipher));
  16039. XMEMSET(plain, 0, sizeof(plain));
  16040. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16041. NULL, effectiveKeyBits), 0);
  16042. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16043. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16044. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16045. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16046. /* Rc2EcbEncrypt bad arguments */
  16047. /* null Rc2 struct */
  16048. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16049. BAD_FUNC_ARG);
  16050. /* null out buffer */
  16051. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16052. BAD_FUNC_ARG);
  16053. /* null input buffer */
  16054. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16055. BAD_FUNC_ARG);
  16056. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16057. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16058. /* Rc2EcbDecrypt bad arguments */
  16059. /* null Rc2 struct */
  16060. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16061. BAD_FUNC_ARG);
  16062. /* null out buffer */
  16063. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16064. BAD_FUNC_ARG);
  16065. /* null input buffer */
  16066. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16067. BAD_FUNC_ARG);
  16068. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16069. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16070. #endif
  16071. return EXPECT_RESULT();
  16072. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16073. /*
  16074. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16075. */
  16076. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16077. {
  16078. EXPECT_DECLS;
  16079. #ifdef WC_RC2
  16080. Rc2 rc2;
  16081. int effectiveKeyBits = 63;
  16082. byte cipher[RC2_BLOCK_SIZE*2];
  16083. byte plain[RC2_BLOCK_SIZE*2];
  16084. /* vector taken from test.c */
  16085. byte key[] = {
  16086. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16087. };
  16088. byte iv[] = {
  16089. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16090. };
  16091. byte input[] = {
  16092. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16093. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16094. };
  16095. byte output[] = {
  16096. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16097. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16098. };
  16099. XMEMSET(cipher, 0, sizeof(cipher));
  16100. XMEMSET(plain, 0, sizeof(plain));
  16101. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16102. iv, effectiveKeyBits), 0);
  16103. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16104. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16105. /* reset IV for decrypt */
  16106. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16107. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16108. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16109. /* Rc2CbcEncrypt bad arguments */
  16110. /* null Rc2 struct */
  16111. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16112. BAD_FUNC_ARG);
  16113. /* null out buffer */
  16114. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16115. BAD_FUNC_ARG);
  16116. /* null input buffer */
  16117. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16118. BAD_FUNC_ARG);
  16119. /* Rc2CbcDecrypt bad arguments */
  16120. /* in size is 0 */
  16121. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16122. /* null Rc2 struct */
  16123. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16124. BAD_FUNC_ARG);
  16125. /* null out buffer */
  16126. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16127. BAD_FUNC_ARG);
  16128. /* null input buffer */
  16129. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16130. BAD_FUNC_ARG);
  16131. #endif
  16132. return EXPECT_RESULT();
  16133. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16134. /*
  16135. * Testing function for wc_AesSetIV
  16136. */
  16137. static int test_wc_AesSetIV(void)
  16138. {
  16139. int res = TEST_SKIPPED;
  16140. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16141. Aes aes;
  16142. int ret = 0;
  16143. byte key16[] =
  16144. {
  16145. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16146. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16147. };
  16148. byte iv1[] = "1234567890abcdef";
  16149. byte iv2[] = "0987654321fedcba";
  16150. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16151. if (ret != 0)
  16152. return ret;
  16153. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16154. iv1, AES_ENCRYPTION);
  16155. if (ret == 0) {
  16156. ret = wc_AesSetIV(&aes, iv2);
  16157. }
  16158. /* Test bad args. */
  16159. if (ret == 0) {
  16160. ret = wc_AesSetIV(NULL, iv1);
  16161. if (ret == BAD_FUNC_ARG) {
  16162. /* NULL iv should return 0. */
  16163. ret = wc_AesSetIV(&aes, NULL);
  16164. }
  16165. else {
  16166. ret = WOLFSSL_FATAL_ERROR;
  16167. }
  16168. }
  16169. wc_AesFree(&aes);
  16170. res = TEST_RES_CHECK(ret == 0);
  16171. #endif
  16172. return res;
  16173. } /* test_wc_AesSetIV */
  16174. /*
  16175. * Testing function for wc_AesSetKey().
  16176. */
  16177. static int test_wc_AesSetKey(void)
  16178. {
  16179. EXPECT_DECLS;
  16180. #ifndef NO_AES
  16181. Aes aes;
  16182. byte key16[] = {
  16183. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16184. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16185. };
  16186. #ifdef WOLFSSL_AES_192
  16187. byte key24[] = {
  16188. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16189. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16190. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16191. };
  16192. #endif
  16193. #ifdef WOLFSSL_AES_256
  16194. byte key32[] = {
  16195. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16196. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16197. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16198. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16199. };
  16200. #endif
  16201. byte badKey16[] = {
  16202. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16203. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16204. };
  16205. byte iv[] = "1234567890abcdef";
  16206. XMEMSET(&aes, 0, sizeof(Aes));
  16207. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16208. #ifdef WOLFSSL_AES_128
  16209. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16210. iv, AES_ENCRYPTION), 0);
  16211. #endif
  16212. #ifdef WOLFSSL_AES_192
  16213. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16214. iv, AES_ENCRYPTION), 0);
  16215. #endif
  16216. #ifdef WOLFSSL_AES_256
  16217. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16218. iv, AES_ENCRYPTION), 0);
  16219. #endif
  16220. /* Pass in bad args. */
  16221. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16222. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16223. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16224. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16225. BAD_FUNC_ARG);
  16226. wc_AesFree(&aes);
  16227. #endif
  16228. return EXPECT_RESULT();
  16229. } /* END test_wc_AesSetKey */
  16230. /*
  16231. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16232. * and wc_AesCbcDecryptWithKey()
  16233. */
  16234. static int test_wc_AesCbcEncryptDecrypt(void)
  16235. {
  16236. EXPECT_DECLS;
  16237. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16238. defined(WOLFSSL_AES_256)
  16239. Aes aes;
  16240. byte key32[] = {
  16241. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16242. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16243. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16244. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16245. };
  16246. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16247. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16248. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16249. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16250. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16251. };
  16252. byte iv[] = "1234567890abcdef";
  16253. byte enc[sizeof(vector)];
  16254. byte dec[sizeof(vector)];
  16255. byte dec2[sizeof(vector)];
  16256. /* Init stack variables. */
  16257. XMEMSET(&aes, 0, sizeof(Aes));
  16258. XMEMSET(enc, 0, sizeof(enc));
  16259. XMEMSET(dec, 0, sizeof(vector));
  16260. XMEMSET(dec2, 0, sizeof(vector));
  16261. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16262. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16263. AES_ENCRYPTION), 0);
  16264. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16265. /* Re init for decrypt and set flag. */
  16266. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16267. AES_DECRYPTION), 0);
  16268. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16269. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16270. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16271. sizeof(key32)/sizeof(byte), iv), 0);
  16272. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16273. /* Pass in bad args */
  16274. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16275. BAD_FUNC_ARG);
  16276. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16277. BAD_FUNC_ARG);
  16278. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16279. BAD_FUNC_ARG);
  16280. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16281. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16282. BAD_LENGTH_E);
  16283. #endif
  16284. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16285. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16286. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16287. "mode (v2), skip test");
  16288. #else
  16289. /* Test passing in size of 0 */
  16290. XMEMSET(enc, 0, sizeof(enc));
  16291. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16292. /* Check enc was not modified */
  16293. {
  16294. int i;
  16295. for (i = 0; i < (int)sizeof(enc); i++)
  16296. ExpectIntEQ(enc[i], 0);
  16297. }
  16298. #endif
  16299. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16300. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16301. BAD_FUNC_ARG);
  16302. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16303. BAD_FUNC_ARG);
  16304. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16305. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16306. BAD_LENGTH_E);
  16307. #else
  16308. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16309. BAD_FUNC_ARG);
  16310. #endif
  16311. /* Test passing in size of 0 */
  16312. XMEMSET(dec, 0, sizeof(dec));
  16313. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16314. /* Check dec was not modified */
  16315. {
  16316. int i;
  16317. for (i = 0; i < (int)sizeof(dec); i++)
  16318. ExpectIntEQ(dec[i], 0);
  16319. }
  16320. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16321. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16322. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16323. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16324. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16325. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16326. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16327. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16328. wc_AesFree(&aes);
  16329. #endif
  16330. return EXPECT_RESULT();
  16331. } /* END test_wc_AesCbcEncryptDecrypt */
  16332. /*
  16333. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16334. */
  16335. static int test_wc_AesCtrEncryptDecrypt(void)
  16336. {
  16337. EXPECT_DECLS;
  16338. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16339. Aes aesEnc;
  16340. Aes aesDec;
  16341. byte key32[] = {
  16342. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16343. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16344. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16345. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16346. };
  16347. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16348. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16349. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16350. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16351. };
  16352. byte iv[] = "1234567890abcdef";
  16353. byte enc[AES_BLOCK_SIZE * 2];
  16354. byte dec[AES_BLOCK_SIZE * 2];
  16355. /* Init stack variables. */
  16356. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16357. XMEMSET(&aesDec, 0, sizeof(Aes));
  16358. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16359. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16360. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16361. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16362. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16363. AES_ENCRYPTION), 0);
  16364. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16365. sizeof(vector)/sizeof(byte)), 0);
  16366. /* Decrypt with wc_AesCtrEncrypt() */
  16367. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16368. AES_ENCRYPTION), 0);
  16369. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16370. 0);
  16371. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16372. /* Test bad args. */
  16373. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16374. BAD_FUNC_ARG);
  16375. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16376. BAD_FUNC_ARG);
  16377. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16378. BAD_FUNC_ARG);
  16379. wc_AesFree(&aesEnc);
  16380. wc_AesFree(&aesDec);
  16381. #endif
  16382. return EXPECT_RESULT();
  16383. } /* END test_wc_AesCtrEncryptDecrypt */
  16384. /*
  16385. * test function for wc_AesGcmSetKey()
  16386. */
  16387. static int test_wc_AesGcmSetKey(void)
  16388. {
  16389. EXPECT_DECLS;
  16390. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16391. Aes aes;
  16392. #ifdef WOLFSSL_AES_128
  16393. byte key16[] = {
  16394. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16395. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16396. };
  16397. #endif
  16398. #ifdef WOLFSSL_AES_192
  16399. byte key24[] = {
  16400. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16401. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16402. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16403. };
  16404. #endif
  16405. #ifdef WOLFSSL_AES_256
  16406. byte key32[] = {
  16407. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16408. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16409. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16410. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16411. };
  16412. #endif
  16413. byte badKey16[] = {
  16414. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16415. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16416. };
  16417. byte badKey24[] = {
  16418. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16419. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16420. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16421. };
  16422. byte badKey32[] = {
  16423. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16424. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16425. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16426. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16427. };
  16428. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16429. #ifdef WOLFSSL_AES_128
  16430. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16431. #endif
  16432. #ifdef WOLFSSL_AES_192
  16433. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16434. #endif
  16435. #ifdef WOLFSSL_AES_256
  16436. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16437. #endif
  16438. /* Pass in bad args. */
  16439. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16440. BAD_FUNC_ARG);
  16441. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16442. BAD_FUNC_ARG);
  16443. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16444. BAD_FUNC_ARG);
  16445. wc_AesFree(&aes);
  16446. #endif
  16447. return EXPECT_RESULT();
  16448. } /* END test_wc_AesGcmSetKey */
  16449. /*
  16450. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16451. */
  16452. static int test_wc_AesGcmEncryptDecrypt(void)
  16453. {
  16454. EXPECT_DECLS;
  16455. /* WOLFSSL_AFALG requires 12 byte IV */
  16456. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16457. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16458. Aes aes;
  16459. byte key32[] = {
  16460. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16461. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16462. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16463. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16464. };
  16465. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16466. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16467. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16468. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16469. };
  16470. const byte a[] = {
  16471. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16472. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16473. 0xab, 0xad, 0xda, 0xd2
  16474. };
  16475. byte iv[] = "1234567890a";
  16476. byte longIV[] = "1234567890abcdefghij";
  16477. byte enc[sizeof(vector)];
  16478. byte resultT[AES_BLOCK_SIZE];
  16479. byte dec[sizeof(vector)];
  16480. /* Init stack variables. */
  16481. XMEMSET(&aes, 0, sizeof(Aes));
  16482. XMEMSET(enc, 0, sizeof(vector));
  16483. XMEMSET(dec, 0, sizeof(vector));
  16484. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16485. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16486. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16487. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16488. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16489. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16490. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16491. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16492. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16493. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16494. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16495. BAD_FUNC_ARG);
  16496. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16497. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16498. BAD_FUNC_ARG);
  16499. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16500. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16501. BAD_FUNC_ARG);
  16502. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16503. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16504. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16505. /* FIPS does not check the lower bound of ivSz */
  16506. #else
  16507. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16508. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16509. #endif
  16510. /* This case is now considered good. Long IVs are now allowed.
  16511. * Except for the original FIPS release, it still has an upper
  16512. * bound on the IV length. */
  16513. #if (!defined(HAVE_FIPS) || \
  16514. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16515. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16516. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16517. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16518. 0);
  16519. #else
  16520. (void)longIV;
  16521. #endif /* Old FIPS */
  16522. /* END wc_AesGcmEncrypt */
  16523. #ifdef HAVE_AES_DECRYPT
  16524. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16525. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16526. BAD_FUNC_ARG);
  16527. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16528. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16529. BAD_FUNC_ARG);
  16530. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16531. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16532. BAD_FUNC_ARG);
  16533. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16534. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16535. BAD_FUNC_ARG);
  16536. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16537. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16538. BAD_FUNC_ARG);
  16539. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16540. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16541. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16542. AES_GCM_AUTH_E);
  16543. #else
  16544. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16545. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16546. BAD_FUNC_ARG);
  16547. #endif
  16548. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16549. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16550. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16551. /* FIPS does not check the lower bound of ivSz */
  16552. #else
  16553. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16554. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16555. #endif
  16556. #endif /* HAVE_AES_DECRYPT */
  16557. wc_AesFree(&aes);
  16558. #endif
  16559. return EXPECT_RESULT();
  16560. } /* END test_wc_AesGcmEncryptDecrypt */
  16561. /*
  16562. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16563. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16564. */
  16565. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16566. {
  16567. EXPECT_DECLS;
  16568. #if (!defined(HAVE_FIPS) || \
  16569. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16570. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16571. const byte key[] = {
  16572. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16573. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16574. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16575. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16576. };
  16577. const byte in[] = {
  16578. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16579. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16580. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16581. };
  16582. const byte aad[] = {
  16583. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16584. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16585. 0xab, 0xad, 0xda, 0xd2
  16586. };
  16587. Aes aesEnc;
  16588. Aes aesDec;
  16589. byte iv[] = "1234567890abcdefghij";
  16590. byte out[sizeof(in)];
  16591. byte plain[sizeof(in)];
  16592. byte tag[AES_BLOCK_SIZE];
  16593. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16594. XMEMSET(&aesDec, 0, sizeof(Aes));
  16595. XMEMSET(out, 0, sizeof(out));
  16596. XMEMSET(plain, 0, sizeof(plain));
  16597. XMEMSET(tag, 0, sizeof(tag));
  16598. /* Perform one-shot encryption using long IV */
  16599. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16600. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16601. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16602. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16603. /* Perform streaming decryption using long IV */
  16604. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16605. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16606. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16607. sizeof(aad)), 0);
  16608. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16609. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16610. /* Free resources */
  16611. wc_AesFree(&aesEnc);
  16612. wc_AesFree(&aesDec);
  16613. #endif
  16614. return EXPECT_RESULT();
  16615. } /* END wc_AesGcmMixedEncDecLongIV */
  16616. /*
  16617. * unit test for wc_GmacSetKey()
  16618. */
  16619. static int test_wc_GmacSetKey(void)
  16620. {
  16621. EXPECT_DECLS;
  16622. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16623. Gmac gmac;
  16624. byte key16[] = {
  16625. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16626. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16627. };
  16628. #ifdef WOLFSSL_AES_192
  16629. byte key24[] = {
  16630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16632. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16633. };
  16634. #endif
  16635. #ifdef WOLFSSL_AES_256
  16636. byte key32[] = {
  16637. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16638. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16639. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16640. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16641. };
  16642. #endif
  16643. byte badKey16[] = {
  16644. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16645. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16646. };
  16647. byte badKey24[] = {
  16648. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16649. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16650. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16651. };
  16652. byte badKey32[] = {
  16653. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16654. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16655. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16656. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16657. };
  16658. XMEMSET(&gmac, 0, sizeof(Gmac));
  16659. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16660. #ifdef WOLFSSL_AES_128
  16661. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16662. #endif
  16663. #ifdef WOLFSSL_AES_192
  16664. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16665. #endif
  16666. #ifdef WOLFSSL_AES_256
  16667. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16668. #endif
  16669. /* Pass in bad args. */
  16670. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16671. BAD_FUNC_ARG);
  16672. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16673. BAD_FUNC_ARG);
  16674. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16675. BAD_FUNC_ARG);
  16676. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16677. BAD_FUNC_ARG);
  16678. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16679. BAD_FUNC_ARG);
  16680. wc_AesFree(&gmac.aes);
  16681. #endif
  16682. return EXPECT_RESULT();
  16683. } /* END test_wc_GmacSetKey */
  16684. /*
  16685. * unit test for wc_GmacUpdate
  16686. */
  16687. static int test_wc_GmacUpdate(void)
  16688. {
  16689. EXPECT_DECLS;
  16690. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16691. Gmac gmac;
  16692. #ifdef WOLFSSL_AES_128
  16693. const byte key16[] = {
  16694. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16695. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16696. };
  16697. #endif
  16698. #ifdef WOLFSSL_AES_192
  16699. byte key24[] = {
  16700. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16701. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16702. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16703. };
  16704. #endif
  16705. #ifdef WOLFSSL_AES_256
  16706. byte key32[] = {
  16707. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16708. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16709. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16710. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16711. };
  16712. #endif
  16713. #ifdef WOLFSSL_AES_128
  16714. const byte authIn[] = {
  16715. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16716. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16717. };
  16718. #endif
  16719. #ifdef WOLFSSL_AES_192
  16720. const byte authIn2[] = {
  16721. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16722. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16723. };
  16724. #endif
  16725. const byte authIn3[] = {
  16726. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16727. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16728. };
  16729. #ifdef WOLFSSL_AES_128
  16730. const byte tag1[] = { /* Known. */
  16731. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16732. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16733. };
  16734. #endif
  16735. #ifdef WOLFSSL_AES_192
  16736. const byte tag2[] = { /* Known */
  16737. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16738. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16739. };
  16740. #endif
  16741. const byte tag3[] = { /* Known */
  16742. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16743. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16744. };
  16745. #ifdef WOLFSSL_AES_128
  16746. const byte iv[] = {
  16747. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16748. 0xe2, 0x8c, 0x8f, 0x16
  16749. };
  16750. #endif
  16751. #ifdef WOLFSSL_AES_192
  16752. const byte iv2[] = {
  16753. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16754. 0x7e, 0x1a, 0x6f, 0xbc
  16755. };
  16756. #endif
  16757. const byte iv3[] = {
  16758. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16759. 0xc3, 0xfb, 0x6c, 0x8a
  16760. };
  16761. byte tagOut[16];
  16762. byte tagOut2[24];
  16763. byte tagOut3[32];
  16764. /* Init stack variables. */
  16765. XMEMSET(&gmac, 0, sizeof(Gmac));
  16766. XMEMSET(tagOut, 0, sizeof(tagOut));
  16767. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16768. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16769. #ifdef WOLFSSL_AES_128
  16770. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16771. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16772. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16773. tagOut, sizeof(tag1)), 0);
  16774. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16775. wc_AesFree(&gmac.aes);
  16776. #endif
  16777. #ifdef WOLFSSL_AES_192
  16778. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16779. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16780. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16781. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16782. tagOut2, sizeof(tag2)), 0);
  16783. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16784. wc_AesFree(&gmac.aes);
  16785. #endif
  16786. #ifdef WOLFSSL_AES_256
  16787. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16788. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16789. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16790. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16791. tagOut3, sizeof(tag3)), 0);
  16792. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16793. wc_AesFree(&gmac.aes);
  16794. #endif
  16795. /* Pass bad args. */
  16796. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16797. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16798. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16799. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16800. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16801. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16802. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16803. wc_AesFree(&gmac.aes);
  16804. #endif
  16805. return EXPECT_RESULT();
  16806. } /* END test_wc_GmacUpdate */
  16807. /*
  16808. * testing wc_CamelliaSetKey
  16809. */
  16810. static int test_wc_CamelliaSetKey(void)
  16811. {
  16812. EXPECT_DECLS;
  16813. #ifdef HAVE_CAMELLIA
  16814. Camellia camellia;
  16815. /*128-bit key*/
  16816. static const byte key16[] = {
  16817. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16818. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16819. };
  16820. /* 192-bit key */
  16821. static const byte key24[] = {
  16822. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16823. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16824. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16825. };
  16826. /* 256-bit key */
  16827. static const byte key32[] = {
  16828. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16829. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16830. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16831. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16832. };
  16833. static const byte iv[] = {
  16834. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16835. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16836. };
  16837. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16838. 0);
  16839. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16840. NULL), 0);
  16841. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16842. 0);
  16843. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16844. NULL), 0);
  16845. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16846. 0);
  16847. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16848. NULL), 0);
  16849. /* Bad args. */
  16850. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16851. BAD_FUNC_ARG);
  16852. #endif
  16853. return EXPECT_RESULT();
  16854. } /* END test_wc_CammeliaSetKey */
  16855. /*
  16856. * Testing wc_CamelliaSetIV()
  16857. */
  16858. static int test_wc_CamelliaSetIV(void)
  16859. {
  16860. EXPECT_DECLS;
  16861. #ifdef HAVE_CAMELLIA
  16862. Camellia camellia;
  16863. static const byte iv[] = {
  16864. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16865. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16866. };
  16867. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16868. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16869. /* Bad args. */
  16870. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16871. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16872. #endif
  16873. return EXPECT_RESULT();
  16874. } /* END test_wc_CamelliaSetIV*/
  16875. /*
  16876. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16877. */
  16878. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16879. {
  16880. EXPECT_DECLS;
  16881. #ifdef HAVE_CAMELLIA
  16882. Camellia camellia;
  16883. static const byte key24[] = {
  16884. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16885. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16886. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16887. };
  16888. static const byte iv[] = {
  16889. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16890. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16891. };
  16892. static const byte plainT[] = {
  16893. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16894. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16895. };
  16896. byte enc[sizeof(plainT)];
  16897. byte dec[sizeof(enc)];
  16898. /* Init stack variables.*/
  16899. XMEMSET(enc, 0, 16);
  16900. XMEMSET(enc, 0, 16);
  16901. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16902. 0);
  16903. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16904. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16905. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16906. /* Pass bad args. */
  16907. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16908. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16909. BAD_FUNC_ARG);
  16910. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16911. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16912. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16913. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16914. #endif
  16915. return EXPECT_RESULT();
  16916. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16917. /*
  16918. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16919. */
  16920. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16921. {
  16922. EXPECT_DECLS;
  16923. #ifdef HAVE_CAMELLIA
  16924. Camellia camellia;
  16925. static const byte key24[] = {
  16926. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16927. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16928. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16929. };
  16930. static const byte plainT[] = {
  16931. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16932. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16933. };
  16934. byte enc[CAMELLIA_BLOCK_SIZE];
  16935. byte dec[CAMELLIA_BLOCK_SIZE];
  16936. /* Init stack variables. */
  16937. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16938. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16939. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16940. NULL), 0);
  16941. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16942. CAMELLIA_BLOCK_SIZE), 0);
  16943. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16944. NULL), 0);
  16945. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16946. 0);
  16947. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16948. /* Pass in bad args. */
  16949. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16950. BAD_FUNC_ARG);
  16951. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16952. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16953. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16954. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16955. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16956. BAD_FUNC_ARG);
  16957. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16958. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16959. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16960. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16961. #endif
  16962. return EXPECT_RESULT();
  16963. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16964. /*
  16965. * Testing wc_Arc4SetKey()
  16966. */
  16967. static int test_wc_Arc4SetKey(void)
  16968. {
  16969. EXPECT_DECLS;
  16970. #ifndef NO_RC4
  16971. Arc4 arc;
  16972. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16973. int keyLen = 8;
  16974. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  16975. /* Test bad args. */
  16976. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  16977. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  16978. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16979. #endif
  16980. return EXPECT_RESULT();
  16981. } /* END test_wc_Arc4SetKey */
  16982. /*
  16983. * Testing wc_Arc4Process for ENC/DEC.
  16984. */
  16985. static int test_wc_Arc4Process(void)
  16986. {
  16987. EXPECT_DECLS;
  16988. #ifndef NO_RC4
  16989. Arc4 enc;
  16990. Arc4 dec;
  16991. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16992. int keyLen = 8;
  16993. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16994. byte cipher[8];
  16995. byte plain[8];
  16996. /* Init stack variables */
  16997. XMEMSET(&enc, 0, sizeof(Arc4));
  16998. XMEMSET(&dec, 0, sizeof(Arc4));
  16999. XMEMSET(cipher, 0, sizeof(cipher));
  17000. XMEMSET(plain, 0, sizeof(plain));
  17001. /* Use for async. */
  17002. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  17003. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  17004. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  17005. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  17006. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  17007. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  17008. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  17009. /* Bad args. */
  17010. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17011. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  17012. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  17013. wc_Arc4Free(&enc);
  17014. wc_Arc4Free(&dec);
  17015. #endif
  17016. return EXPECT_RESULT();
  17017. } /* END test_wc_Arc4Process */
  17018. /*
  17019. * Testing wc_Init RsaKey()
  17020. */
  17021. static int test_wc_InitRsaKey(void)
  17022. {
  17023. EXPECT_DECLS;
  17024. #ifndef NO_RSA
  17025. RsaKey key;
  17026. XMEMSET(&key, 0, sizeof(RsaKey));
  17027. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17028. /* Test bad args. */
  17029. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17030. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17031. #endif
  17032. return EXPECT_RESULT();
  17033. } /* END test_wc_InitRsaKey */
  17034. /*
  17035. * Testing wc_RsaPrivateKeyDecode()
  17036. */
  17037. static int test_wc_RsaPrivateKeyDecode(void)
  17038. {
  17039. EXPECT_DECLS;
  17040. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17041. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17042. RsaKey key;
  17043. byte* tmp = NULL;
  17044. word32 idx = 0;
  17045. int bytes = 0;
  17046. XMEMSET(&key, 0, sizeof(RsaKey));
  17047. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17048. DYNAMIC_TYPE_TMP_BUFFER));
  17049. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17050. if (tmp != NULL) {
  17051. #ifdef USE_CERT_BUFFERS_1024
  17052. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17053. bytes = sizeof_client_key_der_1024;
  17054. #else
  17055. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17056. bytes = sizeof_client_key_der_2048;
  17057. #endif /* Use cert buffers. */
  17058. }
  17059. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17060. /* Test bad args. */
  17061. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17062. BAD_FUNC_ARG);
  17063. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17064. BAD_FUNC_ARG);
  17065. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17066. BAD_FUNC_ARG);
  17067. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17068. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17069. #endif
  17070. return EXPECT_RESULT();
  17071. } /* END test_wc_RsaPrivateKeyDecode */
  17072. /*
  17073. * Testing wc_RsaPublicKeyDecode()
  17074. */
  17075. static int test_wc_RsaPublicKeyDecode(void)
  17076. {
  17077. EXPECT_DECLS;
  17078. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17079. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17080. RsaKey keyPub;
  17081. byte* tmp = NULL;
  17082. word32 idx = 0;
  17083. int bytes = 0;
  17084. word32 keySz = 0;
  17085. word32 tstKeySz = 0;
  17086. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17087. XFILE f = XBADFILE;
  17088. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17089. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17090. byte buf[4096];
  17091. #endif
  17092. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17093. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17094. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17095. if (tmp != NULL) {
  17096. #ifdef USE_CERT_BUFFERS_1024
  17097. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17098. bytes = sizeof_client_keypub_der_1024;
  17099. keySz = 1024;
  17100. #else
  17101. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17102. bytes = sizeof_client_keypub_der_2048;
  17103. keySz = 2048;
  17104. #endif
  17105. }
  17106. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17107. /* Pass in bad args. */
  17108. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17109. BAD_FUNC_ARG);
  17110. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17111. BAD_FUNC_ARG);
  17112. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17113. BAD_FUNC_ARG);
  17114. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17115. /* Test for getting modulus key size */
  17116. idx = 0;
  17117. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17118. &tstKeySz, NULL, NULL), 0);
  17119. ExpectIntEQ(tstKeySz, keySz/8);
  17120. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17121. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17122. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17123. if (f != XBADFILE) {
  17124. XFCLOSE(f);
  17125. f = XBADFILE;
  17126. }
  17127. idx = 0;
  17128. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17129. NULL), 0);
  17130. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17131. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17132. if (f != XBADFILE)
  17133. XFCLOSE(f);
  17134. idx = 0;
  17135. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17136. NULL), 0);
  17137. #endif
  17138. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17139. #endif
  17140. return EXPECT_RESULT();
  17141. } /* END test_wc_RsaPublicKeyDecode */
  17142. /*
  17143. * Testing wc_RsaPublicKeyDecodeRaw()
  17144. */
  17145. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17146. {
  17147. EXPECT_DECLS;
  17148. #if !defined(NO_RSA)
  17149. RsaKey key;
  17150. const byte n = 0x23;
  17151. const byte e = 0x03;
  17152. int nSz = sizeof(n);
  17153. int eSz = sizeof(e);
  17154. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17155. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17156. /* Pass in bad args. */
  17157. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17158. BAD_FUNC_ARG);
  17159. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17160. BAD_FUNC_ARG);
  17161. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17162. BAD_FUNC_ARG);
  17163. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17164. #endif
  17165. return EXPECT_RESULT();
  17166. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17167. /*
  17168. * Testing wc_RsaPrivateKeyDecodeRaw()
  17169. */
  17170. static int test_wc_RsaPrivateKeyDecodeRaw(void)
  17171. {
  17172. EXPECT_DECLS;
  17173. #if !defined(NO_RSA) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) \
  17174. && !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  17175. RsaKey key;
  17176. const byte n = 33;
  17177. const byte e = 3;
  17178. const byte d = 7;
  17179. const byte u = 2;
  17180. const byte p = 3;
  17181. const byte q = 11;
  17182. const byte dp = 1;
  17183. const byte dq = 7;
  17184. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17185. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17186. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17187. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17188. NULL, 0, &key), 0);
  17189. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17190. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17191. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17192. NULL, 0, &key), 0);
  17193. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17194. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17195. &p, sizeof(p), &q, sizeof(q), NULL, 0,
  17196. &dq, sizeof(dq), &key), 0);
  17197. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17198. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17199. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17200. &dq, sizeof(dq), &key), 0);
  17201. /* Pass in bad args. */
  17202. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(NULL, sizeof(n),
  17203. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17204. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17205. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17206. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, 0,
  17207. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17208. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17209. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17210. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17211. NULL, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17212. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17213. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17214. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17215. &e, 0, &d, sizeof(d), &u, sizeof(u),
  17216. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17217. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17218. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17219. &e, sizeof(e), NULL, sizeof(d), &u, sizeof(u),
  17220. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17221. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17222. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17223. &e, sizeof(e), &d, 0, &u, sizeof(u),
  17224. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17225. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17226. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17227. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17228. NULL, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17229. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17230. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17231. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17232. &p, 0, &q, sizeof(q), &dp, sizeof(dp),
  17233. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17234. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17235. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17236. &p, sizeof(p), NULL, sizeof(q), &dp, sizeof(dp),
  17237. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17238. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17239. &e, sizeof(e), &d, sizeof(d), &u, sizeof(u),
  17240. &p, sizeof(p), &q, 0, &dp, sizeof(dp),
  17241. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17242. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  17243. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17244. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17245. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17246. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17247. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17248. &e, sizeof(e), &d, sizeof(d), NULL, sizeof(u),
  17249. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17250. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17251. ExpectIntEQ(wc_RsaPrivateKeyDecodeRaw(&n, sizeof(n),
  17252. &e, sizeof(e), &d, sizeof(d), &u, 0,
  17253. &p, sizeof(p), &q, sizeof(q), &dp, sizeof(dp),
  17254. &dq, sizeof(dq), &key), BAD_FUNC_ARG);
  17255. #endif
  17256. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17257. #endif
  17258. return EXPECT_RESULT();
  17259. } /* END test_wc_RsaPrivateKeyDecodeRaw */
  17260. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17261. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17262. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17263. * trying until it gets a probable prime. */
  17264. #ifdef HAVE_FIPS
  17265. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17266. {
  17267. int ret;
  17268. for (;;) {
  17269. ret = wc_MakeRsaKey(key, size, e, rng);
  17270. if (ret != PRIME_GEN_E) break;
  17271. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17272. "trying again.\n");
  17273. }
  17274. return ret;
  17275. }
  17276. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17277. #else
  17278. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17279. #endif
  17280. #endif
  17281. /*
  17282. * Testing wc_MakeRsaKey()
  17283. */
  17284. static int test_wc_MakeRsaKey(void)
  17285. {
  17286. EXPECT_DECLS;
  17287. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17288. RsaKey genKey;
  17289. WC_RNG rng;
  17290. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17291. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17292. int bits = 1024;
  17293. #else
  17294. int bits = 2048;
  17295. #endif
  17296. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17297. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17298. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17299. ExpectIntEQ(wc_InitRng(&rng), 0);
  17300. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17301. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17302. /* Test bad args. */
  17303. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17304. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17305. BAD_FUNC_ARG);
  17306. /* e < 3 */
  17307. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17308. /* e & 1 == 0 */
  17309. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17310. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17311. #endif
  17312. return EXPECT_RESULT();
  17313. } /* END test_wc_MakeRsaKey */
  17314. /*
  17315. * Test the bounds checking on the cipher text versus the key modulus.
  17316. * 1. Make a new RSA key.
  17317. * 2. Set c to 1.
  17318. * 3. Decrypt c into k. (error)
  17319. * 4. Copy the key modulus to c and sub 1 from the copy.
  17320. * 5. Decrypt c into k. (error)
  17321. * Valid bounds test cases are covered by all the other RSA tests.
  17322. */
  17323. static int test_RsaDecryptBoundsCheck(void)
  17324. {
  17325. EXPECT_DECLS;
  17326. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17327. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17328. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17329. WC_RNG rng;
  17330. RsaKey key;
  17331. byte flatC[256];
  17332. word32 flatCSz;
  17333. byte out[256];
  17334. word32 outSz = sizeof(out);
  17335. XMEMSET(&key, 0, sizeof(RsaKey));
  17336. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17337. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17338. ExpectIntEQ(wc_InitRng(&rng), 0);
  17339. if (EXPECT_SUCCESS()) {
  17340. const byte* derKey;
  17341. word32 derKeySz;
  17342. word32 idx = 0;
  17343. #ifdef USE_CERT_BUFFERS_1024
  17344. derKey = server_key_der_1024;
  17345. derKeySz = (word32)sizeof_server_key_der_1024;
  17346. flatCSz = 128;
  17347. #else
  17348. derKey = server_key_der_2048;
  17349. derKeySz = (word32)sizeof_server_key_der_2048;
  17350. flatCSz = 256;
  17351. #endif
  17352. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17353. }
  17354. if (EXPECT_SUCCESS()) {
  17355. XMEMSET(flatC, 0, flatCSz);
  17356. flatC[flatCSz-1] = 1;
  17357. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17358. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17359. if (EXPECT_SUCCESS()) {
  17360. mp_int c;
  17361. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17362. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17363. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17364. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17365. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17366. mp_clear(&c);
  17367. }
  17368. }
  17369. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17370. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17371. #endif
  17372. return EXPECT_RESULT();
  17373. } /* END test_wc_RsaDecryptBoundsCheck */
  17374. /*
  17375. * Testing wc_SetKeyUsage()
  17376. */
  17377. static int test_wc_SetKeyUsage(void)
  17378. {
  17379. EXPECT_DECLS;
  17380. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17381. Cert myCert;
  17382. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17383. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17384. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17385. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17386. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17387. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17388. /* Test bad args. */
  17389. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17390. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17391. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17392. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17393. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17394. KEYUSAGE_E);
  17395. #endif
  17396. return EXPECT_RESULT();
  17397. } /* END test_wc_SetKeyUsage */
  17398. /*
  17399. * Testing wc_CheckProbablePrime()
  17400. */
  17401. static int test_wc_CheckProbablePrime(void)
  17402. {
  17403. EXPECT_DECLS;
  17404. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17405. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17406. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17407. RsaKey key;
  17408. WC_RNG rng;
  17409. byte e[3];
  17410. word32 eSz = (word32)sizeof(e);
  17411. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17412. word32 nSz = (word32)sizeof(n);
  17413. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17414. word32 dSz = (word32)sizeof(d);
  17415. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17416. word32 pSz = (word32)sizeof(p);
  17417. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17418. word32 qSz = (word32)sizeof(q);
  17419. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17420. int* isPrime;
  17421. int test[5];
  17422. isPrime = test;
  17423. XMEMSET(&key, 0, sizeof(RsaKey));
  17424. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17425. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17426. ExpectIntEQ(wc_InitRng(&rng), 0);
  17427. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17428. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17429. WC_RSA_EXPONENT, &rng), 0);
  17430. PRIVATE_KEY_UNLOCK();
  17431. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17432. &qSz), 0);
  17433. PRIVATE_KEY_LOCK();
  17434. /* Bad cases */
  17435. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17436. BAD_FUNC_ARG);
  17437. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17438. BAD_FUNC_ARG);
  17439. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17440. BAD_FUNC_ARG);
  17441. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17442. BAD_FUNC_ARG);
  17443. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17444. BAD_FUNC_ARG);
  17445. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17446. BAD_FUNC_ARG);
  17447. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17448. BAD_FUNC_ARG);
  17449. /* Good case */
  17450. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17451. 0);
  17452. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17453. wc_FreeRng(&rng);
  17454. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17455. #endif
  17456. return EXPECT_RESULT();
  17457. } /* END test_wc_CheckProbablePrime */
  17458. /*
  17459. * Testing wc_RsaPSS_Verify()
  17460. */
  17461. static int test_wc_RsaPSS_Verify(void)
  17462. {
  17463. EXPECT_DECLS;
  17464. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17465. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17466. RsaKey key;
  17467. WC_RNG rng;
  17468. int sz = 256;
  17469. const char* szMessage = "This is the string to be signed";
  17470. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17471. unsigned char pDecrypted[2048/8];
  17472. byte* pt = pDecrypted;
  17473. word32 outLen = sizeof(pDecrypted);
  17474. XMEMSET(&key, 0, sizeof(RsaKey));
  17475. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17476. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17477. ExpectIntEQ(wc_InitRng(&rng), 0);
  17478. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17479. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17480. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17481. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17482. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17483. /* Bad cases */
  17484. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17485. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17486. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17487. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17488. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17489. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17490. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17491. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17492. /* Good case */
  17493. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17494. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17495. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17496. wc_FreeRng(&rng);
  17497. #endif
  17498. return EXPECT_RESULT();
  17499. } /* END test_wc_RsaPSS_Verify */
  17500. /*
  17501. * Testing wc_RsaPSS_VerifyCheck()
  17502. */
  17503. static int test_wc_RsaPSS_VerifyCheck(void)
  17504. {
  17505. EXPECT_DECLS;
  17506. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17507. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17508. RsaKey key;
  17509. WC_RNG rng;
  17510. int sz = 256; /* 2048/8 */
  17511. byte digest[32];
  17512. word32 digestSz = sizeof(digest);
  17513. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17514. word32 pSignatureSz = sizeof(pSignature);
  17515. unsigned char pDecrypted[2048/8];
  17516. byte* pt = pDecrypted;
  17517. word32 outLen = sizeof(pDecrypted);
  17518. XMEMSET(&key, 0, sizeof(RsaKey));
  17519. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17520. XMEMSET(digest, 0, sizeof(digest));
  17521. XMEMSET(pSignature, 0, sizeof(pSignature));
  17522. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17523. ExpectIntEQ(wc_InitRng(&rng), 0);
  17524. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17525. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17526. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17527. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17528. 0);
  17529. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17530. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17531. /* Bad cases */
  17532. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17533. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17534. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17535. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17536. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17537. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17538. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17539. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17540. /* Good case */
  17541. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17542. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17543. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17544. wc_FreeRng(&rng);
  17545. #endif
  17546. return EXPECT_RESULT();
  17547. } /* END test_wc_RsaPSS_VerifyCheck */
  17548. /*
  17549. * Testing wc_RsaPSS_VerifyCheckInline()
  17550. */
  17551. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17552. {
  17553. EXPECT_DECLS;
  17554. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17555. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17556. RsaKey key;
  17557. WC_RNG rng;
  17558. int sz = 256;
  17559. byte digest[32];
  17560. word32 digestSz = sizeof(digest);
  17561. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17562. unsigned char pDecrypted[2048/8];
  17563. byte* pt = pDecrypted;
  17564. XMEMSET(&key, 0, sizeof(RsaKey));
  17565. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17566. XMEMSET(digest, 0, sizeof(digest));
  17567. XMEMSET(pSignature, 0, sizeof(pSignature));
  17568. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17569. ExpectIntEQ(wc_InitRng(&rng), 0);
  17570. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17571. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17572. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17573. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17574. 0);
  17575. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17576. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17577. /* Bad Cases */
  17578. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17579. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17580. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17581. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17582. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17583. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17584. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17585. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17586. /* Good case */
  17587. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17588. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17589. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17590. wc_FreeRng(&rng);
  17591. #endif
  17592. return EXPECT_RESULT();
  17593. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17594. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17595. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17596. {
  17597. (void)flag;
  17598. (void)type;
  17599. (void)file;
  17600. (void)line;
  17601. }
  17602. #endif
  17603. /*
  17604. * Testing wc_LockMutex_ex
  17605. */
  17606. static int test_wc_LockMutex_ex(void)
  17607. {
  17608. EXPECT_DECLS;
  17609. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17610. int flag = CRYPTO_LOCK;
  17611. int type = 0;
  17612. const char* file = "./test-LockMutex_ex.txt";
  17613. int line = 0;
  17614. /* without SetMutexCb */
  17615. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17616. /* with SetMutexCb */
  17617. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17618. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17619. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17620. #endif
  17621. return EXPECT_RESULT();
  17622. } /* End test_wc_LockMutex_ex*/
  17623. /*
  17624. * Testing wc_SetMutexCb
  17625. */
  17626. static int test_wc_SetMutexCb(void)
  17627. {
  17628. EXPECT_DECLS;
  17629. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17630. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17631. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17632. #endif
  17633. return EXPECT_RESULT();
  17634. } /* End test_wc_SetMutexCb*/
  17635. /*
  17636. * Testing wc_RsaKeyToDer()
  17637. */
  17638. static int test_wc_RsaKeyToDer(void)
  17639. {
  17640. EXPECT_DECLS;
  17641. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17642. RsaKey genKey;
  17643. WC_RNG rng;
  17644. byte* der = NULL;
  17645. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17646. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17647. int bits = 1024;
  17648. word32 derSz = 611;
  17649. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17650. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17651. #else
  17652. int bits = 2048;
  17653. word32 derSz = 1196;
  17654. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17655. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17656. #endif
  17657. XMEMSET(&rng, 0, sizeof(rng));
  17658. XMEMSET(&genKey, 0, sizeof(genKey));
  17659. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17660. /* Init structures. */
  17661. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17662. ExpectIntEQ(wc_InitRng(&rng), 0);
  17663. /* Make key. */
  17664. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17665. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17666. /* Pass good/bad args. */
  17667. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17668. /* Get just the output length */
  17669. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17670. /* Try Public Key. */
  17671. genKey.type = 0;
  17672. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17673. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17674. /* Put back to Private Key */
  17675. genKey.type = 1;
  17676. #endif
  17677. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17678. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17679. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17680. #endif
  17681. return EXPECT_RESULT();
  17682. } /* END test_wc_RsaKeyToDer */
  17683. /*
  17684. * Testing wc_RsaKeyToPublicDer()
  17685. */
  17686. static int test_wc_RsaKeyToPublicDer(void)
  17687. {
  17688. EXPECT_DECLS;
  17689. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17690. RsaKey key;
  17691. WC_RNG rng;
  17692. byte* der = NULL;
  17693. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17694. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17695. int bits = 1024;
  17696. word32 derLen = 162;
  17697. #else
  17698. int bits = 2048;
  17699. word32 derLen = 294;
  17700. #endif
  17701. int ret;
  17702. XMEMSET(&rng, 0, sizeof(rng));
  17703. XMEMSET(&key, 0, sizeof(key));
  17704. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17705. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17706. ExpectIntEQ(wc_InitRng(&rng), 0);
  17707. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17708. /* test getting size only */
  17709. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17710. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17711. /* test getting size only */
  17712. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17713. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17714. /* Pass in bad args. */
  17715. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17716. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17717. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17718. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17719. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17720. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17721. #endif
  17722. return EXPECT_RESULT();
  17723. } /* END test_wc_RsaKeyToPublicDer */
  17724. /*
  17725. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17726. */
  17727. static int test_wc_RsaPublicEncryptDecrypt(void)
  17728. {
  17729. EXPECT_DECLS;
  17730. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17731. RsaKey key;
  17732. WC_RNG rng;
  17733. const char inStr[] = TEST_STRING;
  17734. const word32 plainLen = (word32)TEST_STRING_SZ;
  17735. const word32 inLen = (word32)TEST_STRING_SZ;
  17736. int bits = TEST_RSA_BITS;
  17737. const word32 cipherLen = TEST_RSA_BYTES;
  17738. word32 cipherLenResult = cipherLen;
  17739. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17740. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17741. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17742. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17743. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17744. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17745. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17746. ExpectNotNull(in);
  17747. ExpectNotNull(plain);
  17748. ExpectNotNull(cipher);
  17749. #endif
  17750. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17751. /* Initialize stack structures. */
  17752. XMEMSET(&key, 0, sizeof(RsaKey));
  17753. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17754. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17755. ExpectIntEQ(wc_InitRng(&rng), 0);
  17756. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17757. /* Encrypt. */
  17758. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  17759. cipherLen, &key, &rng), 0);
  17760. /* Pass bad args - tested in another testing function.*/
  17761. /* Decrypt */
  17762. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17763. /* Bind rng */
  17764. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17765. #endif
  17766. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17767. &key), 0);
  17768. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17769. /* Pass bad args - tested in another testing function.*/
  17770. WC_FREE_VAR(in, NULL);
  17771. WC_FREE_VAR(plain, NULL);
  17772. WC_FREE_VAR(cipher, NULL);
  17773. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17774. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17775. #endif
  17776. return EXPECT_RESULT();
  17777. } /* END test_wc_RsaPublicEncryptDecrypt */
  17778. /*
  17779. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17780. */
  17781. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17782. {
  17783. EXPECT_DECLS;
  17784. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17785. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17786. RsaKey key;
  17787. WC_RNG rng;
  17788. const char inStr[] = TEST_STRING;
  17789. const word32 inLen = (word32)TEST_STRING_SZ;
  17790. const word32 plainSz = (word32)TEST_STRING_SZ;
  17791. byte* res = NULL;
  17792. int idx = 0;
  17793. int bits = TEST_RSA_BITS;
  17794. const word32 cipherSz = TEST_RSA_BYTES;
  17795. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17796. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17797. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17798. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17799. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17800. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17801. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17802. ExpectNotNull(in);
  17803. ExpectNotNull(plain);
  17804. ExpectNotNull(cipher);
  17805. #endif
  17806. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17807. /* Initialize stack structures. */
  17808. XMEMSET(&key, 0, sizeof(RsaKey));
  17809. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17810. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17811. ExpectIntEQ(wc_InitRng(&rng), 0);
  17812. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17813. /* Encrypt */
  17814. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17815. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17816. /* Pass bad args - tested in another testing function.*/
  17817. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17818. /* Decrypt */
  17819. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17820. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17821. #endif
  17822. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17823. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17824. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17825. /* Pass bad args - tested in another testing function.*/
  17826. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17827. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17828. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17829. #endif
  17830. WC_FREE_VAR(in, NULL);
  17831. WC_FREE_VAR(plain, NULL);
  17832. WC_FREE_VAR(cipher, NULL);
  17833. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17834. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17835. #endif
  17836. return EXPECT_RESULT();
  17837. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17838. /*
  17839. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17840. */
  17841. static int test_wc_RsaSSL_SignVerify(void)
  17842. {
  17843. EXPECT_DECLS;
  17844. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17845. RsaKey key;
  17846. WC_RNG rng;
  17847. const char inStr[] = TEST_STRING;
  17848. const word32 plainSz = (word32)TEST_STRING_SZ;
  17849. const word32 inLen = (word32)TEST_STRING_SZ;
  17850. word32 idx = 0;
  17851. int bits = TEST_RSA_BITS;
  17852. const word32 outSz = TEST_RSA_BYTES;
  17853. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17854. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17855. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17856. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17857. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17858. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17859. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17860. ExpectNotNull(in);
  17861. ExpectNotNull(out);
  17862. ExpectNotNull(plain);
  17863. #endif
  17864. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17865. XMEMSET(&key, 0, sizeof(RsaKey));
  17866. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17867. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17868. ExpectIntEQ(wc_InitRng(&rng), 0);
  17869. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17870. /* Sign. */
  17871. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17872. idx = (int)outSz;
  17873. /* Test bad args. */
  17874. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17875. BAD_FUNC_ARG);
  17876. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17877. BAD_FUNC_ARG);
  17878. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17879. BAD_FUNC_ARG);
  17880. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17881. BAD_FUNC_ARG);
  17882. /* Verify. */
  17883. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17884. /* Pass bad args. */
  17885. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17886. BAD_FUNC_ARG);
  17887. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17888. BAD_FUNC_ARG);
  17889. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17890. BAD_FUNC_ARG);
  17891. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17892. BAD_FUNC_ARG);
  17893. WC_FREE_VAR(in, NULL);
  17894. WC_FREE_VAR(out, NULL);
  17895. WC_FREE_VAR(plain, NULL);
  17896. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17897. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17898. #endif
  17899. return EXPECT_RESULT();
  17900. } /* END test_wc_RsaSSL_SignVerify */
  17901. /*
  17902. * Testing wc_RsaEncryptSize()
  17903. */
  17904. static int test_wc_RsaEncryptSize(void)
  17905. {
  17906. EXPECT_DECLS;
  17907. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17908. RsaKey key;
  17909. WC_RNG rng;
  17910. XMEMSET(&key, 0, sizeof(RsaKey));
  17911. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17912. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17913. ExpectIntEQ(wc_InitRng(&rng), 0);
  17914. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17915. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17916. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17917. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17918. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17919. #endif
  17920. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17921. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17922. /* Pass in bad arg. */
  17923. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17924. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17925. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17926. #endif
  17927. return EXPECT_RESULT();
  17928. } /* END test_wc_RsaEncryptSize*/
  17929. /*
  17930. * Testing wc_RsaFlattenPublicKey()
  17931. */
  17932. static int test_wc_RsaFlattenPublicKey(void)
  17933. {
  17934. EXPECT_DECLS;
  17935. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17936. RsaKey key;
  17937. WC_RNG rng;
  17938. byte e[256];
  17939. byte n[256];
  17940. word32 eSz = sizeof(e);
  17941. word32 nSz = sizeof(n);
  17942. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17943. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17944. int bits = 1024;
  17945. #else
  17946. int bits = 2048;
  17947. #endif
  17948. XMEMSET(&key, 0, sizeof(RsaKey));
  17949. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17950. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17951. ExpectIntEQ(wc_InitRng(&rng), 0);
  17952. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17953. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17954. /* Pass bad args. */
  17955. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17956. BAD_FUNC_ARG);
  17957. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17958. BAD_FUNC_ARG);
  17959. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17960. BAD_FUNC_ARG);
  17961. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17962. BAD_FUNC_ARG);
  17963. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17964. BAD_FUNC_ARG);
  17965. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17966. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17967. #endif
  17968. return EXPECT_RESULT();
  17969. } /* END test_wc_RsaFlattenPublicKey */
  17970. /*
  17971. * unit test for wc_AesCcmSetKey
  17972. */
  17973. static int test_wc_AesCcmSetKey(void)
  17974. {
  17975. EXPECT_DECLS;
  17976. #ifdef HAVE_AESCCM
  17977. Aes aes;
  17978. const byte key16[] = {
  17979. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17980. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17981. };
  17982. const byte key24[] = {
  17983. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17984. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17985. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17986. };
  17987. const byte key32[] = {
  17988. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17989. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17990. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17991. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17992. };
  17993. XMEMSET(&aes, 0, sizeof(Aes));
  17994. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17995. #ifdef WOLFSSL_AES_128
  17996. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17997. #endif
  17998. #ifdef WOLFSSL_AES_192
  17999. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  18000. #endif
  18001. #ifdef WOLFSSL_AES_256
  18002. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  18003. #endif
  18004. /* Test bad args. */
  18005. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  18006. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  18007. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  18008. wc_AesFree(&aes);
  18009. #endif
  18010. return EXPECT_RESULT();
  18011. } /* END test_wc_AesCcmSetKey */
  18012. /*
  18013. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  18014. */
  18015. static int test_wc_AesCcmEncryptDecrypt(void)
  18016. {
  18017. EXPECT_DECLS;
  18018. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  18019. Aes aes;
  18020. const byte key16[] = {
  18021. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  18022. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  18023. };
  18024. /* plaintext */
  18025. const byte plainT[] = {
  18026. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  18027. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  18028. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  18029. };
  18030. /* nonce */
  18031. const byte iv[] = {
  18032. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  18033. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  18034. };
  18035. const byte c[] = { /* cipher text. */
  18036. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  18037. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  18038. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  18039. };
  18040. const byte t[] = { /* Auth tag */
  18041. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  18042. };
  18043. const byte authIn[] = {
  18044. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  18045. };
  18046. byte cipherOut[sizeof(plainT)];
  18047. byte authTag[sizeof(t)];
  18048. #ifdef HAVE_AES_DECRYPT
  18049. byte plainOut[sizeof(cipherOut)];
  18050. #endif
  18051. XMEMSET(&aes, 0, sizeof(Aes));
  18052. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  18053. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  18054. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18055. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  18056. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  18057. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  18058. #ifdef HAVE_AES_DECRYPT
  18059. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18060. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  18061. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  18062. #endif
  18063. /* Pass in bad args. Encrypt*/
  18064. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  18065. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18066. BAD_FUNC_ARG);
  18067. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  18068. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18069. BAD_FUNC_ARG);
  18070. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  18071. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18072. BAD_FUNC_ARG);
  18073. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18074. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18075. BAD_FUNC_ARG);
  18076. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18077. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  18078. BAD_FUNC_ARG);
  18079. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18080. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18081. BAD_FUNC_ARG);
  18082. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  18083. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  18084. BAD_FUNC_ARG);
  18085. #ifdef HAVE_AES_DECRYPT
  18086. /* Pass in bad args. Decrypt*/
  18087. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  18088. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18089. BAD_FUNC_ARG);
  18090. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  18091. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18092. BAD_FUNC_ARG);
  18093. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  18094. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18095. BAD_FUNC_ARG);
  18096. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18097. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18098. BAD_FUNC_ARG);
  18099. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18100. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  18101. BAD_FUNC_ARG);
  18102. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18103. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18104. BAD_FUNC_ARG);
  18105. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  18106. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  18107. BAD_FUNC_ARG);
  18108. #endif
  18109. wc_AesFree(&aes);
  18110. #endif /* HAVE_AESCCM */
  18111. return EXPECT_RESULT();
  18112. } /* END test_wc_AesCcmEncryptDecrypt */
  18113. #if defined(WOLFSSL_AES_EAX) && \
  18114. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  18115. /*
  18116. * Testing test_wc_AesEaxVectors()
  18117. */
  18118. static int test_wc_AesEaxVectors(void)
  18119. {
  18120. EXPECT_DECLS;
  18121. typedef struct {
  18122. byte key[AES_256_KEY_SIZE];
  18123. int key_length;
  18124. byte iv[AES_BLOCK_SIZE];
  18125. int iv_length;
  18126. byte aad[AES_BLOCK_SIZE * 2];
  18127. int aad_length;
  18128. byte msg[AES_BLOCK_SIZE * 5];
  18129. int msg_length;
  18130. byte ct[AES_BLOCK_SIZE * 5];
  18131. int ct_length;
  18132. byte tag[AES_BLOCK_SIZE];
  18133. int tag_length;
  18134. int valid;
  18135. } AadVector;
  18136. /* Test vectors obtained from Google wycheproof project
  18137. * https://github.com/google/wycheproof
  18138. * from testvectors/aes_eax_test.json
  18139. */
  18140. const AadVector vectors[] = {
  18141. {
  18142. /* key, key length */
  18143. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18144. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18145. /* iv, iv length */
  18146. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18147. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18148. /* aad, aad length */
  18149. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18150. /* msg, msg length */
  18151. {0x00}, 0,
  18152. /* ct, ct length */
  18153. {0x00}, 0,
  18154. /* tag, tag length */
  18155. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18156. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18157. /* valid */
  18158. 1,
  18159. },
  18160. {
  18161. /* key, key length */
  18162. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18163. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18164. /* iv, iv length */
  18165. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18166. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18167. /* aad, aad length */
  18168. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18169. /* msg, msg length */
  18170. {0xf7, 0xfb}, 2,
  18171. /* ct, ct length */
  18172. {0x19, 0xdd}, 2,
  18173. /* tag, tag length */
  18174. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18175. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18176. /* valid */
  18177. 1,
  18178. },
  18179. {
  18180. /* key, key length */
  18181. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18182. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18183. /* iv, iv length */
  18184. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18185. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18186. /* aad, aad length */
  18187. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18188. /* msg, msg length */
  18189. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18190. /* ct, ct length */
  18191. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18192. /* tag, tag length */
  18193. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18194. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18195. /* valid */
  18196. 1,
  18197. },
  18198. {
  18199. /* key, key length */
  18200. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18201. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18202. /* iv, iv length */
  18203. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18204. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18205. /* aad, aad length */
  18206. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18207. /* msg, msg length */
  18208. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18209. /* ct, ct length */
  18210. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18211. /* tag, tag length */
  18212. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18213. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18214. /* valid */
  18215. 1,
  18216. },
  18217. {
  18218. /* key, key length */
  18219. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18220. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18221. /* iv, iv length */
  18222. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18223. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18224. /* aad, aad length */
  18225. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18226. /* msg, msg length */
  18227. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18228. /* ct, ct length */
  18229. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18230. /* tag, tag length */
  18231. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18232. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18233. /* valid */
  18234. 1,
  18235. },
  18236. {
  18237. /* key, key length */
  18238. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18239. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18240. /* iv, iv length */
  18241. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18242. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18243. /* aad, aad length */
  18244. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18245. /* msg, msg length */
  18246. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18247. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18248. /* ct, ct length */
  18249. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18250. 0x0e, 0x72, 0x84, 0x14}, 12,
  18251. /* tag, tag length */
  18252. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18253. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18254. /* valid */
  18255. 1,
  18256. },
  18257. {
  18258. /* key, key length */
  18259. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18260. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18261. /* iv, iv length */
  18262. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18263. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18264. /* aad, aad length */
  18265. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18266. /* msg, msg length */
  18267. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18268. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18269. 0x36}, 17,
  18270. /* ct, ct length */
  18271. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18272. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18273. 0x30}, 17,
  18274. /* tag, tag length */
  18275. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18276. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18277. /* valid */
  18278. 1,
  18279. },
  18280. {
  18281. /* key, key length */
  18282. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18283. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18284. /* iv, iv length */
  18285. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18286. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18287. /* aad, aad length */
  18288. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18289. /* msg, msg length */
  18290. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18291. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18292. 0x2d, 0x56}, 18,
  18293. /* ct, ct length */
  18294. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18295. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18296. 0x8c, 0xc3}, 18,
  18297. /* tag, tag length */
  18298. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18299. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18300. /* valid */
  18301. 1,
  18302. },
  18303. {
  18304. /* key, key length */
  18305. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18306. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18307. /* iv, iv length */
  18308. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18309. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18310. /* aad, aad length */
  18311. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18312. /* msg, msg length */
  18313. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18314. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18315. 0xef, 0x11}, 18,
  18316. /* ct, ct length */
  18317. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18318. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18319. 0xb1, 0xe8}, 18,
  18320. /* tag, tag length */
  18321. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18322. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18323. /* valid */
  18324. 1,
  18325. },
  18326. {
  18327. /* key, key length */
  18328. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18329. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18330. /* iv, iv length */
  18331. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18332. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18333. /* aad, aad length */
  18334. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18335. /* msg, msg length */
  18336. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18337. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18338. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18339. /* ct, ct length */
  18340. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18341. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18342. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18343. /* tag, tag length */
  18344. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18345. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18346. /* valid */
  18347. 1,
  18348. },
  18349. {
  18350. /* key, key length */
  18351. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18352. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18353. /* iv, iv length */
  18354. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18355. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18356. /* aad, aad length */
  18357. {0x00}, 0,
  18358. /* msg, msg length */
  18359. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18360. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18361. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18362. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18363. /* ct, ct length */
  18364. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18365. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18366. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18367. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18368. /* tag, tag length */
  18369. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18370. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18371. /* valid */
  18372. 1,
  18373. },
  18374. {
  18375. /* key, key length */
  18376. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18377. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18378. /* iv, iv length */
  18379. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18380. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18381. /* aad, aad length */
  18382. {0x00}, 0,
  18383. /* msg, msg length */
  18384. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18385. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18386. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18387. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18388. /* ct, ct length */
  18389. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18390. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18391. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18392. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18393. /* tag, tag length */
  18394. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18395. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18396. /* valid */
  18397. 1,
  18398. },
  18399. {
  18400. /* key, key length */
  18401. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18402. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18403. /* iv, iv length */
  18404. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18405. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18406. /* aad, aad length */
  18407. {0x00}, 0,
  18408. /* msg, msg length */
  18409. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18411. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18412. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18413. /* ct, ct length */
  18414. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18415. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18416. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18417. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18418. /* tag, tag length */
  18419. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18420. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18421. /* valid */
  18422. 1,
  18423. },
  18424. {
  18425. /* key, key length */
  18426. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18427. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18428. /* iv, iv length */
  18429. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18430. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18431. /* aad, aad length */
  18432. {0x00}, 0,
  18433. /* msg, msg length */
  18434. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18435. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18436. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18437. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18438. /* ct, ct length */
  18439. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18440. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18441. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18442. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18443. /* tag, tag length */
  18444. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18445. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18446. /* valid */
  18447. 1,
  18448. },
  18449. {
  18450. /* key, key length */
  18451. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18452. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18453. /* iv, iv length */
  18454. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18455. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18456. /* aad, aad length */
  18457. {0x00}, 0,
  18458. /* msg, msg length */
  18459. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18460. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18461. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18462. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18463. /* ct, ct length */
  18464. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18465. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18466. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18467. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18468. /* tag, tag length */
  18469. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18470. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18471. /* valid */
  18472. 1,
  18473. },
  18474. {
  18475. /* key, key length */
  18476. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18477. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18478. /* iv, iv length */
  18479. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18480. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18481. /* aad, aad length */
  18482. {0x00}, 0,
  18483. /* msg, msg length */
  18484. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18485. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18486. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18487. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18488. /* ct, ct length */
  18489. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18490. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18491. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18492. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18493. /* tag, tag length */
  18494. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18495. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18496. /* valid */
  18497. 1,
  18498. },
  18499. {
  18500. /* key, key length */
  18501. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18502. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18503. /* iv, iv length */
  18504. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18505. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18506. /* aad, aad length */
  18507. {0x00}, 0,
  18508. /* msg, msg length */
  18509. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18510. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18511. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18512. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18513. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18514. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18515. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18516. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18517. /* ct, ct length */
  18518. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18519. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18520. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18521. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18522. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18523. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18524. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18525. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18526. /* tag, tag length */
  18527. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18528. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18529. /* valid */
  18530. 1,
  18531. },
  18532. {
  18533. /* key, key length */
  18534. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18535. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18536. /* iv, iv length */
  18537. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18538. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18539. /* aad, aad length */
  18540. {0x00}, 0,
  18541. /* msg, msg length */
  18542. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18543. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18544. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18545. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18546. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18547. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18548. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18549. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18550. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18551. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18552. /* ct, ct length */
  18553. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18554. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18555. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18556. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18557. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18558. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18559. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18560. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18561. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18562. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18563. /* tag, tag length */
  18564. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18565. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18566. /* valid */
  18567. 1,
  18568. },
  18569. {
  18570. /* key, key length */
  18571. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18572. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18573. /* iv, iv length */
  18574. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18575. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18576. /* aad, aad length */
  18577. {0x00}, 0,
  18578. /* msg, msg length */
  18579. {0x00}, 0,
  18580. /* ct, ct length */
  18581. {0x00}, 0,
  18582. /* tag, tag length */
  18583. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18584. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18585. /* valid */
  18586. 1,
  18587. },
  18588. {
  18589. /* key, key length */
  18590. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18591. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18592. /* iv, iv length */
  18593. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18594. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18595. /* aad, aad length */
  18596. {0x00}, 0,
  18597. /* msg, msg length */
  18598. {0x01}, 1,
  18599. /* ct, ct length */
  18600. {0x1d}, 1,
  18601. /* tag, tag length */
  18602. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18603. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18604. /* valid */
  18605. 1,
  18606. },
  18607. {
  18608. /* key, key length */
  18609. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18610. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18611. /* iv, iv length */
  18612. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18613. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18614. /* aad, aad length */
  18615. {0x00}, 0,
  18616. /* msg, msg length */
  18617. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18618. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18619. /* ct, ct length */
  18620. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18621. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18622. /* tag, tag length */
  18623. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18624. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18625. /* valid */
  18626. 1,
  18627. },
  18628. {
  18629. /* key, key length */
  18630. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18631. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18632. /* iv, iv length */
  18633. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18634. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18635. /* aad, aad length */
  18636. {0x00}, 0,
  18637. /* msg, msg length */
  18638. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18639. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18640. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18641. /* ct, ct length */
  18642. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18643. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18644. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18645. /* tag, tag length */
  18646. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18647. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18648. /* valid */
  18649. 1,
  18650. },
  18651. {
  18652. /* key, key length */
  18653. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18654. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18655. /* iv, iv length */
  18656. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18657. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18658. /* aad, aad length */
  18659. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18660. /* msg, msg length */
  18661. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18662. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18663. /* ct, ct length */
  18664. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18665. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18666. /* tag, tag length */
  18667. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18668. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18669. /* valid */
  18670. 1,
  18671. },
  18672. {
  18673. /* key, key length */
  18674. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18675. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18676. /* iv, iv length */
  18677. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18678. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18679. /* aad, aad length */
  18680. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18681. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18682. /* msg, msg length */
  18683. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18684. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18685. /* ct, ct length */
  18686. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18687. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18688. /* tag, tag length */
  18689. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18690. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18691. /* valid */
  18692. 1,
  18693. },
  18694. {
  18695. /* key, key length */
  18696. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18697. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18698. /* iv, iv length */
  18699. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18700. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18701. /* aad, aad length */
  18702. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18703. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18704. /* msg, msg length */
  18705. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18706. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18707. /* ct, ct length */
  18708. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18709. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18710. /* tag, tag length */
  18711. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18712. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18713. /* valid */
  18714. 1,
  18715. },
  18716. {
  18717. /* key, key length */
  18718. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18719. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18720. /* iv, iv length */
  18721. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18722. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18723. /* aad, aad length */
  18724. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18725. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18726. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18727. /* msg, msg length */
  18728. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18729. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18730. /* ct, ct length */
  18731. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18732. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18733. /* tag, tag length */
  18734. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18735. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18736. /* valid */
  18737. 1,
  18738. },
  18739. {
  18740. /* key, key length */
  18741. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18742. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18743. /* iv, iv length */
  18744. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18745. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18746. /* aad, aad length */
  18747. {0x00}, 0,
  18748. /* msg, msg length */
  18749. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18750. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18751. /* ct, ct length */
  18752. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18753. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18754. /* tag, tag length */
  18755. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18756. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18757. /* valid */
  18758. 0,
  18759. },
  18760. {
  18761. /* key, key length */
  18762. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18763. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18764. /* iv, iv length */
  18765. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18766. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18767. /* aad, aad length */
  18768. {0x00}, 0,
  18769. /* msg, msg length */
  18770. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18771. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18772. /* ct, ct length */
  18773. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18774. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18775. /* tag, tag length */
  18776. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18777. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18778. /* valid */
  18779. 0,
  18780. },
  18781. {
  18782. /* key, key length */
  18783. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18784. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18785. /* iv, iv length */
  18786. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18787. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18788. /* aad, aad length */
  18789. {0x00}, 0,
  18790. /* msg, msg length */
  18791. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18792. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18793. /* ct, ct length */
  18794. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18795. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18796. /* tag, tag length */
  18797. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18798. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18799. /* valid */
  18800. 0,
  18801. },
  18802. {
  18803. /* key, key length */
  18804. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18805. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18806. /* iv, iv length */
  18807. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18808. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18809. /* aad, aad length */
  18810. {0x00}, 0,
  18811. /* msg, msg length */
  18812. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18813. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18814. /* ct, ct length */
  18815. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18816. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18817. /* tag, tag length */
  18818. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18819. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18820. /* valid */
  18821. 0,
  18822. },
  18823. {
  18824. /* key, key length */
  18825. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18826. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18827. /* iv, iv length */
  18828. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18829. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18830. /* aad, aad length */
  18831. {0x00}, 0,
  18832. /* msg, msg length */
  18833. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18834. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18835. /* ct, ct length */
  18836. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18837. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18838. /* tag, tag length */
  18839. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18840. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18841. /* valid */
  18842. 0,
  18843. },
  18844. {
  18845. /* key, key length */
  18846. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18847. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18848. /* iv, iv length */
  18849. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18850. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18851. /* aad, aad length */
  18852. {0x00}, 0,
  18853. /* msg, msg length */
  18854. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18855. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18856. /* ct, ct length */
  18857. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18858. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18859. /* tag, tag length */
  18860. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18861. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18862. /* valid */
  18863. 0,
  18864. },
  18865. {
  18866. /* key, key length */
  18867. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18868. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18869. /* iv, iv length */
  18870. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18871. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18872. /* aad, aad length */
  18873. {0x00}, 0,
  18874. /* msg, msg length */
  18875. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18876. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18877. /* ct, ct length */
  18878. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18879. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18880. /* tag, tag length */
  18881. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18882. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18883. /* valid */
  18884. 0,
  18885. },
  18886. {
  18887. /* key, key length */
  18888. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18889. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18890. /* iv, iv length */
  18891. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18892. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18893. /* aad, aad length */
  18894. {0x00}, 0,
  18895. /* msg, msg length */
  18896. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18897. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18898. /* ct, ct length */
  18899. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18900. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18901. /* tag, tag length */
  18902. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18903. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18904. /* valid */
  18905. 0,
  18906. },
  18907. {
  18908. /* key, key length */
  18909. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18910. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18911. /* iv, iv length */
  18912. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18913. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18914. /* aad, aad length */
  18915. {0x00}, 0,
  18916. /* msg, msg length */
  18917. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18918. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18919. /* ct, ct length */
  18920. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18921. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18922. /* tag, tag length */
  18923. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18924. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18925. /* valid */
  18926. 0,
  18927. },
  18928. {
  18929. /* key, key length */
  18930. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18931. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18932. /* iv, iv length */
  18933. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18934. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18935. /* aad, aad length */
  18936. {0x00}, 0,
  18937. /* msg, msg length */
  18938. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18939. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18940. /* ct, ct length */
  18941. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18942. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18943. /* tag, tag length */
  18944. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18945. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18946. /* valid */
  18947. 0,
  18948. },
  18949. {
  18950. /* key, key length */
  18951. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18952. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18953. /* iv, iv length */
  18954. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18955. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18956. /* aad, aad length */
  18957. {0x00}, 0,
  18958. /* msg, msg length */
  18959. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18960. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18961. /* ct, ct length */
  18962. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18963. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18964. /* tag, tag length */
  18965. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18966. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18967. /* valid */
  18968. 0,
  18969. },
  18970. {
  18971. /* key, key length */
  18972. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18973. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18974. /* iv, iv length */
  18975. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18976. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18977. /* aad, aad length */
  18978. {0x00}, 0,
  18979. /* msg, msg length */
  18980. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18981. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18982. /* ct, ct length */
  18983. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18984. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18985. /* tag, tag length */
  18986. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18987. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18988. /* valid */
  18989. 0,
  18990. },
  18991. {
  18992. /* key, key length */
  18993. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18994. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18995. /* iv, iv length */
  18996. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18997. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18998. /* aad, aad length */
  18999. {0x00}, 0,
  19000. /* msg, msg length */
  19001. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19002. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19003. /* ct, ct length */
  19004. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19005. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19006. /* tag, tag length */
  19007. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19008. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  19009. /* valid */
  19010. 0,
  19011. },
  19012. {
  19013. /* key, key length */
  19014. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19015. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19016. /* iv, iv length */
  19017. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19018. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19019. /* aad, aad length */
  19020. {0x00}, 0,
  19021. /* msg, msg length */
  19022. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19023. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19024. /* ct, ct length */
  19025. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19026. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19027. /* tag, tag length */
  19028. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19029. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  19030. /* valid */
  19031. 0,
  19032. },
  19033. {
  19034. /* key, key length */
  19035. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19036. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19037. /* iv, iv length */
  19038. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19039. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19040. /* aad, aad length */
  19041. {0x00}, 0,
  19042. /* msg, msg length */
  19043. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19044. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19045. /* ct, ct length */
  19046. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19047. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19048. /* tag, tag length */
  19049. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19050. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  19051. /* valid */
  19052. 0,
  19053. },
  19054. {
  19055. /* key, key length */
  19056. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19057. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19058. /* iv, iv length */
  19059. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19060. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19061. /* aad, aad length */
  19062. {0x00}, 0,
  19063. /* msg, msg length */
  19064. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19065. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19066. /* ct, ct length */
  19067. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19068. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19069. /* tag, tag length */
  19070. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19071. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  19072. /* valid */
  19073. 0,
  19074. },
  19075. {
  19076. /* key, key length */
  19077. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19078. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19079. /* iv, iv length */
  19080. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19081. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19082. /* aad, aad length */
  19083. {0x00}, 0,
  19084. /* msg, msg length */
  19085. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19086. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19087. /* ct, ct length */
  19088. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19089. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19090. /* tag, tag length */
  19091. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19092. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  19093. /* valid */
  19094. 0,
  19095. },
  19096. {
  19097. /* key, key length */
  19098. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19099. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19100. /* iv, iv length */
  19101. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19102. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19103. /* aad, aad length */
  19104. {0x00}, 0,
  19105. /* msg, msg length */
  19106. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19107. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19108. /* ct, ct length */
  19109. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19110. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19111. /* tag, tag length */
  19112. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19113. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  19114. /* valid */
  19115. 0,
  19116. },
  19117. {
  19118. /* key, key length */
  19119. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19120. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19121. /* iv, iv length */
  19122. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19123. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19124. /* aad, aad length */
  19125. {0x00}, 0,
  19126. /* msg, msg length */
  19127. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19128. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19129. /* ct, ct length */
  19130. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19131. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19132. /* tag, tag length */
  19133. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19134. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19135. /* valid */
  19136. 0,
  19137. },
  19138. {
  19139. /* key, key length */
  19140. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19141. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19142. /* iv, iv length */
  19143. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19144. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19145. /* aad, aad length */
  19146. {0x00}, 0,
  19147. /* msg, msg length */
  19148. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19149. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19150. /* ct, ct length */
  19151. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19152. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19153. /* tag, tag length */
  19154. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19155. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19156. /* valid */
  19157. 0,
  19158. },
  19159. {
  19160. /* key, key length */
  19161. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19162. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19163. /* iv, iv length */
  19164. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19165. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19166. /* aad, aad length */
  19167. {0x00}, 0,
  19168. /* msg, msg length */
  19169. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19170. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19171. /* ct, ct length */
  19172. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19173. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19174. /* tag, tag length */
  19175. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19176. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19177. /* valid */
  19178. 0,
  19179. },
  19180. {
  19181. /* key, key length */
  19182. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19183. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19184. /* iv, iv length */
  19185. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19186. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19187. /* aad, aad length */
  19188. {0x00}, 0,
  19189. /* msg, msg length */
  19190. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19191. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19192. /* ct, ct length */
  19193. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19194. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19195. /* tag, tag length */
  19196. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19197. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19198. /* valid */
  19199. 0,
  19200. },
  19201. {
  19202. /* key, key length */
  19203. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19204. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19205. /* iv, iv length */
  19206. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19207. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19208. /* aad, aad length */
  19209. {0x00}, 0,
  19210. /* msg, msg length */
  19211. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19212. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19213. /* ct, ct length */
  19214. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19215. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19216. /* tag, tag length */
  19217. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19218. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19219. /* valid */
  19220. 0,
  19221. },
  19222. {
  19223. /* key, key length */
  19224. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19225. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19226. /* iv, iv length */
  19227. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19228. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19229. /* aad, aad length */
  19230. {0x00}, 0,
  19231. /* msg, msg length */
  19232. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19233. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19234. /* ct, ct length */
  19235. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19236. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19237. /* tag, tag length */
  19238. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19239. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19240. /* valid */
  19241. 0,
  19242. },
  19243. {
  19244. /* key, key length */
  19245. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19246. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19247. /* iv, iv length */
  19248. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19249. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19250. /* aad, aad length */
  19251. {0x00}, 0,
  19252. /* msg, msg length */
  19253. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19254. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19255. /* ct, ct length */
  19256. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19257. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19258. /* tag, tag length */
  19259. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19260. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19261. /* valid */
  19262. 0,
  19263. },
  19264. {
  19265. /* key, key length */
  19266. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19267. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19268. /* iv, iv length */
  19269. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19270. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19271. /* aad, aad length */
  19272. {0x00}, 0,
  19273. /* msg, msg length */
  19274. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19275. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19276. /* ct, ct length */
  19277. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19278. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19279. /* tag, tag length */
  19280. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19281. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19282. /* valid */
  19283. 0,
  19284. },
  19285. {
  19286. /* key, key length */
  19287. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19288. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19289. /* iv, iv length */
  19290. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19291. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19292. /* aad, aad length */
  19293. {0x00}, 0,
  19294. /* msg, msg length */
  19295. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19296. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19297. /* ct, ct length */
  19298. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19299. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19300. /* tag, tag length */
  19301. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19302. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19303. /* valid */
  19304. 0,
  19305. },
  19306. };
  19307. byte ciphertext[sizeof(vectors[0].ct)];
  19308. byte authtag[sizeof(vectors[0].tag)];
  19309. int i;
  19310. int len;
  19311. int ret;
  19312. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19313. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19314. len = sizeof(authtag);
  19315. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19316. ciphertext,
  19317. vectors[i].msg, vectors[i].msg_length,
  19318. vectors[i].iv, vectors[i].iv_length,
  19319. authtag, len,
  19320. vectors[i].aad, vectors[i].aad_length),
  19321. 0);
  19322. /* check ciphertext matches vector */
  19323. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19324. 0);
  19325. /* check that computed tag matches vector only for vectors marked as valid */
  19326. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19327. if (vectors[i].valid) {
  19328. ExpectIntEQ(ret, 0);
  19329. }
  19330. else {
  19331. ExpectIntNE(ret, 0);
  19332. }
  19333. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19334. /* Decrypt, checking that the computed auth tags match */
  19335. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19336. ciphertext,
  19337. vectors[i].ct, vectors[i].ct_length,
  19338. vectors[i].iv, vectors[i].iv_length,
  19339. authtag, len,
  19340. vectors[i].aad, vectors[i].aad_length),
  19341. 0);
  19342. /* check decrypted ciphertext matches vector plaintext */
  19343. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19344. 0);
  19345. }
  19346. return EXPECT_RESULT();
  19347. } /* END test_wc_AesEaxVectors */
  19348. /*
  19349. * Testing test_wc_AesEaxEncryptAuth()
  19350. */
  19351. static int test_wc_AesEaxEncryptAuth(void)
  19352. {
  19353. EXPECT_DECLS;
  19354. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19355. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19356. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19357. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19358. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19359. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19360. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19361. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19362. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19363. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19364. byte ciphertext[sizeof(msg)];
  19365. byte authtag[AES_BLOCK_SIZE];
  19366. int i;
  19367. int len;
  19368. len = sizeof(authtag);
  19369. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19370. ciphertext,
  19371. msg, sizeof(msg),
  19372. iv, sizeof(iv),
  19373. authtag, (word32)len,
  19374. aad, sizeof(aad)),
  19375. 0);
  19376. /* Test null checking */
  19377. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19378. ciphertext,
  19379. msg, sizeof(msg),
  19380. iv, sizeof(iv),
  19381. authtag, (word32)len,
  19382. aad, sizeof(aad)),
  19383. BAD_FUNC_ARG);
  19384. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19385. NULL,
  19386. msg, sizeof(msg),
  19387. iv, sizeof(iv),
  19388. authtag, (word32)len,
  19389. aad, sizeof(aad)),
  19390. BAD_FUNC_ARG);
  19391. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19392. ciphertext,
  19393. NULL, sizeof(msg),
  19394. iv, sizeof(iv),
  19395. authtag, (word32)len,
  19396. aad, sizeof(aad)),
  19397. BAD_FUNC_ARG);
  19398. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19399. ciphertext,
  19400. msg, sizeof(msg),
  19401. NULL, sizeof(iv),
  19402. authtag, (word32)len,
  19403. aad, sizeof(aad)),
  19404. BAD_FUNC_ARG);
  19405. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19406. ciphertext,
  19407. msg, sizeof(msg),
  19408. iv, sizeof(iv),
  19409. NULL, (word32)len,
  19410. aad, sizeof(aad)),
  19411. BAD_FUNC_ARG);
  19412. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19413. ciphertext,
  19414. msg, sizeof(msg),
  19415. iv, sizeof(iv),
  19416. authtag, (word32)len,
  19417. NULL, sizeof(aad)),
  19418. BAD_FUNC_ARG);
  19419. /* Test bad key lengths */
  19420. for (i = 0; i <= 32; i++) {
  19421. int exp_ret;
  19422. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19423. || i == AES_256_KEY_SIZE) {
  19424. exp_ret = 0;
  19425. }
  19426. else {
  19427. exp_ret = BAD_FUNC_ARG;
  19428. }
  19429. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19430. ciphertext,
  19431. msg, sizeof(msg),
  19432. iv, sizeof(iv),
  19433. authtag, (word32)len,
  19434. aad, sizeof(aad)),
  19435. exp_ret);
  19436. }
  19437. /* Test auth tag size out of range */
  19438. len = AES_BLOCK_SIZE + 1;
  19439. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19440. ciphertext,
  19441. msg, sizeof(msg),
  19442. iv, sizeof(iv),
  19443. authtag, (word32)len,
  19444. aad, sizeof(aad)),
  19445. BAD_FUNC_ARG);
  19446. return EXPECT_RESULT();
  19447. } /* END test_wc_AesEaxEncryptAuth() */
  19448. /*
  19449. * Testing test_wc_AesEaxDecryptAuth()
  19450. */
  19451. static int test_wc_AesEaxDecryptAuth(void)
  19452. {
  19453. EXPECT_DECLS;
  19454. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19455. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19456. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19457. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19458. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19459. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19460. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19461. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19462. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19463. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19464. /* Garbage tag that should always fail for above aad */
  19465. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19466. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19467. byte plaintext[sizeof(ct)];
  19468. int i;
  19469. int len;
  19470. len = sizeof(tag);
  19471. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19472. plaintext,
  19473. ct, sizeof(ct),
  19474. iv, sizeof(iv),
  19475. tag, (word32)len,
  19476. aad, sizeof(aad)),
  19477. AES_EAX_AUTH_E);
  19478. /* Test null checking */
  19479. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19480. plaintext,
  19481. ct, sizeof(ct),
  19482. iv, sizeof(iv),
  19483. tag, (word32)len,
  19484. aad, sizeof(aad)),
  19485. BAD_FUNC_ARG);
  19486. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19487. NULL,
  19488. ct, sizeof(ct),
  19489. iv, sizeof(iv),
  19490. tag, (word32)len,
  19491. aad, sizeof(aad)),
  19492. BAD_FUNC_ARG);
  19493. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19494. plaintext,
  19495. NULL, sizeof(ct),
  19496. iv, sizeof(iv),
  19497. tag, (word32)len,
  19498. aad, sizeof(aad)),
  19499. BAD_FUNC_ARG);
  19500. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19501. plaintext,
  19502. ct, sizeof(ct),
  19503. NULL, sizeof(iv),
  19504. tag, (word32)len,
  19505. aad, sizeof(aad)),
  19506. BAD_FUNC_ARG);
  19507. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19508. plaintext,
  19509. ct, sizeof(ct),
  19510. iv, sizeof(iv),
  19511. NULL, (word32)len,
  19512. aad, sizeof(aad)),
  19513. BAD_FUNC_ARG);
  19514. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19515. plaintext,
  19516. ct, sizeof(ct),
  19517. iv, sizeof(iv),
  19518. tag, (word32)len,
  19519. NULL, sizeof(aad)),
  19520. BAD_FUNC_ARG);
  19521. /* Test bad key lengths */
  19522. for (i = 0; i <= 32; i++) {
  19523. int exp_ret;
  19524. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19525. || i == AES_256_KEY_SIZE) {
  19526. exp_ret = AES_EAX_AUTH_E;
  19527. }
  19528. else {
  19529. exp_ret = BAD_FUNC_ARG;
  19530. }
  19531. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19532. plaintext,
  19533. ct, sizeof(ct),
  19534. iv, sizeof(iv),
  19535. tag, (word32)len,
  19536. aad, sizeof(aad)),
  19537. exp_ret);
  19538. }
  19539. /* Test auth tag size out of range */
  19540. len = AES_BLOCK_SIZE + 1;
  19541. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19542. plaintext,
  19543. ct, sizeof(ct),
  19544. iv, sizeof(iv),
  19545. tag, (word32)len,
  19546. aad, sizeof(aad)),
  19547. BAD_FUNC_ARG);
  19548. return EXPECT_RESULT();
  19549. } /* END test_wc_AesEaxDecryptAuth() */
  19550. #endif /* WOLFSSL_AES_EAX &&
  19551. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19552. */
  19553. /*
  19554. * Testing wc_InitDsaKey()
  19555. */
  19556. static int test_wc_InitDsaKey(void)
  19557. {
  19558. EXPECT_DECLS;
  19559. #ifndef NO_DSA
  19560. DsaKey key;
  19561. XMEMSET(&key, 0, sizeof(DsaKey));
  19562. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19563. /* Pass in bad args. */
  19564. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19565. wc_FreeDsaKey(&key);
  19566. #endif
  19567. return EXPECT_RESULT();
  19568. } /* END test_wc_InitDsaKey */
  19569. /*
  19570. * Testing wc_DsaSign() and wc_DsaVerify()
  19571. */
  19572. static int test_wc_DsaSignVerify(void)
  19573. {
  19574. EXPECT_DECLS;
  19575. #if !defined(NO_DSA)
  19576. DsaKey key;
  19577. WC_RNG rng;
  19578. wc_Sha sha;
  19579. byte signature[DSA_SIG_SIZE];
  19580. byte hash[WC_SHA_DIGEST_SIZE];
  19581. word32 idx = 0;
  19582. word32 bytes;
  19583. int answer;
  19584. #ifdef USE_CERT_BUFFERS_1024
  19585. byte tmp[ONEK_BUF];
  19586. XMEMSET(tmp, 0, sizeof(tmp));
  19587. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19588. bytes = sizeof_dsa_key_der_1024;
  19589. #elif defined(USE_CERT_BUFFERS_2048)
  19590. byte tmp[TWOK_BUF];
  19591. XMEMSET(tmp, 0, sizeof(tmp));
  19592. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19593. bytes = sizeof_dsa_key_der_2048;
  19594. #else
  19595. byte tmp[TWOK_BUF];
  19596. XFILE fp = XBADFILE;
  19597. XMEMSET(tmp, 0, sizeof(tmp));
  19598. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19599. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19600. if (fp != XBADFILE)
  19601. XFCLOSE(fp);
  19602. #endif /* END USE_CERT_BUFFERS_1024 */
  19603. ExpectIntEQ(wc_InitSha(&sha), 0);
  19604. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19605. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19606. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19607. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19608. ExpectIntEQ(wc_InitRng(&rng), 0);
  19609. /* Sign. */
  19610. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19611. /* Test bad args. */
  19612. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19613. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19614. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19615. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19616. /* Verify. */
  19617. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19618. ExpectIntEQ(answer, 1);
  19619. /* Pass in bad args. */
  19620. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19621. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19622. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19623. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19624. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19625. /* hard set q to 0 and test fail case */
  19626. mp_free(&key.q);
  19627. mp_init(&key.q);
  19628. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19629. mp_set(&key.q, 1);
  19630. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19631. #endif
  19632. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19633. wc_FreeDsaKey(&key);
  19634. wc_ShaFree(&sha);
  19635. #endif
  19636. return EXPECT_RESULT();
  19637. } /* END test_wc_DsaSign */
  19638. /*
  19639. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19640. */
  19641. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19642. {
  19643. EXPECT_DECLS;
  19644. #if !defined(NO_DSA)
  19645. DsaKey key;
  19646. word32 bytes = 0;
  19647. word32 idx = 0;
  19648. int ret = 0;
  19649. #ifdef USE_CERT_BUFFERS_1024
  19650. byte tmp[ONEK_BUF];
  19651. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19652. bytes = sizeof_dsa_key_der_1024;
  19653. #elif defined(USE_CERT_BUFFERS_2048)
  19654. byte tmp[TWOK_BUF];
  19655. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19656. bytes = sizeof_dsa_key_der_2048;
  19657. #else
  19658. byte tmp[TWOK_BUF];
  19659. XFILE fp = XBADFILE;
  19660. XMEMSET(tmp, 0, sizeof(tmp));
  19661. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19662. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19663. if (fp != XBADFILE)
  19664. XFCLOSE(fp);
  19665. #endif /* END USE_CERT_BUFFERS_1024 */
  19666. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19667. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19668. /* Test bad args. */
  19669. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19670. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19671. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19672. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19673. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19674. wc_FreeDsaKey(&key);
  19675. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19676. idx = 0; /* Reset */
  19677. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19678. /* Test bad args. */
  19679. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19680. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19681. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19682. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19683. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19684. wc_FreeDsaKey(&key);
  19685. #endif /* !NO_DSA */
  19686. return EXPECT_RESULT();
  19687. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19688. /*
  19689. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19690. */
  19691. static int test_wc_MakeDsaKey(void)
  19692. {
  19693. EXPECT_DECLS;
  19694. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19695. DsaKey genKey;
  19696. WC_RNG rng;
  19697. XMEMSET(&genKey, 0, sizeof(genKey));
  19698. XMEMSET(&rng, 0, sizeof(rng));
  19699. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19700. ExpectIntEQ(wc_InitRng(&rng), 0);
  19701. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19702. /* Test bad args. */
  19703. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19704. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19705. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19706. BAD_FUNC_ARG);
  19707. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19708. /* Test bad args. */
  19709. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19710. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19711. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19712. wc_FreeDsaKey(&genKey);
  19713. #endif
  19714. return EXPECT_RESULT();
  19715. } /* END test_wc_MakeDsaKey */
  19716. /*
  19717. * Testing wc_DsaKeyToDer()
  19718. */
  19719. static int test_wc_DsaKeyToDer(void)
  19720. {
  19721. EXPECT_DECLS;
  19722. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19723. DsaKey key;
  19724. word32 bytes;
  19725. word32 idx = 0;
  19726. #ifdef USE_CERT_BUFFERS_1024
  19727. byte tmp[ONEK_BUF];
  19728. byte der[ONEK_BUF];
  19729. XMEMSET(tmp, 0, sizeof(tmp));
  19730. XMEMSET(der, 0, sizeof(der));
  19731. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19732. bytes = sizeof_dsa_key_der_1024;
  19733. #elif defined(USE_CERT_BUFFERS_2048)
  19734. byte tmp[TWOK_BUF];
  19735. byte der[TWOK_BUF];
  19736. XMEMSET(tmp, 0, sizeof(tmp));
  19737. XMEMSET(der, 0, sizeof(der));
  19738. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19739. bytes = sizeof_dsa_key_der_2048;
  19740. #else
  19741. byte tmp[TWOK_BUF];
  19742. byte der[TWOK_BUF];
  19743. XFILE fp = XBADFILE;
  19744. XMEMSET(tmp, 0, sizeof(tmp));
  19745. XMEMSET(der, 0, sizeof(der));
  19746. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19747. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19748. if (fp != XBADFILE)
  19749. XFCLOSE(fp);
  19750. #endif /* END USE_CERT_BUFFERS_1024 */
  19751. XMEMSET(&key, 0, sizeof(DsaKey));
  19752. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19753. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19754. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19755. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19756. /* Test bad args. */
  19757. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19758. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19759. wc_FreeDsaKey(&key);
  19760. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19761. return EXPECT_RESULT();
  19762. } /* END test_wc_DsaKeyToDer */
  19763. /*
  19764. * Testing wc_DsaKeyToPublicDer()
  19765. * (indirectly testing setDsaPublicKey())
  19766. */
  19767. static int test_wc_DsaKeyToPublicDer(void)
  19768. {
  19769. EXPECT_DECLS;
  19770. #ifndef HAVE_SELFTEST
  19771. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19772. DsaKey key;
  19773. WC_RNG rng;
  19774. byte* der = NULL;
  19775. word32 sz = 0;
  19776. word32 idx = 0;
  19777. XMEMSET(&key, 0, sizeof(DsaKey));
  19778. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19779. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19780. DYNAMIC_TYPE_TMP_BUFFER));
  19781. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19782. ExpectIntEQ(wc_InitRng(&rng), 0);
  19783. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19784. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19785. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19786. wc_FreeDsaKey(&key);
  19787. idx = 0;
  19788. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19789. /* Test without the SubjectPublicKeyInfo header */
  19790. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19791. wc_FreeDsaKey(&key);
  19792. idx = 0;
  19793. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19794. /* Test bad args. */
  19795. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19796. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19797. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19798. wc_FreeDsaKey(&key);
  19799. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19800. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19801. #endif /* !HAVE_SELFTEST */
  19802. return EXPECT_RESULT();
  19803. } /* END test_wc_DsaKeyToPublicDer */
  19804. /*
  19805. * Testing wc_DsaImportParamsRaw()
  19806. */
  19807. static int test_wc_DsaImportParamsRaw(void)
  19808. {
  19809. EXPECT_DECLS;
  19810. #if !defined(NO_DSA)
  19811. DsaKey key;
  19812. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19813. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19814. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19815. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19816. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19817. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19818. "80a0093113a8bd73";
  19819. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19820. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19821. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19822. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19823. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19824. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19825. "76341a7e7d9";
  19826. /* invalid p and q parameters */
  19827. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19828. const char* invalidQ = "96c5390a";
  19829. XMEMSET(&key, 0, sizeof(DsaKey));
  19830. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19831. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19832. /* test bad args */
  19833. /* null key struct */
  19834. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19835. /* null param pointers */
  19836. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19837. /* illegal p length */
  19838. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19839. /* illegal q length */
  19840. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19841. wc_FreeDsaKey(&key);
  19842. #endif
  19843. return EXPECT_RESULT();
  19844. } /* END test_wc_DsaImportParamsRaw */
  19845. /*
  19846. * Testing wc_DsaImportParamsRawCheck()
  19847. */
  19848. static int test_wc_DsaImportParamsRawCheck(void)
  19849. {
  19850. EXPECT_DECLS;
  19851. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19852. DsaKey key;
  19853. int trusted = 0;
  19854. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19855. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19856. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19857. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19858. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19859. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19860. "80a0093113a8bd73";
  19861. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19862. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19863. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19864. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19865. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19866. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19867. "76341a7e7d9";
  19868. /* invalid p and q parameters */
  19869. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19870. const char* invalidQ = "96c5390a";
  19871. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19872. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19873. /* test bad args */
  19874. /* null key struct */
  19875. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19876. BAD_FUNC_ARG);
  19877. /* null param pointers */
  19878. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19879. NULL), BAD_FUNC_ARG);
  19880. /* illegal p length */
  19881. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19882. BAD_FUNC_ARG);
  19883. /* illegal q length */
  19884. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19885. BAD_FUNC_ARG);
  19886. wc_FreeDsaKey(&key);
  19887. #endif
  19888. return EXPECT_RESULT();
  19889. } /* END test_wc_DsaImportParamsRawCheck */
  19890. /*
  19891. * Testing wc_DsaExportParamsRaw()
  19892. */
  19893. static int test_wc_DsaExportParamsRaw(void)
  19894. {
  19895. EXPECT_DECLS;
  19896. #if !defined(NO_DSA)
  19897. DsaKey key;
  19898. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19899. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19900. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19901. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19902. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19903. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19904. "80a0093113a8bd73";
  19905. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19906. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19907. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19908. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19909. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19910. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19911. "76341a7e7d9";
  19912. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19913. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19914. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19915. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19916. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19917. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19918. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19919. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19920. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19921. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19922. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19923. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19924. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19925. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19926. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19927. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19928. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19929. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19930. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19931. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19932. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19933. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19934. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19935. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19936. byte pOut[MAX_DSA_PARAM_SIZE];
  19937. byte qOut[MAX_DSA_PARAM_SIZE];
  19938. byte gOut[MAX_DSA_PARAM_SIZE];
  19939. word32 pOutSz;
  19940. word32 qOutSz;
  19941. word32 gOutSz;
  19942. XMEMSET(&key, 0, sizeof(DsaKey));
  19943. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19944. /* first test using imported raw parameters, for expected */
  19945. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19946. pOutSz = sizeof(pOut);
  19947. qOutSz = sizeof(qOut);
  19948. gOutSz = sizeof(gOut);
  19949. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19950. &gOutSz), 0);
  19951. /* validate exported parameters are correct */
  19952. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19953. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19954. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19955. /* test bad args */
  19956. /* null key struct */
  19957. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19958. &gOutSz), BAD_FUNC_ARG);
  19959. /* null output pointers */
  19960. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19961. &gOutSz), LENGTH_ONLY_E);
  19962. /* null output size pointers */
  19963. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19964. NULL), BAD_FUNC_ARG);
  19965. /* p output buffer size too small */
  19966. pOutSz = 1;
  19967. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19968. &gOutSz), BUFFER_E);
  19969. pOutSz = sizeof(pOut);
  19970. /* q output buffer size too small */
  19971. qOutSz = 1;
  19972. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19973. &gOutSz), BUFFER_E);
  19974. qOutSz = sizeof(qOut);
  19975. /* g output buffer size too small */
  19976. gOutSz = 1;
  19977. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19978. &gOutSz), BUFFER_E);
  19979. wc_FreeDsaKey(&key);
  19980. #endif
  19981. return EXPECT_RESULT();
  19982. } /* END test_wc_DsaExportParamsRaw */
  19983. /*
  19984. * Testing wc_DsaExportKeyRaw()
  19985. */
  19986. static int test_wc_DsaExportKeyRaw(void)
  19987. {
  19988. EXPECT_DECLS;
  19989. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19990. DsaKey key;
  19991. WC_RNG rng;
  19992. byte xOut[MAX_DSA_PARAM_SIZE];
  19993. byte yOut[MAX_DSA_PARAM_SIZE];
  19994. word32 xOutSz, yOutSz;
  19995. XMEMSET(&key, 0, sizeof(key));
  19996. XMEMSET(&rng, 0, sizeof(rng));
  19997. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19998. ExpectIntEQ(wc_InitRng(&rng), 0);
  19999. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  20000. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  20001. /* try successful export */
  20002. xOutSz = sizeof(xOut);
  20003. yOutSz = sizeof(yOut);
  20004. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  20005. /* test bad args */
  20006. /* null key struct */
  20007. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  20008. BAD_FUNC_ARG);
  20009. /* null output pointers */
  20010. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  20011. LENGTH_ONLY_E);
  20012. /* null output size pointers */
  20013. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  20014. BAD_FUNC_ARG);
  20015. /* x output buffer size too small */
  20016. xOutSz = 1;
  20017. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20018. BUFFER_E);
  20019. xOutSz = sizeof(xOut);
  20020. /* y output buffer size too small */
  20021. yOutSz = 1;
  20022. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  20023. BUFFER_E);
  20024. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20025. wc_FreeDsaKey(&key);
  20026. #endif
  20027. return EXPECT_RESULT();
  20028. } /* END test_wc_DsaExportParamsRaw */
  20029. /*
  20030. * Testing wc_ed25519_make_key().
  20031. */
  20032. static int test_wc_ed25519_make_key(void)
  20033. {
  20034. EXPECT_DECLS;
  20035. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  20036. ed25519_key key;
  20037. WC_RNG rng;
  20038. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  20039. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  20040. XMEMSET(&key, 0, sizeof(ed25519_key));
  20041. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20042. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20043. ExpectIntEQ(wc_InitRng(&rng), 0);
  20044. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  20045. ECC_PRIV_KEY_E);
  20046. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  20047. ECC_PRIV_KEY_E);
  20048. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20049. /* Test bad args. */
  20050. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  20051. BAD_FUNC_ARG);
  20052. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  20053. BAD_FUNC_ARG);
  20054. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  20055. BAD_FUNC_ARG);
  20056. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  20057. BAD_FUNC_ARG);
  20058. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20059. wc_ed25519_free(&key);
  20060. #endif
  20061. return EXPECT_RESULT();
  20062. } /* END test_wc_ed25519_make_key */
  20063. /*
  20064. * Testing wc_ed25519_init()
  20065. */
  20066. static int test_wc_ed25519_init(void)
  20067. {
  20068. EXPECT_DECLS;
  20069. #if defined(HAVE_ED25519)
  20070. ed25519_key key;
  20071. XMEMSET(&key, 0, sizeof(ed25519_key));
  20072. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20073. /* Test bad args. */
  20074. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  20075. wc_ed25519_free(&key);
  20076. #endif
  20077. return EXPECT_RESULT();
  20078. } /* END test_wc_ed25519_init */
  20079. /*
  20080. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  20081. */
  20082. static int test_wc_ed25519_sign_msg(void)
  20083. {
  20084. EXPECT_DECLS;
  20085. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  20086. WC_RNG rng;
  20087. ed25519_key key;
  20088. byte msg[] = "Everybody gets Friday off.\n";
  20089. byte sig[ED25519_SIG_SIZE+1];
  20090. word32 msglen = sizeof(msg);
  20091. word32 siglen = ED25519_SIG_SIZE;
  20092. word32 badSigLen = ED25519_SIG_SIZE - 1;
  20093. #ifdef HAVE_ED25519_VERIFY
  20094. int verify_ok = 0; /*1 = Verify success.*/
  20095. #endif
  20096. /* Initialize stack variables. */
  20097. XMEMSET(&key, 0, sizeof(ed25519_key));
  20098. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20099. XMEMSET(sig, 0, sizeof(sig));
  20100. /* Initialize key. */
  20101. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20102. ExpectIntEQ(wc_InitRng(&rng), 0);
  20103. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20104. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  20105. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20106. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  20107. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  20108. /* Test bad args. */
  20109. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  20110. BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  20112. BAD_FUNC_ARG);
  20113. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  20114. BAD_FUNC_ARG);
  20115. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  20116. BAD_FUNC_ARG);
  20117. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20118. BUFFER_E);
  20119. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20120. badSigLen -= 1;
  20121. #ifdef HAVE_ED25519_VERIFY
  20122. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20123. &key), 0);
  20124. ExpectIntEQ(verify_ok, 1);
  20125. /* Test bad args. */
  20126. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20127. &verify_ok, &key), BAD_FUNC_ARG);
  20128. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20129. &verify_ok, &key), BAD_FUNC_ARG);
  20130. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20131. &key), BAD_FUNC_ARG);
  20132. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20133. &key), BAD_FUNC_ARG);
  20134. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20135. BAD_FUNC_ARG);
  20136. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20137. NULL), BAD_FUNC_ARG);
  20138. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20139. &key), BAD_FUNC_ARG);
  20140. #endif /* Verify. */
  20141. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20142. wc_ed25519_free(&key);
  20143. #endif
  20144. return EXPECT_RESULT();
  20145. } /* END test_wc_ed25519_sign_msg */
  20146. /*
  20147. * Testing wc_ed25519_import_public()
  20148. */
  20149. static int test_wc_ed25519_import_public(void)
  20150. {
  20151. EXPECT_DECLS;
  20152. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20153. ed25519_key pubKey;
  20154. WC_RNG rng;
  20155. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20156. word32 inlen = sizeof(in);
  20157. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20158. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20159. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20160. ExpectIntEQ(wc_InitRng(&rng), 0);
  20161. #ifdef HAVE_ED25519_MAKE_KEY
  20162. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20163. #endif
  20164. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20165. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20166. /* Test bad args. */
  20167. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20168. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20169. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20170. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20171. wc_ed25519_free(&pubKey);
  20172. #endif
  20173. return EXPECT_RESULT();
  20174. } /* END wc_ed25519_import_public */
  20175. /*
  20176. * Testing wc_ed25519_import_private_key()
  20177. */
  20178. static int test_wc_ed25519_import_private_key(void)
  20179. {
  20180. EXPECT_DECLS;
  20181. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20182. ed25519_key key;
  20183. WC_RNG rng;
  20184. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20185. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20186. word32 privKeySz = sizeof(privKey);
  20187. word32 pubKeySz = sizeof(pubKey);
  20188. #ifdef HAVE_ED25519_KEY_EXPORT
  20189. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20190. word32 bothKeysSz = sizeof(bothKeys);
  20191. #endif
  20192. XMEMSET(&key, 0, sizeof(ed25519_key));
  20193. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20194. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20195. ExpectIntEQ(wc_InitRng(&rng), 0);
  20196. #ifdef HAVE_ED25519_MAKE_KEY
  20197. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20198. #endif
  20199. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20200. pubKeySz, &key, 1), 0);
  20201. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20202. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20203. #ifdef HAVE_ED25519_KEY_EXPORT
  20204. PRIVATE_KEY_UNLOCK();
  20205. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20206. PRIVATE_KEY_LOCK();
  20207. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20208. &key, 1), 0);
  20209. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20210. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20211. #endif
  20212. /* Test bad args. */
  20213. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20214. &key), BAD_FUNC_ARG);
  20215. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20216. pubKeySz, &key), BAD_FUNC_ARG);
  20217. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20218. pubKeySz, NULL), BAD_FUNC_ARG);
  20219. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20220. pubKeySz, &key), BAD_FUNC_ARG);
  20221. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20222. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20223. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20224. &key), BAD_FUNC_ARG);
  20225. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20226. wc_ed25519_free(&key);
  20227. #endif
  20228. return EXPECT_RESULT();
  20229. } /* END test_wc_ed25519_import_private_key */
  20230. /*
  20231. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20232. */
  20233. static int test_wc_ed25519_export(void)
  20234. {
  20235. EXPECT_DECLS;
  20236. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20237. ed25519_key key;
  20238. WC_RNG rng;
  20239. byte priv[ED25519_PRV_KEY_SIZE];
  20240. byte pub[ED25519_PUB_KEY_SIZE];
  20241. word32 privSz = sizeof(priv);
  20242. word32 pubSz = sizeof(pub);
  20243. #ifndef HAVE_ED25519_MAKE_KEY
  20244. const byte privKey[] = {
  20245. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20246. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20247. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20248. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20249. };
  20250. const byte pubKey[] = {
  20251. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20252. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20253. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20254. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20255. };
  20256. #endif
  20257. XMEMSET(&key, 0, sizeof(ed25519_key));
  20258. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20259. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20260. ExpectIntEQ(wc_InitRng(&rng), 0);
  20261. #ifdef HAVE_ED25519_MAKE_KEY
  20262. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20263. #else
  20264. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20265. pubKey, sizeof(pubKey), &key, 1), 0);
  20266. #endif
  20267. PRIVATE_KEY_UNLOCK();
  20268. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20269. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20270. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20271. /* Test bad args. */
  20272. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20273. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20274. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20275. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20276. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20277. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20278. /* Test bad args. */
  20279. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20280. BAD_FUNC_ARG);
  20281. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20282. BAD_FUNC_ARG);
  20283. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20284. BAD_FUNC_ARG);
  20285. PRIVATE_KEY_LOCK();
  20286. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20287. wc_ed25519_free(&key);
  20288. #endif
  20289. return EXPECT_RESULT();
  20290. } /* END test_wc_ed25519_export */
  20291. /*
  20292. * Testing wc_ed25519_size()
  20293. */
  20294. static int test_wc_ed25519_size(void)
  20295. {
  20296. EXPECT_DECLS;
  20297. #if defined(HAVE_ED25519)
  20298. ed25519_key key;
  20299. WC_RNG rng;
  20300. #ifndef HAVE_ED25519_MAKE_KEY
  20301. const byte privKey[] = {
  20302. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20303. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20304. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20305. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20306. };
  20307. const byte pubKey[] = {
  20308. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20309. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20310. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20311. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20312. };
  20313. #endif
  20314. XMEMSET(&key, 0, sizeof(ed25519_key));
  20315. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20316. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20317. ExpectIntEQ(wc_InitRng(&rng), 0);
  20318. #ifdef HAVE_ED25519_MAKE_KEY
  20319. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20320. #else
  20321. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20322. pubKey, sizeof(pubKey), &key, 1), 0);
  20323. #endif
  20324. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20325. /* Test bad args. */
  20326. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20327. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20328. /* Test bad args. */
  20329. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20330. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20331. /* Test bad args. */
  20332. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20333. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20334. /* Test bad args. */
  20335. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20336. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20337. wc_ed25519_free(&key);
  20338. #endif
  20339. return EXPECT_RESULT();
  20340. } /* END test_wc_ed25519_size */
  20341. /*
  20342. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20343. */
  20344. static int test_wc_ed25519_exportKey(void)
  20345. {
  20346. EXPECT_DECLS;
  20347. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20348. WC_RNG rng;
  20349. ed25519_key key;
  20350. byte priv[ED25519_PRV_KEY_SIZE];
  20351. byte pub[ED25519_PUB_KEY_SIZE];
  20352. byte privOnly[ED25519_PRV_KEY_SIZE];
  20353. word32 privSz = sizeof(priv);
  20354. word32 pubSz = sizeof(pub);
  20355. word32 privOnlySz = sizeof(privOnly);
  20356. #ifndef HAVE_ED25519_MAKE_KEY
  20357. const byte privKey[] = {
  20358. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20359. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20360. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20361. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20362. };
  20363. const byte pubKey[] = {
  20364. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20365. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20366. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20367. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20368. };
  20369. #endif
  20370. XMEMSET(&key, 0, sizeof(ed25519_key));
  20371. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20372. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20373. ExpectIntEQ(wc_InitRng(&rng), 0);
  20374. #ifdef HAVE_ED25519_MAKE_KEY
  20375. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20376. #else
  20377. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20378. pubKey, sizeof(pubKey), &key, 1), 0);
  20379. #endif
  20380. PRIVATE_KEY_UNLOCK();
  20381. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20382. /* Test bad args. */
  20383. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20384. BAD_FUNC_ARG);
  20385. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20386. BAD_FUNC_ARG);
  20387. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20388. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20389. /* Test bad args. */
  20390. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20391. BAD_FUNC_ARG);
  20392. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20393. BAD_FUNC_ARG);
  20394. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20395. BAD_FUNC_ARG);
  20396. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20397. BAD_FUNC_ARG);
  20398. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20399. BAD_FUNC_ARG);
  20400. PRIVATE_KEY_LOCK();
  20401. /* Cross check output. */
  20402. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20403. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20404. wc_ed25519_free(&key);
  20405. #endif
  20406. return EXPECT_RESULT();
  20407. } /* END test_wc_ed25519_exportKey */
  20408. /*
  20409. * Testing wc_Ed25519PublicKeyToDer
  20410. */
  20411. static int test_wc_Ed25519PublicKeyToDer(void)
  20412. {
  20413. EXPECT_DECLS;
  20414. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20415. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20416. ed25519_key key;
  20417. byte derBuf[1024];
  20418. XMEMSET(&key, 0, sizeof(ed25519_key));
  20419. /* Test bad args */
  20420. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20421. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20422. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20423. wc_ed25519_free(&key);
  20424. /* Test good args */
  20425. if (EXPECT_SUCCESS()) {
  20426. WC_RNG rng;
  20427. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20428. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20429. ExpectIntEQ(wc_InitRng(&rng), 0);
  20430. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20431. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20432. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20433. wc_ed25519_free(&key);
  20434. }
  20435. #endif
  20436. return EXPECT_RESULT();
  20437. } /* END testing wc_Ed25519PublicKeyToDer */
  20438. /*
  20439. * Testing wc_curve25519_init and wc_curve25519_free.
  20440. */
  20441. static int test_wc_curve25519_init(void)
  20442. {
  20443. EXPECT_DECLS;
  20444. #if defined(HAVE_CURVE25519)
  20445. curve25519_key key;
  20446. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20447. /* Test bad args for wc_curve25519_init */
  20448. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20449. /* Test good args for wc_curve_25519_free */
  20450. wc_curve25519_free(&key);
  20451. /* Test bad args for wc_curve25519 free. */
  20452. wc_curve25519_free(NULL);
  20453. #endif
  20454. return EXPECT_RESULT();
  20455. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20456. /*
  20457. * Testing test_wc_curve25519_size.
  20458. */
  20459. static int test_wc_curve25519_size(void)
  20460. {
  20461. EXPECT_DECLS;
  20462. #if defined(HAVE_CURVE25519)
  20463. curve25519_key key;
  20464. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20465. /* Test good args for wc_curve25519_size */
  20466. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20467. /* Test bad args for wc_curve25519_size */
  20468. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20469. wc_curve25519_free(&key);
  20470. #endif
  20471. return EXPECT_RESULT();
  20472. } /* END test_wc_curve25519_size*/
  20473. /*
  20474. * Testing test_wc_curve25519_export_key_raw().
  20475. */
  20476. static int test_wc_curve25519_export_key_raw(void)
  20477. {
  20478. EXPECT_DECLS;
  20479. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20480. curve25519_key key;
  20481. WC_RNG rng;
  20482. byte privateKey[CURVE25519_KEYSIZE];
  20483. byte publicKey[CURVE25519_KEYSIZE];
  20484. word32 prvkSz;
  20485. word32 pubkSz;
  20486. byte prik[CURVE25519_KEYSIZE];
  20487. byte pubk[CURVE25519_KEYSIZE];
  20488. word32 prksz;
  20489. word32 pbksz;
  20490. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20491. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20492. ExpectIntEQ(wc_InitRng(&rng), 0);
  20493. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20494. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20495. prvkSz = CURVE25519_KEYSIZE;
  20496. pubkSz = CURVE25519_KEYSIZE;
  20497. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20498. publicKey, &pubkSz), BAD_FUNC_ARG);
  20499. prvkSz = CURVE25519_KEYSIZE;
  20500. pubkSz = CURVE25519_KEYSIZE;
  20501. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20502. &pubkSz), BAD_FUNC_ARG);
  20503. prvkSz = CURVE25519_KEYSIZE;
  20504. pubkSz = CURVE25519_KEYSIZE;
  20505. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20506. publicKey, &pubkSz), BAD_FUNC_ARG);
  20507. /* prvkSz = CURVE25519_KEYSIZE; */
  20508. pubkSz = CURVE25519_KEYSIZE;
  20509. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20510. NULL, &pubkSz), BAD_FUNC_ARG);
  20511. prvkSz = CURVE25519_KEYSIZE;
  20512. pubkSz = CURVE25519_KEYSIZE;
  20513. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20514. publicKey, NULL), BAD_FUNC_ARG);
  20515. /* cross-testing */
  20516. prksz = CURVE25519_KEYSIZE;
  20517. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20518. pbksz = CURVE25519_KEYSIZE;
  20519. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20520. prvkSz = CURVE25519_KEYSIZE;
  20521. /* pubkSz = CURVE25519_KEYSIZE; */
  20522. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20523. publicKey, &pubkSz), 0);
  20524. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20525. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20526. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20527. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20528. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20529. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20530. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20531. wc_curve25519_free(&key);
  20532. #endif
  20533. return EXPECT_RESULT();
  20534. } /* end of test_wc_curve25519_export_key_raw */
  20535. /*
  20536. * Testing test_wc_curve25519_export_key_raw_ex().
  20537. */
  20538. static int test_wc_curve25519_export_key_raw_ex(void)
  20539. {
  20540. EXPECT_DECLS;
  20541. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20542. curve25519_key key;
  20543. WC_RNG rng;
  20544. byte privateKey[CURVE25519_KEYSIZE];
  20545. byte publicKey[CURVE25519_KEYSIZE];
  20546. word32 prvkSz;
  20547. word32 pubkSz;
  20548. byte prik[CURVE25519_KEYSIZE];
  20549. byte pubk[CURVE25519_KEYSIZE];
  20550. word32 prksz;
  20551. word32 pbksz;
  20552. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20553. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20554. ExpectIntEQ(wc_InitRng(&rng), 0);
  20555. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20556. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20557. prvkSz = CURVE25519_KEYSIZE;
  20558. pubkSz = CURVE25519_KEYSIZE;
  20559. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20560. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20561. prvkSz = CURVE25519_KEYSIZE;
  20562. pubkSz = CURVE25519_KEYSIZE;
  20563. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20564. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20565. prvkSz = CURVE25519_KEYSIZE;
  20566. pubkSz = CURVE25519_KEYSIZE;
  20567. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20568. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20569. /* prvkSz = CURVE25519_KEYSIZE; */
  20570. pubkSz = CURVE25519_KEYSIZE;
  20571. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20572. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20573. prvkSz = CURVE25519_KEYSIZE;
  20574. pubkSz = CURVE25519_KEYSIZE;
  20575. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20576. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20577. prvkSz = CURVE25519_KEYSIZE;
  20578. /* pubkSz = CURVE25519_KEYSIZE; */
  20579. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20580. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20581. prvkSz = CURVE25519_KEYSIZE;
  20582. pubkSz = CURVE25519_KEYSIZE;
  20583. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20584. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20585. prvkSz = CURVE25519_KEYSIZE;
  20586. pubkSz = CURVE25519_KEYSIZE;
  20587. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20588. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20589. /* prvkSz = CURVE25519_KEYSIZE; */
  20590. pubkSz = CURVE25519_KEYSIZE;
  20591. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20592. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20593. prvkSz = CURVE25519_KEYSIZE;
  20594. pubkSz = CURVE25519_KEYSIZE;
  20595. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20596. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20597. /* illegal value for endian */
  20598. prvkSz = CURVE25519_KEYSIZE;
  20599. /* pubkSz = CURVE25519_KEYSIZE; */
  20600. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20601. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20602. /* cross-testing */
  20603. prksz = CURVE25519_KEYSIZE;
  20604. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20605. pbksz = CURVE25519_KEYSIZE;
  20606. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20607. prvkSz = CURVE25519_KEYSIZE;
  20608. /* pubkSz = CURVE25519_KEYSIZE; */
  20609. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20610. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20611. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20612. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20613. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20614. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20615. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20616. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20617. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20618. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20619. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20620. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20621. /* try once with another endian */
  20622. prvkSz = CURVE25519_KEYSIZE;
  20623. pubkSz = CURVE25519_KEYSIZE;
  20624. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20625. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20626. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20627. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20628. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20629. wc_curve25519_free(&key);
  20630. #endif
  20631. return EXPECT_RESULT();
  20632. } /* end of test_wc_curve25519_export_key_raw_ex */
  20633. /*
  20634. * Testing wc_curve25519_make_key
  20635. */
  20636. static int test_wc_curve25519_make_key(void)
  20637. {
  20638. EXPECT_DECLS;
  20639. #if defined(HAVE_CURVE25519)
  20640. curve25519_key key;
  20641. WC_RNG rng;
  20642. int keysize;
  20643. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20644. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20645. ExpectIntEQ(wc_InitRng(&rng), 0);
  20646. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20647. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20648. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20649. /* test bad cases*/
  20650. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20651. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20652. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20653. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20654. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20655. wc_curve25519_free(&key);
  20656. #endif
  20657. return EXPECT_RESULT();
  20658. } /* END test_wc_curve25519_make_key*/
  20659. /*
  20660. * Testing wc_curve25519_shared_secret_ex
  20661. */
  20662. static int test_wc_curve25519_shared_secret_ex(void)
  20663. {
  20664. EXPECT_DECLS;
  20665. #if defined(HAVE_CURVE25519)
  20666. curve25519_key private_key;
  20667. curve25519_key public_key;
  20668. WC_RNG rng;
  20669. byte out[CURVE25519_KEYSIZE];
  20670. word32 outLen = sizeof(out);
  20671. int endian = EC25519_BIG_ENDIAN;
  20672. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20673. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20674. ExpectIntEQ(wc_InitRng(&rng), 0);
  20675. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20676. 0);
  20677. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20678. 0);
  20679. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20680. &outLen, endian), 0);
  20681. /* test bad cases*/
  20682. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20683. BAD_FUNC_ARG);
  20684. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20685. endian), BAD_FUNC_ARG);
  20686. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20687. endian), BAD_FUNC_ARG);
  20688. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20689. &outLen, endian), BAD_FUNC_ARG);
  20690. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20691. NULL, endian), BAD_FUNC_ARG);
  20692. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20693. * increasing to 0x8f checks for error being returned*/
  20694. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20695. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20696. &outLen, endian), ECC_BAD_ARG_E);
  20697. outLen = outLen - 2;
  20698. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20699. &outLen, endian), BAD_FUNC_ARG);
  20700. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20701. wc_curve25519_free(&private_key);
  20702. wc_curve25519_free(&public_key);
  20703. #endif
  20704. return EXPECT_RESULT();
  20705. } /* END test_wc_curve25519_shared_secret_ex*/
  20706. /*
  20707. * Testing wc_curve25519_make_pub
  20708. */
  20709. static int test_wc_curve25519_make_pub(void)
  20710. {
  20711. EXPECT_DECLS;
  20712. #ifdef HAVE_CURVE25519
  20713. curve25519_key key;
  20714. WC_RNG rng;
  20715. byte out[CURVE25519_KEYSIZE];
  20716. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20717. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20718. ExpectIntEQ(wc_InitRng(&rng), 0);
  20719. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20720. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20721. (int)sizeof(key.k), key.k), 0);
  20722. /* test bad cases*/
  20723. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20724. (int)sizeof out, out), ECC_BAD_ARG_E);
  20725. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20726. NULL), ECC_BAD_ARG_E);
  20727. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20728. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20729. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20730. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20731. /* verify clamping test */
  20732. key.k[0] |= ~248;
  20733. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20734. key.k), ECC_BAD_ARG_E);
  20735. key.k[0] &= 248;
  20736. /* repeat the expected-to-succeed test. */
  20737. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20738. key.k), 0);
  20739. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20740. wc_curve25519_free(&key);
  20741. #endif
  20742. return EXPECT_RESULT();
  20743. } /* END test_wc_curve25519_make_pub */
  20744. /*
  20745. * Testing test_wc_curve25519_export_public_ex
  20746. */
  20747. static int test_wc_curve25519_export_public_ex(void)
  20748. {
  20749. EXPECT_DECLS;
  20750. #if defined(HAVE_CURVE25519)
  20751. curve25519_key key;
  20752. WC_RNG rng;
  20753. byte out[CURVE25519_KEYSIZE];
  20754. word32 outLen = sizeof(out);
  20755. int endian = EC25519_BIG_ENDIAN;
  20756. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20757. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20758. ExpectIntEQ(wc_InitRng(&rng), 0);
  20759. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20760. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20761. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20762. /* test bad cases*/
  20763. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20764. BAD_FUNC_ARG);
  20765. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20766. BAD_FUNC_ARG);
  20767. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20768. BAD_FUNC_ARG);
  20769. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20770. BAD_FUNC_ARG);
  20771. outLen = outLen - 2;
  20772. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20773. ECC_BAD_ARG_E);
  20774. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20775. wc_curve25519_free(&key);
  20776. #endif
  20777. return EXPECT_RESULT();
  20778. } /* END test_wc_curve25519_export_public_ex*/
  20779. /*
  20780. * Testing test_wc_curve25519_import_private_raw_ex
  20781. */
  20782. static int test_wc_curve25519_import_private_raw_ex(void)
  20783. {
  20784. EXPECT_DECLS;
  20785. #if defined(HAVE_CURVE25519)
  20786. curve25519_key key;
  20787. WC_RNG rng;
  20788. byte priv[CURVE25519_KEYSIZE];
  20789. byte pub[CURVE25519_KEYSIZE];
  20790. word32 privSz = sizeof(priv);
  20791. word32 pubSz = sizeof(pub);
  20792. int endian = EC25519_BIG_ENDIAN;
  20793. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20794. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20795. ExpectIntEQ(wc_InitRng(&rng), 0);
  20796. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20797. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20798. endian), 0);
  20799. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20800. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20801. &key, endian), 0);
  20802. /* test bad cases*/
  20803. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20804. endian), BAD_FUNC_ARG);
  20805. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20806. &key, endian), BAD_FUNC_ARG);
  20807. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20808. &key, endian), BAD_FUNC_ARG);
  20809. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20810. NULL, endian), BAD_FUNC_ARG);
  20811. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20812. &key, endian), ECC_BAD_ARG_E);
  20813. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20814. &key, endian), ECC_BAD_ARG_E);
  20815. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20816. &key, EC25519_LITTLE_ENDIAN), 0);
  20817. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20818. wc_curve25519_free(&key);
  20819. #endif
  20820. return EXPECT_RESULT();
  20821. } /* END test_wc_curve25519_import_private_raw_ex*/
  20822. /*
  20823. * Testing test_wc_curve25519_import_private
  20824. */
  20825. static int test_wc_curve25519_import_private(void)
  20826. {
  20827. EXPECT_DECLS;
  20828. #if defined(HAVE_CURVE25519)
  20829. curve25519_key key;
  20830. WC_RNG rng;
  20831. byte priv[CURVE25519_KEYSIZE];
  20832. word32 privSz = sizeof(priv);
  20833. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20834. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20835. ExpectIntEQ(wc_InitRng(&rng), 0);
  20836. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20837. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20838. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20839. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20840. wc_curve25519_free(&key);
  20841. #endif
  20842. return EXPECT_RESULT();
  20843. } /* END test_wc_curve25519_import*/
  20844. /*
  20845. * Testing test_wc_curve25519_export_private_raw_ex
  20846. */
  20847. static int test_wc_curve25519_export_private_raw_ex(void)
  20848. {
  20849. EXPECT_DECLS;
  20850. #if defined(HAVE_CURVE25519)
  20851. curve25519_key key;
  20852. byte out[CURVE25519_KEYSIZE];
  20853. word32 outLen = sizeof(out);
  20854. int endian = EC25519_BIG_ENDIAN;
  20855. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20856. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20857. 0);
  20858. /* test bad cases*/
  20859. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20860. BAD_FUNC_ARG);
  20861. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20862. BAD_FUNC_ARG);
  20863. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20864. endian), BAD_FUNC_ARG);
  20865. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20866. BAD_FUNC_ARG);
  20867. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20868. EC25519_LITTLE_ENDIAN), 0);
  20869. outLen = outLen - 2;
  20870. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20871. ECC_BAD_ARG_E);
  20872. wc_curve25519_free(&key);
  20873. #endif
  20874. return EXPECT_RESULT();
  20875. } /* END test_wc_curve25519_export_private_raw_ex*/
  20876. /*
  20877. * Testing wc_ed448_make_key().
  20878. */
  20879. static int test_wc_ed448_make_key(void)
  20880. {
  20881. EXPECT_DECLS;
  20882. #if defined(HAVE_ED448)
  20883. ed448_key key;
  20884. WC_RNG rng;
  20885. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20886. XMEMSET(&key, 0, sizeof(ed448_key));
  20887. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20888. ExpectIntEQ(wc_ed448_init(&key), 0);
  20889. ExpectIntEQ(wc_InitRng(&rng), 0);
  20890. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20891. ECC_PRIV_KEY_E);
  20892. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20893. /* Test bad args. */
  20894. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20895. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20896. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20897. BAD_FUNC_ARG);
  20898. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20899. BAD_FUNC_ARG);
  20900. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20901. wc_ed448_free(&key);
  20902. #endif
  20903. return EXPECT_RESULT();
  20904. } /* END test_wc_ed448_make_key */
  20905. /*
  20906. * Testing wc_ed448_init()
  20907. */
  20908. static int test_wc_ed448_init(void)
  20909. {
  20910. EXPECT_DECLS;
  20911. #if defined(HAVE_ED448)
  20912. ed448_key key;
  20913. XMEMSET(&key, 0, sizeof(ed448_key));
  20914. ExpectIntEQ(wc_ed448_init(&key), 0);
  20915. /* Test bad args. */
  20916. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20917. wc_ed448_free(&key);
  20918. #endif
  20919. return EXPECT_RESULT();
  20920. } /* END test_wc_ed448_init */
  20921. /*
  20922. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20923. */
  20924. static int test_wc_ed448_sign_msg(void)
  20925. {
  20926. EXPECT_DECLS;
  20927. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20928. ed448_key key;
  20929. WC_RNG rng;
  20930. byte msg[] = "Everybody gets Friday off.\n";
  20931. byte sig[ED448_SIG_SIZE];
  20932. word32 msglen = sizeof(msg);
  20933. word32 siglen = sizeof(sig);
  20934. word32 badSigLen = sizeof(sig) - 1;
  20935. #ifdef HAVE_ED448_VERIFY
  20936. int verify_ok = 0; /*1 = Verify success.*/
  20937. #endif
  20938. /* Initialize stack variables. */
  20939. XMEMSET(&key, 0, sizeof(ed448_key));
  20940. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20941. XMEMSET(sig, 0, siglen);
  20942. /* Initialize key. */
  20943. ExpectIntEQ(wc_ed448_init(&key), 0);
  20944. ExpectIntEQ(wc_InitRng(&rng), 0);
  20945. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20946. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20947. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20948. /* Test bad args. */
  20949. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20950. BAD_FUNC_ARG);
  20951. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20952. BAD_FUNC_ARG);
  20953. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20954. BAD_FUNC_ARG);
  20955. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20956. BAD_FUNC_ARG);
  20957. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20958. BUFFER_E);
  20959. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20960. badSigLen -= 1;
  20961. #ifdef HAVE_ED448_VERIFY
  20962. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20963. NULL, 0), 0);
  20964. ExpectIntEQ(verify_ok, 1);
  20965. /* Test bad args. */
  20966. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20967. &key, NULL, 0), BAD_FUNC_ARG);
  20968. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20969. &key, NULL, 0), BAD_FUNC_ARG);
  20970. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20971. &key, NULL, 0), BAD_FUNC_ARG);
  20972. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20973. &key, NULL, 0), BAD_FUNC_ARG);
  20974. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20975. &key, NULL, 0), BAD_FUNC_ARG);
  20976. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20977. NULL, NULL, 0), BAD_FUNC_ARG);
  20978. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20979. &key, NULL, 0), BAD_FUNC_ARG);
  20980. #endif /* Verify. */
  20981. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20982. wc_ed448_free(&key);
  20983. #endif
  20984. return EXPECT_RESULT();
  20985. } /* END test_wc_ed448_sign_msg */
  20986. /*
  20987. * Testing wc_ed448_import_public()
  20988. */
  20989. static int test_wc_ed448_import_public(void)
  20990. {
  20991. EXPECT_DECLS;
  20992. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20993. ed448_key pubKey;
  20994. WC_RNG rng;
  20995. const byte in[] =
  20996. "Ed448PublicKeyUnitTest.................................\n";
  20997. word32 inlen = sizeof(in);
  20998. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20999. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21000. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  21001. ExpectIntEQ(wc_InitRng(&rng), 0);
  21002. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  21003. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  21004. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  21005. /* Test bad args. */
  21006. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  21007. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  21008. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  21009. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21010. wc_ed448_free(&pubKey);
  21011. #endif
  21012. return EXPECT_RESULT();
  21013. } /* END wc_ed448_import_public */
  21014. /*
  21015. * Testing wc_ed448_import_private_key()
  21016. */
  21017. static int test_wc_ed448_import_private_key(void)
  21018. {
  21019. EXPECT_DECLS;
  21020. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  21021. ed448_key key;
  21022. WC_RNG rng;
  21023. const byte privKey[] =
  21024. "Ed448PrivateKeyUnitTest................................\n";
  21025. const byte pubKey[] =
  21026. "Ed448PublicKeyUnitTest.................................\n";
  21027. word32 privKeySz = sizeof(privKey);
  21028. word32 pubKeySz = sizeof(pubKey);
  21029. #ifdef HAVE_ED448_KEY_EXPORT
  21030. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  21031. word32 bothKeysSz = sizeof(bothKeys);
  21032. #endif
  21033. XMEMSET(&key, 0, sizeof(ed448_key));
  21034. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21035. ExpectIntEQ(wc_ed448_init(&key), 0);
  21036. ExpectIntEQ(wc_InitRng(&rng), 0);
  21037. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21038. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  21039. pubKeySz, &key, 1), 0);
  21040. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21041. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21042. #ifdef HAVE_ED448_KEY_EXPORT
  21043. PRIVATE_KEY_UNLOCK();
  21044. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  21045. PRIVATE_KEY_LOCK();
  21046. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  21047. &key, 1), 0);
  21048. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  21049. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  21050. #endif
  21051. /* Test bad args. */
  21052. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  21053. &key), BAD_FUNC_ARG);
  21054. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  21055. &key), BAD_FUNC_ARG);
  21056. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21057. pubKeySz, NULL), BAD_FUNC_ARG);
  21058. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  21059. pubKeySz, &key), BAD_FUNC_ARG);
  21060. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  21061. pubKeySz - 1, &key), BAD_FUNC_ARG);
  21062. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  21063. BAD_FUNC_ARG);
  21064. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21065. wc_ed448_free(&key);
  21066. #endif
  21067. return EXPECT_RESULT();
  21068. } /* END test_wc_ed448_import_private_key */
  21069. /*
  21070. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  21071. */
  21072. static int test_wc_ed448_export(void)
  21073. {
  21074. EXPECT_DECLS;
  21075. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21076. ed448_key key;
  21077. WC_RNG rng;
  21078. byte priv[ED448_PRV_KEY_SIZE];
  21079. byte pub[ED448_PUB_KEY_SIZE];
  21080. word32 privSz = sizeof(priv);
  21081. word32 pubSz = sizeof(pub);
  21082. XMEMSET(&key, 0, sizeof(ed448_key));
  21083. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21084. ExpectIntEQ(wc_ed448_init(&key), 0);
  21085. ExpectIntEQ(wc_InitRng(&rng), 0);
  21086. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21087. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  21088. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  21089. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  21090. /* Test bad args. */
  21091. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  21092. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  21093. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  21094. PRIVATE_KEY_UNLOCK();
  21095. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  21096. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  21097. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  21098. /* Test bad args. */
  21099. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  21100. BAD_FUNC_ARG);
  21101. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  21102. BAD_FUNC_ARG);
  21103. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  21104. PRIVATE_KEY_LOCK();
  21105. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21106. wc_ed448_free(&key);
  21107. #endif
  21108. return EXPECT_RESULT();
  21109. } /* END test_wc_ed448_export */
  21110. /*
  21111. * Testing wc_ed448_size()
  21112. */
  21113. static int test_wc_ed448_size(void)
  21114. {
  21115. EXPECT_DECLS;
  21116. #if defined(HAVE_ED448)
  21117. ed448_key key;
  21118. WC_RNG rng;
  21119. XMEMSET(&key, 0, sizeof(ed448_key));
  21120. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21121. ExpectIntEQ(wc_ed448_init(&key), 0);
  21122. ExpectIntEQ(wc_InitRng(&rng), 0);
  21123. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21124. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21125. /* Test bad args. */
  21126. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21127. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21128. /* Test bad args. */
  21129. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21130. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21131. /* Test bad args. */
  21132. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21133. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21134. /* Test bad args. */
  21135. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21136. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21137. wc_ed448_free(&key);
  21138. #endif
  21139. return EXPECT_RESULT();
  21140. } /* END test_wc_ed448_size */
  21141. /*
  21142. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21143. */
  21144. static int test_wc_ed448_exportKey(void)
  21145. {
  21146. EXPECT_DECLS;
  21147. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21148. ed448_key key;
  21149. WC_RNG rng;
  21150. byte priv[ED448_PRV_KEY_SIZE];
  21151. byte pub[ED448_PUB_KEY_SIZE];
  21152. byte privOnly[ED448_PRV_KEY_SIZE];
  21153. word32 privSz = sizeof(priv);
  21154. word32 pubSz = sizeof(pub);
  21155. word32 privOnlySz = sizeof(privOnly);
  21156. XMEMSET(&key, 0, sizeof(ed448_key));
  21157. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21158. ExpectIntEQ(wc_ed448_init(&key), 0);
  21159. ExpectIntEQ(wc_InitRng(&rng), 0);
  21160. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21161. PRIVATE_KEY_UNLOCK();
  21162. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21163. /* Test bad args. */
  21164. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21165. BAD_FUNC_ARG);
  21166. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21167. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21168. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21169. /* Test bad args. */
  21170. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21171. BAD_FUNC_ARG);
  21172. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21173. BAD_FUNC_ARG);
  21174. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21175. BAD_FUNC_ARG);
  21176. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21177. BAD_FUNC_ARG);
  21178. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21179. BAD_FUNC_ARG);
  21180. PRIVATE_KEY_LOCK();
  21181. /* Cross check output. */
  21182. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21183. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21184. wc_ed448_free(&key);
  21185. #endif
  21186. return EXPECT_RESULT();
  21187. } /* END test_wc_ed448_exportKey */
  21188. /*
  21189. * Testing wc_Ed448PublicKeyToDer
  21190. */
  21191. static int test_wc_Ed448PublicKeyToDer(void)
  21192. {
  21193. EXPECT_DECLS;
  21194. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21195. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21196. ed448_key key;
  21197. byte derBuf[1024];
  21198. XMEMSET(&key, 0, sizeof(ed448_key));
  21199. /* Test bad args */
  21200. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21201. ExpectIntEQ(wc_ed448_init(&key), 0);
  21202. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21203. wc_ed448_free(&key);
  21204. /* Test good args */
  21205. if (EXPECT_SUCCESS()) {
  21206. WC_RNG rng;
  21207. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21208. ExpectIntEQ(wc_ed448_init(&key), 0);
  21209. ExpectIntEQ(wc_InitRng(&rng), 0);
  21210. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21211. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21212. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21213. wc_ed448_free(&key);
  21214. }
  21215. #endif
  21216. return EXPECT_RESULT();
  21217. } /* END testing wc_Ed448PublicKeyToDer */
  21218. /*
  21219. * Testing wc_curve448_init and wc_curve448_free.
  21220. */
  21221. static int test_wc_curve448_init(void)
  21222. {
  21223. EXPECT_DECLS;
  21224. #if defined(HAVE_CURVE448)
  21225. curve448_key key;
  21226. /* Test bad args for wc_curve448_init */
  21227. ExpectIntEQ(wc_curve448_init(&key), 0);
  21228. /* Test bad args for wc_curve448_init */
  21229. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21230. /* Test good args for wc_curve_448_free */
  21231. wc_curve448_free(&key);
  21232. /* Test bad args for wc_curve448_free */
  21233. wc_curve448_free(NULL);
  21234. #endif
  21235. return EXPECT_RESULT();
  21236. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21237. /*
  21238. * Testing wc_curve448_make_key
  21239. */
  21240. static int test_wc_curve448_make_key(void)
  21241. {
  21242. EXPECT_DECLS;
  21243. #if defined(HAVE_CURVE448)
  21244. curve448_key key;
  21245. WC_RNG rng;
  21246. int keysize;
  21247. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21248. ExpectIntEQ(wc_curve448_init(&key), 0);
  21249. ExpectIntEQ(wc_InitRng(&rng), 0);
  21250. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21251. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21252. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21253. /* test bad cases */
  21254. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21255. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21256. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21257. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21258. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21259. wc_curve448_free(&key);
  21260. #endif
  21261. return EXPECT_RESULT();
  21262. } /* END test_wc_curve448_make_key*/
  21263. /*
  21264. * Testing test_wc_curve448_shared_secret_ex
  21265. */
  21266. static int test_wc_curve448_shared_secret_ex(void)
  21267. {
  21268. EXPECT_DECLS;
  21269. #if defined(HAVE_CURVE448)
  21270. curve448_key private_key;
  21271. curve448_key public_key;
  21272. WC_RNG rng;
  21273. byte out[CURVE448_KEY_SIZE];
  21274. word32 outLen = sizeof(out);
  21275. int endian = EC448_BIG_ENDIAN;
  21276. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21277. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21278. ExpectIntEQ(wc_InitRng(&rng), 0);
  21279. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21280. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21281. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21282. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21283. &outLen, endian), 0);
  21284. /* test bad cases */
  21285. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21286. BAD_FUNC_ARG);
  21287. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21288. endian), BAD_FUNC_ARG);
  21289. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21290. endian), BAD_FUNC_ARG);
  21291. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21292. &outLen, endian), BAD_FUNC_ARG);
  21293. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21294. NULL, endian), BAD_FUNC_ARG);
  21295. outLen = outLen - 2;
  21296. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21297. &outLen, endian), BAD_FUNC_ARG);
  21298. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21299. wc_curve448_free(&private_key);
  21300. wc_curve448_free(&public_key);
  21301. #endif
  21302. return EXPECT_RESULT();
  21303. } /* END test_wc_curve448_shared_secret_ex*/
  21304. /*
  21305. * Testing test_wc_curve448_export_public_ex
  21306. */
  21307. static int test_wc_curve448_export_public_ex(void)
  21308. {
  21309. EXPECT_DECLS;
  21310. #if defined(HAVE_CURVE448)
  21311. WC_RNG rng;
  21312. curve448_key key;
  21313. byte out[CURVE448_KEY_SIZE];
  21314. word32 outLen = sizeof(out);
  21315. int endian = EC448_BIG_ENDIAN;
  21316. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21317. ExpectIntEQ(wc_curve448_init(&key), 0);
  21318. ExpectIntEQ(wc_InitRng(&rng), 0);
  21319. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21320. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21321. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21322. /* test bad cases*/
  21323. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21324. BAD_FUNC_ARG);
  21325. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21326. BAD_FUNC_ARG);
  21327. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21328. BAD_FUNC_ARG);
  21329. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21330. BAD_FUNC_ARG);
  21331. outLen = outLen - 2;
  21332. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21333. ECC_BAD_ARG_E);
  21334. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21335. wc_curve448_free(&key);
  21336. #endif
  21337. return EXPECT_RESULT();
  21338. } /* END test_wc_curve448_export_public_ex*/
  21339. /*
  21340. * Testing test_wc_curve448_export_private_raw_ex
  21341. */
  21342. static int test_wc_curve448_export_private_raw_ex(void)
  21343. {
  21344. EXPECT_DECLS;
  21345. #if defined(HAVE_CURVE448)
  21346. curve448_key key;
  21347. byte out[CURVE448_KEY_SIZE];
  21348. word32 outLen = sizeof(out);
  21349. int endian = EC448_BIG_ENDIAN;
  21350. ExpectIntEQ(wc_curve448_init(&key), 0);
  21351. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21352. 0);
  21353. /* test bad cases*/
  21354. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21355. BAD_FUNC_ARG);
  21356. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21357. BAD_FUNC_ARG);
  21358. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21359. BAD_FUNC_ARG);
  21360. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21361. BAD_FUNC_ARG);
  21362. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21363. EC448_LITTLE_ENDIAN), 0);
  21364. outLen = outLen - 2;
  21365. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21366. ECC_BAD_ARG_E);
  21367. wc_curve448_free(&key);
  21368. #endif
  21369. return EXPECT_RESULT();
  21370. } /* END test_wc_curve448_export_private_raw_ex*/
  21371. /*
  21372. * Testing test_wc_curve448_import_private_raw_ex
  21373. */
  21374. static int test_wc_curve448_import_private_raw_ex(void)
  21375. {
  21376. EXPECT_DECLS;
  21377. #if defined(HAVE_CURVE448)
  21378. curve448_key key;
  21379. WC_RNG rng;
  21380. byte priv[CURVE448_KEY_SIZE];
  21381. byte pub[CURVE448_KEY_SIZE];
  21382. word32 privSz = sizeof(priv);
  21383. word32 pubSz = sizeof(pub);
  21384. int endian = EC448_BIG_ENDIAN;
  21385. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21386. ExpectIntEQ(wc_curve448_init(&key), 0);
  21387. ExpectIntEQ(wc_InitRng(&rng), 0);
  21388. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21389. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21390. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21391. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21392. &key, endian), 0);
  21393. /* test bad cases */
  21394. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21395. BAD_FUNC_ARG);
  21396. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21397. &key, endian), BAD_FUNC_ARG);
  21398. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21399. &key, endian), BAD_FUNC_ARG);
  21400. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21401. NULL, endian), BAD_FUNC_ARG);
  21402. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21403. &key, endian), ECC_BAD_ARG_E);
  21404. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21405. &key, endian), ECC_BAD_ARG_E);
  21406. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21407. &key, EC448_LITTLE_ENDIAN), 0);
  21408. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21409. wc_curve448_free(&key);
  21410. #endif
  21411. return EXPECT_RESULT();
  21412. } /* END test_wc_curve448_import_private_raw_ex*/
  21413. /*
  21414. * Testing test_curve448_export_key_raw
  21415. */
  21416. static int test_wc_curve448_export_key_raw(void)
  21417. {
  21418. EXPECT_DECLS;
  21419. #if defined(HAVE_CURVE448)
  21420. curve448_key key;
  21421. WC_RNG rng;
  21422. byte priv[CURVE448_KEY_SIZE];
  21423. byte pub[CURVE448_KEY_SIZE];
  21424. word32 privSz = sizeof(priv);
  21425. word32 pubSz = sizeof(pub);
  21426. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21427. ExpectIntEQ(wc_curve448_init(&key), 0);
  21428. ExpectIntEQ(wc_InitRng(&rng), 0);
  21429. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21430. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21431. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21432. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21433. 0);
  21434. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21435. wc_curve448_free(&key);
  21436. #endif
  21437. return EXPECT_RESULT();
  21438. } /* END test_wc_curve448_import_private_raw_ex*/
  21439. /*
  21440. * Testing test_wc_curve448_import_private
  21441. */
  21442. static int test_wc_curve448_import_private(void)
  21443. {
  21444. EXPECT_DECLS;
  21445. #if defined(HAVE_CURVE448)
  21446. curve448_key key;
  21447. WC_RNG rng;
  21448. byte priv[CURVE448_KEY_SIZE];
  21449. word32 privSz = sizeof(priv);
  21450. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21451. ExpectIntEQ(wc_curve448_init(&key), 0);
  21452. ExpectIntEQ(wc_InitRng(&rng), 0);
  21453. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21454. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21455. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21456. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21457. wc_curve448_free(&key);
  21458. #endif
  21459. return EXPECT_RESULT();
  21460. } /* END test_wc_curve448_import*/
  21461. /*
  21462. * Testing test_wc_curve448_size.
  21463. */
  21464. static int test_wc_curve448_size(void)
  21465. {
  21466. EXPECT_DECLS;
  21467. #if defined(HAVE_CURVE448)
  21468. curve448_key key;
  21469. ExpectIntEQ(wc_curve448_init(&key), 0);
  21470. /* Test good args for wc_curve448_size */
  21471. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21472. /* Test bad args for wc_curve448_size */
  21473. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21474. wc_curve448_free(&key);
  21475. #endif
  21476. return EXPECT_RESULT();
  21477. } /* END test_wc_curve448_size*/
  21478. /*
  21479. * Testing wc_ecc_make_key.
  21480. */
  21481. static int test_wc_ecc_make_key(void)
  21482. {
  21483. EXPECT_DECLS;
  21484. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21485. ecc_key key;
  21486. WC_RNG rng;
  21487. int ret;
  21488. XMEMSET(&key, 0, sizeof(ecc_key));
  21489. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21490. ExpectIntEQ(wc_ecc_init(&key), 0);
  21491. ExpectIntEQ(wc_InitRng(&rng), 0);
  21492. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21493. #if defined(WOLFSSL_ASYNC_CRYPT)
  21494. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21495. #endif
  21496. ExpectIntEQ(ret, 0);
  21497. /* Pass in bad args. */
  21498. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21499. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21500. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21501. wc_ecc_free(&key);
  21502. #ifdef FP_ECC
  21503. wc_ecc_fp_free();
  21504. #endif
  21505. #endif
  21506. return EXPECT_RESULT();
  21507. } /* END test_wc_ecc_make_key */
  21508. /*
  21509. * Testing wc_ecc_init()
  21510. */
  21511. static int test_wc_ecc_init(void)
  21512. {
  21513. EXPECT_DECLS;
  21514. #ifdef HAVE_ECC
  21515. ecc_key key;
  21516. XMEMSET(&key, 0, sizeof(ecc_key));
  21517. ExpectIntEQ(wc_ecc_init(&key), 0);
  21518. /* Pass in bad args. */
  21519. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21520. wc_ecc_free(&key);
  21521. #endif
  21522. return EXPECT_RESULT();
  21523. } /* END test_wc_ecc_init */
  21524. /*
  21525. * Testing wc_ecc_check_key()
  21526. */
  21527. static int test_wc_ecc_check_key(void)
  21528. {
  21529. EXPECT_DECLS;
  21530. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21531. ecc_key key;
  21532. WC_RNG rng;
  21533. int ret;
  21534. XMEMSET(&rng, 0, sizeof(rng));
  21535. XMEMSET(&key, 0, sizeof(key));
  21536. ExpectIntEQ(wc_ecc_init(&key), 0);
  21537. ExpectIntEQ(wc_InitRng(&rng), 0);
  21538. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21539. #if defined(WOLFSSL_ASYNC_CRYPT)
  21540. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21541. #endif
  21542. ExpectIntEQ(ret, 0);
  21543. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21544. /* Pass in bad args. */
  21545. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21546. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21547. wc_ecc_free(&key);
  21548. #ifdef FP_ECC
  21549. wc_ecc_fp_free();
  21550. #endif
  21551. #endif
  21552. return EXPECT_RESULT();
  21553. } /* END test_wc_ecc_check_key */
  21554. /*
  21555. * Testing wc_ecc_get_generator()
  21556. */
  21557. static int test_wc_ecc_get_generator(void)
  21558. {
  21559. EXPECT_DECLS;
  21560. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21561. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21562. ecc_point* pt = NULL;
  21563. ExpectNotNull(pt = wc_ecc_new_point());
  21564. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21565. MP_OKAY);
  21566. /* Test bad args. */
  21567. /* Returns Zero for bad arg. */
  21568. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21569. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21570. MP_OKAY);
  21571. /* If we ever get to 1000 curves increase this number */
  21572. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21573. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21574. wc_ecc_del_point(pt);
  21575. #endif
  21576. return EXPECT_RESULT();
  21577. } /* END test_wc_ecc_get_generator */
  21578. /*
  21579. * Testing wc_ecc_size()
  21580. */
  21581. static int test_wc_ecc_size(void)
  21582. {
  21583. EXPECT_DECLS;
  21584. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21585. WC_RNG rng;
  21586. ecc_key key;
  21587. int ret;
  21588. XMEMSET(&key, 0, sizeof(ecc_key));
  21589. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21590. ExpectIntEQ(wc_ecc_init(&key), 0);
  21591. ExpectIntEQ(wc_InitRng(&rng), 0);
  21592. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21593. #if defined(WOLFSSL_ASYNC_CRYPT)
  21594. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21595. #endif
  21596. ExpectIntEQ(ret, 0);
  21597. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21598. /* Test bad args. */
  21599. /* Returns Zero for bad arg. */
  21600. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21601. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21602. wc_ecc_free(&key);
  21603. #endif
  21604. return EXPECT_RESULT();
  21605. } /* END test_wc_ecc_size */
  21606. static int test_wc_ecc_params(void)
  21607. {
  21608. EXPECT_DECLS;
  21609. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21610. It was added after certifications */
  21611. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21612. const ecc_set_type* ecc_set;
  21613. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21614. /* Test for SECP256R1 curve */
  21615. int curve_id = ECC_SECP256R1;
  21616. int curve_idx;
  21617. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21618. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21619. ExpectIntEQ(ecc_set->id, curve_id);
  21620. #endif
  21621. /* Test case when SECP256R1 is not enabled */
  21622. /* Test that we get curve params for index 0 */
  21623. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21624. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21625. return EXPECT_RESULT();
  21626. }
  21627. /*
  21628. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21629. */
  21630. static int test_wc_ecc_signVerify_hash(void)
  21631. {
  21632. EXPECT_DECLS;
  21633. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21634. ecc_key key;
  21635. WC_RNG rng;
  21636. int ret;
  21637. #ifdef HAVE_ECC_VERIFY
  21638. int verify = 0;
  21639. #endif
  21640. word32 siglen = ECC_BUFSIZE;
  21641. byte sig[ECC_BUFSIZE];
  21642. byte adjustedSig[ECC_BUFSIZE+1];
  21643. byte digest[] = TEST_STRING;
  21644. word32 digestlen = (word32)TEST_STRING_SZ;
  21645. /* Init stack var */
  21646. XMEMSET(&key, 0, sizeof(ecc_key));
  21647. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21648. XMEMSET(sig, 0, siglen);
  21649. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21650. /* Init structs. */
  21651. ExpectIntEQ(wc_ecc_init(&key), 0);
  21652. ExpectIntEQ(wc_InitRng(&rng), 0);
  21653. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21654. #if defined(WOLFSSL_ASYNC_CRYPT)
  21655. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21656. #endif
  21657. ExpectIntEQ(ret, 0);
  21658. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21659. 0);
  21660. /* Check bad args. */
  21661. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21662. ECC_BAD_ARG_E);
  21663. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21664. ECC_BAD_ARG_E);
  21665. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21666. ECC_BAD_ARG_E);
  21667. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21668. ECC_BAD_ARG_E);
  21669. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21670. ECC_BAD_ARG_E);
  21671. #ifdef HAVE_ECC_VERIFY
  21672. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21673. &key), 0);
  21674. ExpectIntEQ(verify, 1);
  21675. /* test check on length of signature passed in */
  21676. XMEMCPY(adjustedSig, sig, siglen);
  21677. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21678. #ifndef NO_STRICT_ECDSA_LEN
  21679. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21680. &verify, &key), 0);
  21681. #else
  21682. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21683. * is allowed */
  21684. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21685. &verify, &key), 0);
  21686. #endif
  21687. /* Test bad args. */
  21688. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21689. &key), ECC_BAD_ARG_E);
  21690. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21691. ECC_BAD_ARG_E);
  21692. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21693. ECC_BAD_ARG_E);
  21694. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21695. NULL), ECC_BAD_ARG_E);
  21696. #endif /* HAVE_ECC_VERIFY */
  21697. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21698. wc_ecc_free(&key);
  21699. #ifdef FP_ECC
  21700. wc_ecc_fp_free();
  21701. #endif
  21702. #endif
  21703. return EXPECT_RESULT();
  21704. } /* END test_wc_ecc_sign_hash */
  21705. /*
  21706. * Testing wc_ecc_shared_secret()
  21707. */
  21708. static int test_wc_ecc_shared_secret(void)
  21709. {
  21710. EXPECT_DECLS;
  21711. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21712. ecc_key key;
  21713. ecc_key pubKey;
  21714. WC_RNG rng;
  21715. #if defined(NO_ECC256)
  21716. int ret;
  21717. #endif
  21718. byte out[KEY32];
  21719. int keySz = sizeof(out);
  21720. word32 outlen = (word32)sizeof(out);
  21721. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21722. const char* qx =
  21723. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21724. const char* qy =
  21725. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21726. const char* d =
  21727. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21728. const char* curveName = "SECP256R1";
  21729. const byte expected_shared_secret[] =
  21730. {
  21731. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21732. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21733. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21734. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21735. };
  21736. #endif
  21737. PRIVATE_KEY_UNLOCK();
  21738. /* Initialize variables. */
  21739. XMEMSET(&key, 0, sizeof(ecc_key));
  21740. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21741. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21742. XMEMSET(out, 0, keySz);
  21743. ExpectIntEQ(wc_ecc_init(&key), 0);
  21744. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21745. ExpectIntEQ(wc_InitRng(&rng), 0);
  21746. #if !defined(NO_ECC256)
  21747. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21748. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21749. #else
  21750. ret = wc_ecc_make_key(&rng, keySz, &key);
  21751. #if defined(WOLFSSL_ASYNC_CRYPT)
  21752. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21753. #endif
  21754. ExpectIntEQ(ret, 0);
  21755. ret = wc_ecc_make_key(&rng, keySz, &key);
  21756. #if defined(WOLFSSL_ASYNC_CRYPT)
  21757. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21758. #endif
  21759. ExpectIntEQ(ret, 0);
  21760. #endif
  21761. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21762. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21763. !defined(HAVE_SELFTEST)
  21764. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21765. #endif
  21766. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21767. #if !defined(NO_ECC256)
  21768. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21769. #endif
  21770. /* Test bad args. */
  21771. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21772. BAD_FUNC_ARG);
  21773. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21774. BAD_FUNC_ARG);
  21775. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21776. BAD_FUNC_ARG);
  21777. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21778. BAD_FUNC_ARG);
  21779. /* Invalid length */
  21780. outlen = 1;
  21781. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21782. BUFFER_E);
  21783. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21784. wc_ecc_free(&pubKey);
  21785. wc_ecc_free(&key);
  21786. #ifdef FP_ECC
  21787. wc_ecc_fp_free();
  21788. #endif
  21789. PRIVATE_KEY_LOCK();
  21790. #endif
  21791. return EXPECT_RESULT();
  21792. } /* END tests_wc_ecc_shared_secret */
  21793. /*
  21794. * testint wc_ecc_export_x963()
  21795. */
  21796. static int test_wc_ecc_export_x963(void)
  21797. {
  21798. EXPECT_DECLS;
  21799. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21800. ecc_key key;
  21801. WC_RNG rng;
  21802. byte out[ECC_ASN963_MAX_BUF_SZ];
  21803. word32 outlen = sizeof(out);
  21804. int ret;
  21805. PRIVATE_KEY_UNLOCK();
  21806. /* Initialize variables. */
  21807. XMEMSET(&key, 0, sizeof(ecc_key));
  21808. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21809. XMEMSET(out, 0, outlen);
  21810. ExpectIntEQ(wc_ecc_init(&key), 0);
  21811. ExpectIntEQ(wc_InitRng(&rng), 0);
  21812. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21813. #if defined(WOLFSSL_ASYNC_CRYPT)
  21814. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21815. #endif
  21816. ExpectIntEQ(ret, 0);
  21817. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21818. /* Test bad args. */
  21819. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21820. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21821. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21822. key.idx = -4;
  21823. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21824. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21825. wc_ecc_free(&key);
  21826. #ifdef FP_ECC
  21827. wc_ecc_fp_free();
  21828. #endif
  21829. PRIVATE_KEY_LOCK();
  21830. #endif
  21831. return EXPECT_RESULT();
  21832. } /* END test_wc_ecc_export_x963 */
  21833. /*
  21834. * Testing wc_ecc_export_x963_ex()
  21835. * compile with --enable-compkey will use compression.
  21836. */
  21837. static int test_wc_ecc_export_x963_ex(void)
  21838. {
  21839. EXPECT_DECLS;
  21840. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21841. ecc_key key;
  21842. WC_RNG rng;
  21843. int ret;
  21844. byte out[ECC_ASN963_MAX_BUF_SZ];
  21845. word32 outlen = sizeof(out);
  21846. #ifdef HAVE_COMP_KEY
  21847. word32 badOutLen = 5;
  21848. #endif
  21849. /* Init stack variables. */
  21850. XMEMSET(&key, 0, sizeof(ecc_key));
  21851. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21852. XMEMSET(out, 0, outlen);
  21853. PRIVATE_KEY_UNLOCK();
  21854. ExpectIntEQ(wc_ecc_init(&key), 0);
  21855. ExpectIntEQ(wc_InitRng(&rng), 0);
  21856. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21857. #if defined(WOLFSSL_ASYNC_CRYPT)
  21858. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21859. #endif
  21860. ExpectIntEQ(ret, 0);
  21861. #ifdef HAVE_COMP_KEY
  21862. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21863. #else
  21864. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21865. #endif
  21866. /* Test bad args. */
  21867. #ifdef HAVE_COMP_KEY
  21868. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21869. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21870. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21871. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21872. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21873. #else
  21874. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21875. LENGTH_ONLY_E);
  21876. #endif
  21877. key.idx = -4;
  21878. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21879. #else
  21880. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21881. ECC_BAD_ARG_E);
  21882. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21883. LENGTH_ONLY_E);
  21884. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21885. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21886. ECC_BAD_ARG_E);
  21887. key.idx = -4;
  21888. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21889. ECC_BAD_ARG_E);
  21890. #endif
  21891. PRIVATE_KEY_LOCK();
  21892. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21893. wc_ecc_free(&key);
  21894. #ifdef FP_ECC
  21895. wc_ecc_fp_free();
  21896. #endif
  21897. #endif
  21898. return EXPECT_RESULT();
  21899. } /* END test_wc_ecc_export_x963_ex */
  21900. /*
  21901. * testing wc_ecc_import_x963()
  21902. */
  21903. static int test_wc_ecc_import_x963(void)
  21904. {
  21905. EXPECT_DECLS;
  21906. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21907. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21908. ecc_key pubKey;
  21909. ecc_key key;
  21910. WC_RNG rng;
  21911. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21912. word32 x963Len = (word32)sizeof(x963);
  21913. int ret;
  21914. /* Init stack variables. */
  21915. XMEMSET(&key, 0, sizeof(ecc_key));
  21916. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21917. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21918. XMEMSET(x963, 0, x963Len);
  21919. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21920. ExpectIntEQ(wc_ecc_init(&key), 0);
  21921. ExpectIntEQ(wc_InitRng(&rng), 0);
  21922. #if FIPS_VERSION3_GE(6,0,0)
  21923. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21924. #else
  21925. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21926. #endif
  21927. #if defined(WOLFSSL_ASYNC_CRYPT)
  21928. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21929. #endif
  21930. ExpectIntEQ(ret, 0);
  21931. PRIVATE_KEY_UNLOCK();
  21932. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21933. PRIVATE_KEY_LOCK();
  21934. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21935. /* Test bad args. */
  21936. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21937. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21938. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21939. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21940. wc_ecc_free(&key);
  21941. wc_ecc_free(&pubKey);
  21942. #ifdef FP_ECC
  21943. wc_ecc_fp_free();
  21944. #endif
  21945. #endif
  21946. return EXPECT_RESULT();
  21947. } /* END wc_ecc_import_x963 */
  21948. /*
  21949. * testing wc_ecc_import_private_key()
  21950. */
  21951. static int test_wc_ecc_import_private_key(void)
  21952. {
  21953. EXPECT_DECLS;
  21954. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21955. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21956. ecc_key key;
  21957. ecc_key keyImp;
  21958. WC_RNG rng;
  21959. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21960. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21961. word32 privKeySz = (word32)sizeof(privKey);
  21962. word32 x963KeySz = (word32)sizeof(x963Key);
  21963. int ret;
  21964. /* Init stack variables. */
  21965. XMEMSET(&key, 0, sizeof(ecc_key));
  21966. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21967. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21968. XMEMSET(privKey, 0, privKeySz);
  21969. XMEMSET(x963Key, 0, x963KeySz);
  21970. PRIVATE_KEY_UNLOCK();
  21971. ExpectIntEQ(wc_ecc_init(&key), 0);
  21972. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21973. ExpectIntEQ(wc_InitRng(&rng), 0);
  21974. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21975. #if defined(WOLFSSL_ASYNC_CRYPT)
  21976. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21977. #endif
  21978. ExpectIntEQ(ret, 0);
  21979. PRIVATE_KEY_UNLOCK();
  21980. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21981. PRIVATE_KEY_LOCK();
  21982. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21983. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21984. x963KeySz, &keyImp), 0);
  21985. /* Pass in bad args. */
  21986. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21987. x963KeySz, NULL), BAD_FUNC_ARG);
  21988. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21989. &keyImp), BAD_FUNC_ARG);
  21990. PRIVATE_KEY_LOCK();
  21991. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21992. wc_ecc_free(&keyImp);
  21993. wc_ecc_free(&key);
  21994. #ifdef FP_ECC
  21995. wc_ecc_fp_free();
  21996. #endif
  21997. #endif
  21998. return EXPECT_RESULT();
  21999. } /* END test_wc_ecc_import_private_key */
  22000. /*
  22001. * Testing wc_ecc_export_private_only()
  22002. */
  22003. static int test_wc_ecc_export_private_only(void)
  22004. {
  22005. EXPECT_DECLS;
  22006. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22007. ecc_key key;
  22008. WC_RNG rng;
  22009. byte out[ECC_PRIV_KEY_BUF];
  22010. word32 outlen = sizeof(out);
  22011. int ret;
  22012. /* Init stack variables. */
  22013. XMEMSET(&key, 0, sizeof(ecc_key));
  22014. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22015. XMEMSET(out, 0, outlen);
  22016. PRIVATE_KEY_UNLOCK();
  22017. ExpectIntEQ(wc_ecc_init(&key), 0);
  22018. ExpectIntEQ(wc_InitRng(&rng), 0);
  22019. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22020. #if defined(WOLFSSL_ASYNC_CRYPT)
  22021. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22022. #endif
  22023. ExpectIntEQ(ret, 0);
  22024. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  22025. /* Pass in bad args. */
  22026. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  22027. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  22028. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  22029. PRIVATE_KEY_LOCK();
  22030. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22031. wc_ecc_free(&key);
  22032. #ifdef FP_ECC
  22033. wc_ecc_fp_free();
  22034. #endif
  22035. #endif
  22036. return EXPECT_RESULT();
  22037. } /* END test_wc_ecc_export_private_only */
  22038. /*
  22039. * Testing wc_ecc_rs_to_sig()
  22040. */
  22041. static int test_wc_ecc_rs_to_sig(void)
  22042. {
  22043. EXPECT_DECLS;
  22044. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22045. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22046. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22047. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22048. const char* zeroStr = "0";
  22049. byte sig[ECC_MAX_SIG_SIZE];
  22050. word32 siglen = (word32)sizeof(sig);
  22051. /* R and S max size is the order of curve. 2^192.*/
  22052. int keySz = KEY24;
  22053. byte r[KEY24];
  22054. byte s[KEY24];
  22055. word32 rlen = (word32)sizeof(r);
  22056. word32 slen = (word32)sizeof(s);
  22057. /* Init stack variables. */
  22058. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22059. XMEMSET(r, 0, keySz);
  22060. XMEMSET(s, 0, keySz);
  22061. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  22062. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  22063. /* Test bad args. */
  22064. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  22065. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  22066. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  22067. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  22068. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  22069. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  22070. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  22071. ECC_BAD_ARG_E);
  22072. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  22073. ECC_BAD_ARG_E);
  22074. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  22075. ECC_BAD_ARG_E);
  22076. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  22077. ECC_BAD_ARG_E);
  22078. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  22079. ECC_BAD_ARG_E);
  22080. #endif
  22081. return EXPECT_RESULT();
  22082. } /* END test_wc_ecc_rs_to_sig */
  22083. static int test_wc_ecc_import_raw(void)
  22084. {
  22085. EXPECT_DECLS;
  22086. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22087. ecc_key key;
  22088. const char* qx =
  22089. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22090. const char* qy =
  22091. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22092. const char* d =
  22093. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22094. const char* curveName = "SECP256R1";
  22095. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22096. const char* kNullStr = "";
  22097. int ret;
  22098. #endif
  22099. XMEMSET(&key, 0, sizeof(ecc_key));
  22100. ExpectIntEQ(wc_ecc_init(&key), 0);
  22101. /* Test good import */
  22102. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  22103. /* Test bad args. */
  22104. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  22105. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  22106. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  22107. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  22108. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22109. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22110. wc_ecc_free(&key);
  22111. #endif
  22112. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  22113. curveName), 0);
  22114. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22115. #endif
  22116. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22117. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22118. wc_ecc_free(&key);
  22119. #endif
  22120. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22121. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22122. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22123. #else
  22124. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22125. #endif
  22126. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22127. wc_ecc_free(&key);
  22128. #endif
  22129. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22130. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22131. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22132. #else
  22133. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22134. #endif
  22135. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22136. wc_ecc_free(&key);
  22137. #endif
  22138. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22139. #endif
  22140. wc_ecc_free(&key);
  22141. #endif
  22142. return EXPECT_RESULT();
  22143. } /* END test_wc_ecc_import_raw */
  22144. static int test_wc_ecc_import_unsigned(void)
  22145. {
  22146. EXPECT_DECLS;
  22147. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22148. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22149. HAVE_FIPS_VERSION >= 2))
  22150. ecc_key key;
  22151. const byte qx[] = {
  22152. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22153. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22154. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22155. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22156. };
  22157. const byte qy[] = {
  22158. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22159. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22160. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22161. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22162. };
  22163. const byte d[] = {
  22164. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22165. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22166. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22167. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22168. };
  22169. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22170. const byte nullBytes[32] = {0};
  22171. int ret;
  22172. #endif
  22173. int curveId = ECC_SECP256R1;
  22174. XMEMSET(&key, 0, sizeof(ecc_key));
  22175. ExpectIntEQ(wc_ecc_init(&key), 0);
  22176. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22177. curveId), 0);
  22178. /* Test bad args. */
  22179. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22180. curveId), BAD_FUNC_ARG);
  22181. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22182. curveId), BAD_FUNC_ARG);
  22183. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22184. curveId), BAD_FUNC_ARG);
  22185. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22186. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22187. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22188. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22189. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22190. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22191. #endif
  22192. wc_ecc_free(&key);
  22193. #endif
  22194. return EXPECT_RESULT();
  22195. } /* END test_wc_ecc_import_unsigned */
  22196. /*
  22197. * Testing wc_ecc_sig_size()
  22198. */
  22199. static int test_wc_ecc_sig_size(void)
  22200. {
  22201. EXPECT_DECLS;
  22202. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22203. ecc_key key;
  22204. WC_RNG rng;
  22205. int keySz = KEY16;
  22206. int ret;
  22207. XMEMSET(&rng, 0, sizeof(rng));
  22208. XMEMSET(&key, 0, sizeof(key));
  22209. ExpectIntEQ(wc_ecc_init(&key), 0);
  22210. ExpectIntEQ(wc_InitRng(&rng), 0);
  22211. ret = wc_ecc_make_key(&rng, keySz, &key);
  22212. #if defined(WOLFSSL_ASYNC_CRYPT)
  22213. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22214. #endif
  22215. ExpectIntEQ(ret, 0);
  22216. ExpectIntLE(wc_ecc_sig_size(&key),
  22217. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22218. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22219. wc_ecc_free(&key);
  22220. #endif
  22221. return EXPECT_RESULT();
  22222. } /* END test_wc_ecc_sig_size */
  22223. /*
  22224. * Testing wc_ecc_ctx_new()
  22225. */
  22226. static int test_wc_ecc_ctx_new(void)
  22227. {
  22228. EXPECT_DECLS;
  22229. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22230. WC_RNG rng;
  22231. ecEncCtx* cli = NULL;
  22232. ecEncCtx* srv = NULL;
  22233. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22234. ExpectIntEQ(wc_InitRng(&rng), 0);
  22235. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22236. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22237. wc_ecc_ctx_free(cli);
  22238. cli = NULL;
  22239. wc_ecc_ctx_free(srv);
  22240. /* Test bad args. */
  22241. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22242. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22243. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22244. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22245. wc_ecc_ctx_free(cli);
  22246. #endif
  22247. return EXPECT_RESULT();
  22248. } /* END test_wc_ecc_ctx_new */
  22249. /*
  22250. * Tesing wc_ecc_reset()
  22251. */
  22252. static int test_wc_ecc_ctx_reset(void)
  22253. {
  22254. EXPECT_DECLS;
  22255. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22256. ecEncCtx* ctx = NULL;
  22257. WC_RNG rng;
  22258. XMEMSET(&rng, 0, sizeof(rng));
  22259. ExpectIntEQ(wc_InitRng(&rng), 0);
  22260. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22261. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22262. /* Pass in bad args. */
  22263. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22264. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22265. wc_ecc_ctx_free(ctx);
  22266. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22267. #endif
  22268. return EXPECT_RESULT();
  22269. } /* END test_wc_ecc_ctx_reset */
  22270. /*
  22271. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22272. */
  22273. static int test_wc_ecc_ctx_set_peer_salt(void)
  22274. {
  22275. EXPECT_DECLS;
  22276. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22277. WC_RNG rng;
  22278. ecEncCtx* cliCtx = NULL;
  22279. ecEncCtx* servCtx = NULL;
  22280. const byte* cliSalt = NULL;
  22281. const byte* servSalt = NULL;
  22282. XMEMSET(&rng, 0, sizeof(rng));
  22283. ExpectIntEQ(wc_InitRng(&rng), 0);
  22284. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22285. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22286. /* Test bad args. */
  22287. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22288. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22289. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22290. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22291. /* Test bad args. */
  22292. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22293. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22294. wc_ecc_ctx_free(cliCtx);
  22295. wc_ecc_ctx_free(servCtx);
  22296. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22297. #endif
  22298. return EXPECT_RESULT();
  22299. } /* END test_wc_ecc_ctx_set_peer_salt */
  22300. /*
  22301. * Testing wc_ecc_ctx_set_info()
  22302. */
  22303. static int test_wc_ecc_ctx_set_info(void)
  22304. {
  22305. EXPECT_DECLS;
  22306. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22307. ecEncCtx* ctx = NULL;
  22308. WC_RNG rng;
  22309. const char* optInfo = "Optional Test Info.";
  22310. int optInfoSz = (int)XSTRLEN(optInfo);
  22311. const char* badOptInfo = NULL;
  22312. XMEMSET(&rng, 0, sizeof(rng));
  22313. ExpectIntEQ(wc_InitRng(&rng), 0);
  22314. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22315. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22316. /* Test bad args. */
  22317. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22318. BAD_FUNC_ARG);
  22319. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22320. BAD_FUNC_ARG);
  22321. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22322. wc_ecc_ctx_free(ctx);
  22323. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22324. #endif
  22325. return EXPECT_RESULT();
  22326. } /* END test_wc_ecc_ctx_set_info */
  22327. /*
  22328. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22329. */
  22330. static int test_wc_ecc_encryptDecrypt(void)
  22331. {
  22332. EXPECT_DECLS;
  22333. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22334. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22335. ecc_key srvKey;
  22336. ecc_key cliKey;
  22337. ecc_key tmpKey;
  22338. WC_RNG rng;
  22339. int ret;
  22340. const char* msg = "EccBlock Size 16";
  22341. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22342. #ifdef WOLFSSL_ECIES_OLD
  22343. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22344. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22345. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22346. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22347. #else
  22348. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22349. WC_SHA256_DIGEST_SIZE];
  22350. #endif
  22351. word32 outSz = (word32)sizeof(out);
  22352. byte plain[sizeof("EccBlock Size 16")];
  22353. word32 plainSz = (word32)sizeof(plain);
  22354. int keySz = KEY20;
  22355. /* Init stack variables. */
  22356. XMEMSET(out, 0, outSz);
  22357. XMEMSET(plain, 0, plainSz);
  22358. XMEMSET(&rng, 0, sizeof(rng));
  22359. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22360. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22361. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22362. ExpectIntEQ(wc_InitRng(&rng), 0);
  22363. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22364. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22365. #if defined(WOLFSSL_ASYNC_CRYPT)
  22366. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22367. #endif
  22368. ExpectIntEQ(ret, 0);
  22369. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22370. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22371. #if defined(WOLFSSL_ASYNC_CRYPT)
  22372. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22373. #endif
  22374. ExpectIntEQ(ret, 0);
  22375. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22376. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22377. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22378. !defined(HAVE_SELFTEST)
  22379. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22380. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22381. #endif
  22382. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22383. &outSz, NULL), 0);
  22384. /* Test bad args. */
  22385. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22386. NULL), BAD_FUNC_ARG);
  22387. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22388. NULL), BAD_FUNC_ARG);
  22389. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22390. NULL), BAD_FUNC_ARG);
  22391. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22392. &outSz, NULL), BAD_FUNC_ARG);
  22393. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22394. NULL), BAD_FUNC_ARG);
  22395. #ifdef WOLFSSL_ECIES_OLD
  22396. tmpKey.dp = cliKey.dp;
  22397. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22398. #endif
  22399. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22400. NULL), 0);
  22401. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22402. NULL), BAD_FUNC_ARG);
  22403. #ifdef WOLFSSL_ECIES_OLD
  22404. /* NULL parameter allowed in new implementations - public key comes from
  22405. * the message. */
  22406. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22407. NULL), BAD_FUNC_ARG);
  22408. #endif
  22409. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22410. NULL), BAD_FUNC_ARG);
  22411. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22412. NULL), BAD_FUNC_ARG);
  22413. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22414. BAD_FUNC_ARG);
  22415. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22416. wc_ecc_free(&tmpKey);
  22417. wc_ecc_free(&srvKey);
  22418. wc_ecc_free(&cliKey);
  22419. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22420. #endif
  22421. return EXPECT_RESULT();
  22422. } /* END test_wc_ecc_encryptDecrypt */
  22423. /*
  22424. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22425. */
  22426. static int test_wc_ecc_del_point(void)
  22427. {
  22428. EXPECT_DECLS;
  22429. #if defined(HAVE_ECC)
  22430. ecc_point* pt = NULL;
  22431. ExpectNotNull(pt = wc_ecc_new_point());
  22432. wc_ecc_del_point(pt);
  22433. #endif
  22434. return EXPECT_RESULT();
  22435. } /* END test_wc_ecc_del_point */
  22436. /*
  22437. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22438. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22439. * and wc_ecc_cmp_point()
  22440. */
  22441. static int test_wc_ecc_pointFns(void)
  22442. {
  22443. EXPECT_DECLS;
  22444. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22445. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22446. !defined(WOLFSSL_ATECC608A)
  22447. ecc_key key;
  22448. WC_RNG rng;
  22449. int ret;
  22450. ecc_point* point = NULL;
  22451. ecc_point* cpypt = NULL;
  22452. int idx = 0;
  22453. int keySz = KEY32;
  22454. byte der[DER_SZ(KEY32)];
  22455. word32 derlenChk = 0;
  22456. word32 derSz = DER_SZ(KEY32);
  22457. /* Init stack variables. */
  22458. XMEMSET(der, 0, derSz);
  22459. XMEMSET(&key, 0, sizeof(ecc_key));
  22460. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22461. ExpectIntEQ(wc_InitRng(&rng), 0);
  22462. ExpectIntEQ(wc_ecc_init(&key), 0);
  22463. ret = wc_ecc_make_key(&rng, keySz, &key);
  22464. #if defined(WOLFSSL_ASYNC_CRYPT)
  22465. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22466. #endif
  22467. ExpectIntEQ(ret, 0);
  22468. ExpectNotNull(point = wc_ecc_new_point());
  22469. ExpectNotNull(cpypt = wc_ecc_new_point());
  22470. /* Export */
  22471. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22472. &derlenChk), LENGTH_ONLY_E);
  22473. /* Check length value. */
  22474. ExpectIntEQ(derSz, derlenChk);
  22475. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22476. &derSz), 0);
  22477. /* Test bad args. */
  22478. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22479. ECC_BAD_ARG_E);
  22480. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22481. ECC_BAD_ARG_E);
  22482. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22483. NULL), ECC_BAD_ARG_E);
  22484. /* Import */
  22485. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22486. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22487. /* Test bad args. */
  22488. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22489. ECC_BAD_ARG_E);
  22490. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22491. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22492. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22493. ECC_BAD_ARG_E);
  22494. /* Copy */
  22495. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22496. /* Test bad args. */
  22497. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22498. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22499. /* Compare point */
  22500. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22501. /* Test bad args. */
  22502. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22503. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22504. /* At infinity if return == 1, otherwise return == 0. */
  22505. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22506. /* Test bad args. */
  22507. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22508. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22509. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22510. #ifdef USE_ECC_B_PARAM
  22511. /* On curve if ret == 0 */
  22512. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22513. /* Test bad args. */
  22514. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22515. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22516. #endif /* USE_ECC_B_PARAM */
  22517. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22518. /* Free */
  22519. wc_ecc_del_point(point);
  22520. wc_ecc_del_point(cpypt);
  22521. wc_ecc_free(&key);
  22522. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22523. #endif
  22524. return EXPECT_RESULT();
  22525. } /* END test_wc_ecc_pointFns */
  22526. /*
  22527. * Testing wc_ecc_shared_secret_ssh()
  22528. */
  22529. static int test_wc_ecc_shared_secret_ssh(void)
  22530. {
  22531. EXPECT_DECLS;
  22532. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22533. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22534. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22535. !defined(WOLFSSL_CRYPTOCELL)
  22536. ecc_key key;
  22537. ecc_key key2;
  22538. WC_RNG rng;
  22539. int ret;
  22540. int keySz = KEY32;
  22541. #if FIPS_VERSION3_GE(6,0,0)
  22542. int key2Sz = KEY28;
  22543. #else
  22544. int key2Sz = KEY24;
  22545. #endif
  22546. byte secret[KEY32];
  22547. word32 secretLen = (word32)keySz;
  22548. /* Init stack variables. */
  22549. XMEMSET(&key, 0, sizeof(ecc_key));
  22550. XMEMSET(&key2, 0, sizeof(ecc_key));
  22551. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22552. XMEMSET(secret, 0, secretLen);
  22553. PRIVATE_KEY_UNLOCK();
  22554. /* Make keys */
  22555. ExpectIntEQ(wc_ecc_init(&key), 0);
  22556. ExpectIntEQ(wc_InitRng(&rng), 0);
  22557. ret = wc_ecc_make_key(&rng, keySz, &key);
  22558. #if defined(WOLFSSL_ASYNC_CRYPT)
  22559. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22560. #endif
  22561. ExpectIntEQ(ret, 0);
  22562. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22563. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22564. ExpectIntEQ(wc_InitRng(&rng), 0);
  22565. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22566. #if defined(WOLFSSL_ASYNC_CRYPT)
  22567. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22568. #endif
  22569. ExpectIntEQ(ret, 0);
  22570. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22571. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22572. !defined(HAVE_SELFTEST)
  22573. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22574. #endif
  22575. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22576. &secretLen), 0);
  22577. /* Pass in bad args. */
  22578. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22579. &secretLen), BAD_FUNC_ARG);
  22580. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22581. BAD_FUNC_ARG);
  22582. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22583. BAD_FUNC_ARG);
  22584. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22585. BAD_FUNC_ARG);
  22586. key.type = ECC_PUBLICKEY;
  22587. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22588. &secretLen), ECC_BAD_ARG_E);
  22589. PRIVATE_KEY_LOCK();
  22590. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22591. wc_ecc_free(&key);
  22592. wc_ecc_free(&key2);
  22593. #ifdef FP_ECC
  22594. wc_ecc_fp_free();
  22595. #endif
  22596. #endif
  22597. return EXPECT_RESULT();
  22598. } /* END test_wc_ecc_shared_secret_ssh */
  22599. /*
  22600. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22601. */
  22602. static int test_wc_ecc_verify_hash_ex(void)
  22603. {
  22604. EXPECT_DECLS;
  22605. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22606. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22607. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22608. ecc_key key;
  22609. WC_RNG rng;
  22610. int ret;
  22611. mp_int r;
  22612. mp_int s;
  22613. mp_int z;
  22614. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22615. unsigned char iHash[] = "Everyone gets Friday off.......";
  22616. unsigned char shortHash[] = TEST_STRING;
  22617. word32 hashlen = sizeof(hash);
  22618. word32 iHashLen = sizeof(iHash);
  22619. word32 shortHashLen = sizeof(shortHash);
  22620. int keySz = KEY32;
  22621. int verify_ok = 0;
  22622. XMEMSET(&key, 0, sizeof(ecc_key));
  22623. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22624. XMEMSET(&r, 0, sizeof(mp_int));
  22625. XMEMSET(&s, 0, sizeof(mp_int));
  22626. XMEMSET(&z, 0, sizeof(mp_int));
  22627. /* Initialize r, s and z. */
  22628. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22629. ExpectIntEQ(wc_ecc_init(&key), 0);
  22630. ExpectIntEQ(wc_InitRng(&rng), 0);
  22631. ret = wc_ecc_make_key(&rng, keySz, &key);
  22632. #if defined(WOLFSSL_ASYNC_CRYPT)
  22633. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22634. #endif
  22635. ExpectIntEQ(ret, 0);
  22636. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22637. /* verify_ok should be 1. */
  22638. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22639. 0);
  22640. ExpectIntEQ(verify_ok, 1);
  22641. /* verify_ok should be 0 */
  22642. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22643. &key), 0);
  22644. ExpectIntEQ(verify_ok, 0);
  22645. /* verify_ok should be 0. */
  22646. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22647. &verify_ok, &key), 0);
  22648. ExpectIntEQ(verify_ok, 0);
  22649. /* Test bad args. */
  22650. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22651. ECC_BAD_ARG_E);
  22652. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22653. ECC_BAD_ARG_E);
  22654. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22655. ECC_BAD_ARG_E);
  22656. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22657. ECC_BAD_ARG_E);
  22658. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22659. ECC_BAD_ARG_E);
  22660. /* Test bad args. */
  22661. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22662. &verify_ok, &key), ECC_BAD_ARG_E);
  22663. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22664. &verify_ok, &key), ECC_BAD_ARG_E);
  22665. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22666. &verify_ok, &key), MP_ZERO_E);
  22667. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22668. &verify_ok, &key), MP_ZERO_E);
  22669. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22670. &verify_ok, &key), MP_ZERO_E);
  22671. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22672. &key), ECC_BAD_ARG_E);
  22673. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22674. &key), ECC_BAD_ARG_E);
  22675. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22676. &verify_ok, NULL), ECC_BAD_ARG_E);
  22677. wc_ecc_free(&key);
  22678. mp_free(&r);
  22679. mp_free(&s);
  22680. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22681. #endif
  22682. return EXPECT_RESULT();
  22683. } /* END test_wc_ecc_verify_hash_ex */
  22684. /*
  22685. * Testing wc_ecc_mulmod()
  22686. */
  22687. static int test_wc_ecc_mulmod(void)
  22688. {
  22689. EXPECT_DECLS;
  22690. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22691. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22692. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22693. ecc_key key1;
  22694. ecc_key key2;
  22695. ecc_key key3;
  22696. WC_RNG rng;
  22697. int ret;
  22698. XMEMSET(&key1, 0, sizeof(ecc_key));
  22699. XMEMSET(&key2, 0, sizeof(ecc_key));
  22700. XMEMSET(&key3, 0, sizeof(ecc_key));
  22701. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22702. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22703. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22704. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22705. ExpectIntEQ(wc_InitRng(&rng), 0);
  22706. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22707. #if defined(WOLFSSL_ASYNC_CRYPT)
  22708. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22709. #endif
  22710. ExpectIntEQ(ret, 0);
  22711. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22712. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22713. key1.dp->Af, ECC_SECP256R1), 0);
  22714. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22715. key1.dp->prime, ECC_SECP256R1), 0);
  22716. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22717. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22718. 1), 0);
  22719. /* Test bad args. */
  22720. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22721. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22722. ECC_BAD_ARG_E);
  22723. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22724. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22725. ECC_BAD_ARG_E);
  22726. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22727. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22728. ECC_BAD_ARG_E);
  22729. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22730. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22731. wc_ecc_free(&key1);
  22732. wc_ecc_free(&key2);
  22733. wc_ecc_free(&key3);
  22734. #ifdef FP_ECC
  22735. wc_ecc_fp_free();
  22736. #endif
  22737. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22738. return EXPECT_RESULT();
  22739. } /* END test_wc_ecc_mulmod */
  22740. /*
  22741. * Testing wc_ecc_is_valid_idx()
  22742. */
  22743. static int test_wc_ecc_is_valid_idx(void)
  22744. {
  22745. EXPECT_DECLS;
  22746. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22747. ecc_key key;
  22748. WC_RNG rng;
  22749. int ret;
  22750. int iVal = -2;
  22751. int iVal2 = 3000;
  22752. XMEMSET(&key, 0, sizeof(ecc_key));
  22753. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22754. ExpectIntEQ(wc_ecc_init(&key), 0);
  22755. ExpectIntEQ(wc_InitRng(&rng), 0);
  22756. ret = wc_ecc_make_key(&rng, 32, &key);
  22757. #if defined(WOLFSSL_ASYNC_CRYPT)
  22758. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22759. #endif
  22760. ExpectIntEQ(ret, 0);
  22761. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22762. /* Test bad args. */
  22763. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22764. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22765. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22766. wc_ecc_free(&key);
  22767. #ifdef FP_ECC
  22768. wc_ecc_fp_free();
  22769. #endif
  22770. #endif
  22771. return EXPECT_RESULT();
  22772. } /* END test_wc_ecc_is_valid_idx */
  22773. /*
  22774. * Testing wc_ecc_get_curve_id_from_oid()
  22775. */
  22776. static int test_wc_ecc_get_curve_id_from_oid(void)
  22777. {
  22778. EXPECT_DECLS;
  22779. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22780. !defined(HAVE_FIPS)
  22781. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22782. word32 len = sizeof(oid);
  22783. /* Bad Cases */
  22784. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22785. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22786. /* Good Case */
  22787. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22788. #endif
  22789. return EXPECT_RESULT();
  22790. } /* END test_wc_ecc_get_curve_id_from_oid */
  22791. /*
  22792. * Testing wc_ecc_sig_size_calc()
  22793. */
  22794. static int test_wc_ecc_sig_size_calc(void)
  22795. {
  22796. EXPECT_DECLS;
  22797. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22798. ecc_key key;
  22799. WC_RNG rng;
  22800. int sz = 0;
  22801. int ret;
  22802. XMEMSET(&key, 0, sizeof(ecc_key));
  22803. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22804. ExpectIntEQ(wc_ecc_init(&key), 0);
  22805. ExpectIntEQ(wc_InitRng(&rng), 0);
  22806. ret = wc_ecc_make_key(&rng, 16, &key);
  22807. #if defined(WOLFSSL_ASYNC_CRYPT)
  22808. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22809. #endif
  22810. #if FIPS_VERSION3_GE(6,0,0)
  22811. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22812. #else
  22813. ExpectIntEQ(ret, 0);
  22814. #endif
  22815. #if FIPS_VERSION3_LT(6,0,0)
  22816. sz = key.dp->size;
  22817. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22818. #else
  22819. (void) sz;
  22820. #endif
  22821. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22822. wc_ecc_free(&key);
  22823. #endif
  22824. return EXPECT_RESULT();
  22825. } /* END test_wc_ecc_sig_size_calc */
  22826. /*
  22827. * Testing wc_ecc_sm2_make_key()
  22828. */
  22829. static int test_wc_ecc_sm2_make_key(void)
  22830. {
  22831. int res = TEST_SKIPPED;
  22832. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22833. EXPECT_DECLS;
  22834. WC_RNG rng[1];
  22835. ecc_key key[1];
  22836. XMEMSET(rng, 0, sizeof(*rng));
  22837. XMEMSET(key, 0, sizeof(*key));
  22838. ExpectIntEQ(wc_InitRng(rng), 0);
  22839. ExpectIntEQ(wc_ecc_init(key), 0);
  22840. /* Test invalid parameters. */
  22841. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22842. BAD_FUNC_ARG);
  22843. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22844. BAD_FUNC_ARG);
  22845. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22846. BAD_FUNC_ARG);
  22847. /* Test valid parameters. */
  22848. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22849. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22850. wc_ecc_free(key);
  22851. wc_FreeRng(rng);
  22852. #ifdef FP_ECC
  22853. wc_ecc_fp_free();
  22854. #endif
  22855. res = EXPECT_RESULT();
  22856. #endif
  22857. return res;
  22858. }
  22859. /*
  22860. * Testing wc_ecc_sm2_shared_secret()
  22861. */
  22862. static int test_wc_ecc_sm2_shared_secret(void)
  22863. {
  22864. int res = TEST_SKIPPED;
  22865. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22866. EXPECT_DECLS;
  22867. WC_RNG rng[1];
  22868. ecc_key keyA[1];
  22869. ecc_key keyB[1];
  22870. byte outA[32];
  22871. byte outB[32];
  22872. word32 outALen = 32;
  22873. word32 outBLen = 32;
  22874. XMEMSET(rng, 0, sizeof(*rng));
  22875. XMEMSET(keyA, 0, sizeof(*keyA));
  22876. XMEMSET(keyB, 0, sizeof(*keyB));
  22877. ExpectIntEQ(wc_InitRng(rng), 0);
  22878. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22879. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22880. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22881. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22882. #ifdef ECC_TIMING_RESISTANT
  22883. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22884. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22885. #endif
  22886. /* Test invalid parameters. */
  22887. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22888. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22889. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22890. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22891. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22892. BAD_FUNC_ARG);
  22893. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22894. BAD_FUNC_ARG);
  22895. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22896. BAD_FUNC_ARG);
  22897. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22898. BAD_FUNC_ARG);
  22899. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22900. /* Test valid parameters. */
  22901. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22902. ExpectIntLE(outALen, 32);
  22903. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22904. ExpectIntLE(outBLen, 32);
  22905. ExpectIntEQ(outALen, outBLen);
  22906. ExpectBufEQ(outA, outB, outALen);
  22907. wc_ecc_free(keyB);
  22908. wc_ecc_free(keyA);
  22909. wc_FreeRng(rng);
  22910. #ifdef FP_ECC
  22911. wc_ecc_fp_free();
  22912. #endif
  22913. res = EXPECT_RESULT();
  22914. #endif
  22915. return res;
  22916. }
  22917. /*
  22918. * Testing wc_ecc_sm2_create_digest()
  22919. */
  22920. static int test_wc_ecc_sm2_create_digest(void)
  22921. {
  22922. int res = TEST_SKIPPED;
  22923. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22924. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22925. EXPECT_DECLS;
  22926. ecc_key key[1];
  22927. enum wc_HashType hashType;
  22928. unsigned char pub[] = {
  22929. 0x04,
  22930. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22931. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22932. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22933. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22934. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22935. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22936. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22937. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22938. };
  22939. unsigned char id[] = {
  22940. 0x01, 0x02, 0x03,
  22941. };
  22942. unsigned char msg[] = {
  22943. 0x01, 0x02, 0x03,
  22944. };
  22945. unsigned char hash[32];
  22946. #ifdef WOLFSSL_SM3
  22947. unsigned char expHash[32] = {
  22948. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22949. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22950. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22951. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22952. };
  22953. #else
  22954. unsigned char expHash[32] = {
  22955. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22956. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22957. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22958. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22959. };
  22960. #endif
  22961. #ifdef WOLFSSL_SM3
  22962. hashType = WC_HASH_TYPE_SM3;
  22963. #else
  22964. hashType = WC_HASH_TYPE_SHA256;
  22965. #endif
  22966. XMEMSET(key, 0, sizeof(*key));
  22967. ExpectIntEQ(wc_ecc_init(key), 0);
  22968. /* Test with no curve set. */
  22969. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22970. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22971. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22972. /* Test invalid parameters. */
  22973. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22974. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22975. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22976. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22977. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22978. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22979. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22980. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22981. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22982. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22983. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22984. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22985. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22986. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22987. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22988. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22989. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22990. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22991. /* Bad hash type. */
  22992. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22993. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22994. -1, hash, 0, key), BAD_FUNC_ARG);
  22995. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22996. /* Bad hash size. */
  22997. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22998. hashType, hash, 0, key), BUFFER_E);
  22999. /* Test valid parameters. */
  23000. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  23001. hashType, hash, sizeof(hash), key), 0);
  23002. ExpectBufEQ(hash, expHash, sizeof(expHash));
  23003. wc_ecc_free(key);
  23004. res = EXPECT_RESULT();
  23005. #endif
  23006. return res;
  23007. }
  23008. /*
  23009. * Testing wc_ecc_sm2_verify_hash_ex()
  23010. */
  23011. static int test_wc_ecc_sm2_verify_hash_ex(void)
  23012. {
  23013. int res = TEST_SKIPPED;
  23014. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  23015. defined(WOLFSSL_PUBLIC_MP)
  23016. EXPECT_DECLS;
  23017. ecc_key key[1];
  23018. mp_int r[1];
  23019. mp_int s[1];
  23020. int verified;
  23021. unsigned char pub[] = {
  23022. 0x04,
  23023. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23024. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23025. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23026. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23027. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23028. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23029. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23030. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23031. };
  23032. unsigned char hash[] = {
  23033. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23034. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23035. 0x50, 0x69, 0x5B, 0x20
  23036. };
  23037. unsigned char rData[] = {
  23038. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23039. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23040. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23041. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  23042. };
  23043. unsigned char sData[] = {
  23044. 0x1D,
  23045. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23046. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23047. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23048. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23049. };
  23050. unsigned char rBadData[] = {
  23051. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  23052. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  23053. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  23054. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  23055. };
  23056. XMEMSET(key, 0, sizeof(*key));
  23057. XMEMSET(r, 0, sizeof(*r));
  23058. XMEMSET(s, 0, sizeof(*s));
  23059. ExpectIntEQ(mp_init(r), 0);
  23060. ExpectIntEQ(mp_init(s), 0);
  23061. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  23062. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  23063. ExpectIntEQ(wc_ecc_init(key), 0);
  23064. /* Test with no curve set. */
  23065. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23066. &verified, key), BAD_FUNC_ARG);
  23067. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23068. /* Test invalid parameters. */
  23069. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23070. NULL, NULL), BAD_FUNC_ARG);
  23071. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  23072. NULL, NULL), BAD_FUNC_ARG);
  23073. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  23074. NULL, NULL), BAD_FUNC_ARG);
  23075. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  23076. NULL, NULL), BAD_FUNC_ARG);
  23077. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23078. &verified, NULL), BAD_FUNC_ARG);
  23079. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  23080. NULL, key), BAD_FUNC_ARG);
  23081. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  23082. &verified, key), BAD_FUNC_ARG);
  23083. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  23084. &verified, key), BAD_FUNC_ARG);
  23085. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  23086. &verified, key), BAD_FUNC_ARG);
  23087. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23088. NULL, key), BAD_FUNC_ARG);
  23089. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23090. &verified, NULL), BAD_FUNC_ARG);
  23091. /* Make key not on the SM2 curve. */
  23092. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23093. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23094. &verified, key), BAD_FUNC_ARG);
  23095. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23096. /* Test valid parameters. */
  23097. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23098. &verified, key), 0);
  23099. ExpectIntEQ(verified, 1);
  23100. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  23101. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  23102. &verified, key), 0);
  23103. ExpectIntEQ(verified, 0);
  23104. mp_free(s);
  23105. mp_free(r);
  23106. wc_ecc_free(key);
  23107. #ifdef FP_ECC
  23108. wc_ecc_fp_free();
  23109. #endif
  23110. res = EXPECT_RESULT();
  23111. #endif
  23112. return res;
  23113. }
  23114. /*
  23115. * Testing wc_ecc_sm2_verify_hash()
  23116. */
  23117. static int test_wc_ecc_sm2_verify_hash(void)
  23118. {
  23119. int res = TEST_SKIPPED;
  23120. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  23121. EXPECT_DECLS;
  23122. ecc_key key[1];
  23123. int verified;
  23124. unsigned char pub[] = {
  23125. 0x04,
  23126. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23127. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23128. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23129. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23130. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23131. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23132. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23133. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23134. };
  23135. unsigned char hash[] = {
  23136. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23137. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23138. 0x50, 0x69, 0x5B, 0x20
  23139. };
  23140. unsigned char sig[] = {
  23141. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23142. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23143. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23144. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23145. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23146. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23147. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23148. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23149. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23150. };
  23151. unsigned char sigBad[] = {
  23152. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23153. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23154. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23155. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23156. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23157. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23158. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23159. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23160. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23161. };
  23162. XMEMSET(key, 0, sizeof(*key));
  23163. ExpectIntEQ(wc_ecc_init(key), 0);
  23164. /* Test with no curve set. */
  23165. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23166. &verified, key), BAD_FUNC_ARG);
  23167. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23168. /* Test invalid parameters. */
  23169. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23170. NULL, NULL), BAD_FUNC_ARG);
  23171. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23172. NULL, NULL), BAD_FUNC_ARG);
  23173. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23174. NULL, NULL), BAD_FUNC_ARG);
  23175. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23176. &verified, NULL), BAD_FUNC_ARG);
  23177. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23178. NULL, key), BAD_FUNC_ARG);
  23179. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23180. &verified, key), BAD_FUNC_ARG);
  23181. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23182. &verified, key), BAD_FUNC_ARG);
  23183. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23184. NULL, key), BAD_FUNC_ARG);
  23185. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23186. &verified, NULL), BAD_FUNC_ARG);
  23187. /* Make key not on the SM2 curve. */
  23188. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23189. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23190. &verified, key), BAD_FUNC_ARG);
  23191. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23192. /* Test valid parameters. */
  23193. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23194. &verified, key), 0);
  23195. ExpectIntEQ(verified, 1);
  23196. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23197. sizeof(hash), &verified, key), 0);
  23198. ExpectIntEQ(verified, 0);
  23199. wc_ecc_free(key);
  23200. #ifdef FP_ECC
  23201. wc_ecc_fp_free();
  23202. #endif
  23203. res = EXPECT_RESULT();
  23204. #endif
  23205. return res;
  23206. }
  23207. /*
  23208. * Testing wc_ecc_sm2_verify_hash_ex()
  23209. */
  23210. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23211. {
  23212. int res = TEST_SKIPPED;
  23213. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23214. defined(WOLFSSL_PUBLIC_MP)
  23215. EXPECT_DECLS;
  23216. WC_RNG rng[1];
  23217. ecc_key key[1];
  23218. mp_int r[1];
  23219. mp_int s[1];
  23220. unsigned char hash[32];
  23221. #ifdef HAVE_ECC_VERIFY
  23222. int verified;
  23223. #endif
  23224. XMEMSET(rng, 0, sizeof(*rng));
  23225. XMEMSET(key, 0, sizeof(*key));
  23226. XMEMSET(r, 0, sizeof(*r));
  23227. XMEMSET(s, 0, sizeof(*s));
  23228. ExpectIntEQ(wc_InitRng(rng), 0);
  23229. ExpectIntEQ(mp_init(r), 0);
  23230. ExpectIntEQ(mp_init(s), 0);
  23231. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23232. ExpectIntEQ(wc_ecc_init(key), 0);
  23233. /* Test with no curve set. */
  23234. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23235. BAD_FUNC_ARG);
  23236. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23237. /* Test invalid parameters. */
  23238. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23239. NULL), BAD_FUNC_ARG);
  23240. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23241. NULL), BAD_FUNC_ARG);
  23242. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23243. NULL), BAD_FUNC_ARG);
  23244. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23245. NULL), BAD_FUNC_ARG);
  23246. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23247. NULL), BAD_FUNC_ARG);
  23248. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23249. s), BAD_FUNC_ARG);
  23250. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23251. BAD_FUNC_ARG);
  23252. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23253. BAD_FUNC_ARG);
  23254. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23255. BAD_FUNC_ARG);
  23256. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23257. BAD_FUNC_ARG);
  23258. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23259. BAD_FUNC_ARG);
  23260. /* Make key not on the SM2 curve. */
  23261. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23262. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23263. BAD_FUNC_ARG);
  23264. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23265. #ifdef WOLFSSL_SP_MATH_ALL
  23266. {
  23267. mp_int smallR[1];
  23268. sp_init_size(smallR, 1);
  23269. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23270. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23271. smallR, s), 0);
  23272. }
  23273. #endif
  23274. /* Test valid parameters. */
  23275. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23276. 0);
  23277. #ifdef HAVE_ECC_VERIFY
  23278. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23279. key), 0);
  23280. ExpectIntEQ(verified, 1);
  23281. #endif
  23282. mp_free(s);
  23283. mp_free(r);
  23284. wc_ecc_free(key);
  23285. wc_FreeRng(rng);
  23286. #ifdef FP_ECC
  23287. wc_ecc_fp_free();
  23288. #endif
  23289. res = EXPECT_RESULT();
  23290. #endif
  23291. return res;
  23292. }
  23293. /*
  23294. * Testing wc_ecc_sm2_verify_hash()
  23295. */
  23296. static int test_wc_ecc_sm2_sign_hash(void)
  23297. {
  23298. int res = TEST_SKIPPED;
  23299. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23300. EXPECT_DECLS;
  23301. WC_RNG rng[1];
  23302. ecc_key key[1];
  23303. unsigned char hash[32];
  23304. unsigned char sig[72];
  23305. word32 sigSz = sizeof(sig);
  23306. #ifdef HAVE_ECC_VERIFY
  23307. int verified;
  23308. #endif
  23309. XMEMSET(rng, 0, sizeof(*rng));
  23310. XMEMSET(key, 0, sizeof(*key));
  23311. ExpectIntEQ(wc_InitRng(rng), 0);
  23312. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23313. ExpectIntEQ(wc_ecc_init(key), 0);
  23314. /* Test with no curve set. */
  23315. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23316. BAD_FUNC_ARG);
  23317. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23318. /* Test invalid parameters. */
  23319. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23320. NULL), BAD_FUNC_ARG);
  23321. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23322. NULL), BAD_FUNC_ARG);
  23323. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23324. NULL), BAD_FUNC_ARG);
  23325. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23326. NULL), BAD_FUNC_ARG);
  23327. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23328. NULL), BAD_FUNC_ARG);
  23329. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23330. key), BAD_FUNC_ARG);
  23331. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23332. key), BAD_FUNC_ARG);
  23333. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23334. key), BAD_FUNC_ARG);
  23335. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23336. key), BAD_FUNC_ARG);
  23337. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23338. key), BAD_FUNC_ARG);
  23339. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23340. NULL), BAD_FUNC_ARG);
  23341. /* Make key not on the SM2 curve. */
  23342. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23343. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23344. BAD_FUNC_ARG);
  23345. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23346. /* Test valid parameters. */
  23347. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23348. 0);
  23349. #ifdef HAVE_ECC_VERIFY
  23350. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23351. &verified, key), 0);
  23352. ExpectIntEQ(verified, 1);
  23353. #endif
  23354. wc_ecc_free(key);
  23355. wc_FreeRng(rng);
  23356. #ifdef FP_ECC
  23357. wc_ecc_fp_free();
  23358. #endif
  23359. res = EXPECT_RESULT();
  23360. #endif
  23361. return res;
  23362. }
  23363. /*
  23364. * Testing ToTraditional
  23365. */
  23366. static int test_ToTraditional(void)
  23367. {
  23368. EXPECT_DECLS;
  23369. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23370. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23371. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23372. XFILE f = XBADFILE;
  23373. byte input[TWOK_BUF];
  23374. word32 sz = 0;
  23375. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23376. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23377. if (f != XBADFILE)
  23378. XFCLOSE(f);
  23379. /* Good case */
  23380. ExpectIntGT(ToTraditional(input, sz), 0);
  23381. /* Bad cases */
  23382. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23383. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23384. #ifdef WOLFSSL_ASN_TEMPLATE
  23385. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23386. #else
  23387. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23388. #endif
  23389. #endif
  23390. return EXPECT_RESULT();
  23391. } /* End test_ToTraditional*/
  23392. /*
  23393. * Testing wc_EccPrivateKeyToDer
  23394. */
  23395. static int test_wc_EccPrivateKeyToDer(void)
  23396. {
  23397. EXPECT_DECLS;
  23398. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23399. byte output[ONEK_BUF];
  23400. ecc_key eccKey;
  23401. WC_RNG rng;
  23402. word32 inLen;
  23403. word32 outLen = 0;
  23404. int ret;
  23405. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23406. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23407. PRIVATE_KEY_UNLOCK();
  23408. ExpectIntEQ(wc_InitRng(&rng), 0);
  23409. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23410. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23411. #if defined(WOLFSSL_ASYNC_CRYPT)
  23412. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23413. #endif
  23414. ExpectIntEQ(ret, 0);
  23415. inLen = (word32)sizeof(output);
  23416. /* Bad Cases */
  23417. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23418. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23419. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23420. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23421. /* Good Case */
  23422. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23423. wc_ecc_free(&eccKey);
  23424. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23425. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23426. {
  23427. /* test importing private only into a PKEY struct */
  23428. EC_KEY* ec = NULL;
  23429. EVP_PKEY* pkey = NULL;
  23430. const unsigned char* der;
  23431. der = output;
  23432. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23433. der = output;
  23434. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23435. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23436. if (EXPECT_FAIL()) {
  23437. EC_KEY_free(ec);
  23438. }
  23439. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23440. }
  23441. #endif
  23442. PRIVATE_KEY_LOCK();
  23443. #endif
  23444. return EXPECT_RESULT();
  23445. } /* End test_wc_EccPrivateKeyToDer*/
  23446. /*
  23447. * Testing wc_DhPublicKeyDecode
  23448. */
  23449. static int test_wc_DhPublicKeyDecode(void)
  23450. {
  23451. EXPECT_DECLS;
  23452. #ifndef NO_DH
  23453. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23454. DhKey key;
  23455. word32 inOutIdx;
  23456. XMEMSET(&key, 0, sizeof(DhKey));
  23457. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23458. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23459. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23460. BAD_FUNC_ARG);
  23461. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23462. BAD_FUNC_ARG);
  23463. inOutIdx = 0;
  23464. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23465. BAD_FUNC_ARG);
  23466. inOutIdx = 0;
  23467. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23468. BAD_FUNC_ARG);
  23469. inOutIdx = 0;
  23470. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23471. sizeof_dh_pub_key_der_2048), 0);
  23472. ExpectIntNE(key.p.used, 0);
  23473. ExpectIntNE(key.g.used, 0);
  23474. ExpectIntEQ(key.q.used, 0);
  23475. ExpectIntNE(key.pub.used, 0);
  23476. ExpectIntEQ(key.priv.used, 0);
  23477. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23478. #endif
  23479. #endif /* !NO_DH */
  23480. return EXPECT_RESULT();
  23481. }
  23482. /*
  23483. * Testing wc_Ed25519KeyToDer
  23484. */
  23485. static int test_wc_Ed25519KeyToDer(void)
  23486. {
  23487. EXPECT_DECLS;
  23488. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23489. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23490. byte output[ONEK_BUF];
  23491. ed25519_key ed25519Key;
  23492. WC_RNG rng;
  23493. word32 inLen;
  23494. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23495. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23496. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23497. ExpectIntEQ(wc_InitRng(&rng), 0);
  23498. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23499. inLen = (word32)sizeof(output);
  23500. /* Bad Cases */
  23501. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23502. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23503. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23504. /* Good Cases */
  23505. /* length only */
  23506. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23507. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23508. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23509. wc_ed25519_free(&ed25519Key);
  23510. #endif
  23511. return EXPECT_RESULT();
  23512. } /* End test_wc_Ed25519KeyToDer*/
  23513. /*
  23514. * Testing wc_Ed25519PrivateKeyToDer
  23515. */
  23516. static int test_wc_Ed25519PrivateKeyToDer(void)
  23517. {
  23518. EXPECT_DECLS;
  23519. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23520. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23521. byte output[ONEK_BUF];
  23522. ed25519_key ed25519PrivKey;
  23523. WC_RNG rng;
  23524. word32 inLen;
  23525. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23526. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23527. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23528. ExpectIntEQ(wc_InitRng(&rng), 0);
  23529. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23530. 0);
  23531. inLen = (word32)sizeof(output);
  23532. /* Bad Cases */
  23533. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23534. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23535. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23536. BAD_FUNC_ARG);
  23537. /* Good Cases */
  23538. /* length only */
  23539. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23540. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23541. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23542. wc_ed25519_free(&ed25519PrivKey);
  23543. #endif
  23544. return EXPECT_RESULT();
  23545. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23546. /*
  23547. * Testing wc_Ed448KeyToDer
  23548. */
  23549. static int test_wc_Ed448KeyToDer(void)
  23550. {
  23551. EXPECT_DECLS;
  23552. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23553. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23554. byte output[ONEK_BUF];
  23555. ed448_key ed448Key;
  23556. WC_RNG rng;
  23557. word32 inLen;
  23558. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23559. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23560. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23561. ExpectIntEQ(wc_InitRng(&rng), 0);
  23562. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23563. inLen = (word32)sizeof(output);
  23564. /* Bad Cases */
  23565. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23566. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23567. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23568. /* Good Cases */
  23569. /* length only */
  23570. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23571. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23572. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23573. wc_ed448_free(&ed448Key);
  23574. #endif
  23575. return EXPECT_RESULT();
  23576. } /* End test_wc_Ed448KeyToDer*/
  23577. /*
  23578. * Testing wc_Ed448PrivateKeyToDer
  23579. */
  23580. static int test_wc_Ed448PrivateKeyToDer(void)
  23581. {
  23582. EXPECT_DECLS;
  23583. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23584. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23585. byte output[ONEK_BUF];
  23586. ed448_key ed448PrivKey;
  23587. WC_RNG rng;
  23588. word32 inLen;
  23589. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23590. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23591. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23592. ExpectIntEQ(wc_InitRng(&rng), 0);
  23593. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23594. 0);
  23595. inLen = (word32)sizeof(output);
  23596. /* Bad Cases */
  23597. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23598. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23599. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23600. BAD_FUNC_ARG);
  23601. /* Good cases */
  23602. /* length only */
  23603. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23604. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23605. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23606. wc_ed448_free(&ed448PrivKey);
  23607. #endif
  23608. return EXPECT_RESULT();
  23609. } /* End test_wc_Ed448PrivateKeyToDer*/
  23610. /*
  23611. * Testing wc_Curve448PrivateKeyToDer
  23612. */
  23613. static int test_wc_Curve448PrivateKeyToDer(void)
  23614. {
  23615. EXPECT_DECLS;
  23616. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23617. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23618. byte output[ONEK_BUF];
  23619. curve448_key curve448PrivKey;
  23620. WC_RNG rng;
  23621. word32 inLen;
  23622. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23623. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23624. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23625. ExpectIntEQ(wc_InitRng(&rng), 0);
  23626. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23627. 0);
  23628. inLen = (word32)sizeof(output);
  23629. /* Bad Cases */
  23630. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23631. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23632. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23633. BAD_FUNC_ARG);
  23634. /* Good cases */
  23635. /* length only */
  23636. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23637. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23638. /* Bad Cases */
  23639. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  23640. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  23641. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  23642. BUFFER_E);
  23643. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  23644. BUFFER_E);
  23645. /* Good cases */
  23646. /* length only */
  23647. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  23648. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  23649. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  23650. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  23651. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23652. wc_curve448_free(&curve448PrivKey);
  23653. #endif
  23654. return EXPECT_RESULT();
  23655. } /* End wc_Curve448PrivateKeyToDer*/
  23656. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  23657. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && !defined(WOLFSSL_NO_ML_DSA_44)
  23658. static const byte ml_dsa_44_pub_key[] = {
  23659. 0xf1, 0xdf, 0x1e, 0xfc, 0x6b, 0x41, 0xe7,
  23660. 0x5e, 0xcb, 0xb5, 0xb5, 0xd2, 0x3c, 0xc8,
  23661. 0xd3, 0x99, 0x73, 0x36, 0x9a, 0x0b, 0x32,
  23662. 0x71, 0x7a, 0x9f, 0x6d, 0x66, 0x07, 0xb8,
  23663. 0x31, 0x5f, 0x25, 0xb6, 0x2e, 0xee, 0x4f,
  23664. 0x63, 0x13, 0x02, 0x45, 0x3c, 0xd1, 0x3d,
  23665. 0x79, 0x6b, 0x3c, 0xfe, 0xd9, 0x2f, 0x39,
  23666. 0xe8, 0x62, 0x60, 0xf0, 0x04, 0x83, 0x28,
  23667. 0xaa, 0xdc, 0x15, 0x90, 0xef, 0x55, 0x48,
  23668. 0xf9, 0xd2, 0xcd, 0x53, 0x87, 0x0e, 0x42,
  23669. 0xba, 0x16, 0x87, 0x7b, 0x32, 0xa8, 0xbf,
  23670. 0xed, 0x32, 0xa1, 0x19, 0x66, 0x44, 0xfe,
  23671. 0x57, 0xec, 0x26, 0xed, 0x9e, 0x73, 0xa0,
  23672. 0x87, 0xe8, 0x8a, 0x93, 0x3c, 0xec, 0x1d,
  23673. 0xa0, 0xcc, 0x2e, 0x0d, 0x37, 0x5b, 0xb1,
  23674. 0x74, 0x77, 0x18, 0x4b, 0xde, 0x4b, 0xc9,
  23675. 0xac, 0xf8, 0xda, 0x23, 0x7a, 0x2a, 0x39,
  23676. 0xfa, 0x96, 0x01, 0xff, 0xf0, 0xc7, 0xa7,
  23677. 0x34, 0xca, 0x9d, 0xe9, 0xda, 0x4d, 0x85,
  23678. 0x00, 0xc9, 0xe9, 0xcf, 0xb0, 0x3e, 0x21,
  23679. 0xe6, 0xae, 0x52, 0x67, 0x4f, 0xe3, 0x93,
  23680. 0x2f, 0x50, 0x47, 0xdd, 0x89, 0xa2, 0x48,
  23681. 0xf8, 0xfe, 0x93, 0xfe, 0xce, 0x68, 0x9c,
  23682. 0xe9, 0x4d, 0xdd, 0xbd, 0x9f, 0xeb, 0x14,
  23683. 0x8d, 0x38, 0x7a, 0xc6, 0xf2, 0x50, 0x00,
  23684. 0x91, 0x65, 0xd0, 0xd1, 0xeb, 0x51, 0xab,
  23685. 0x3a, 0x0e, 0x45, 0x5c, 0xbd, 0x65, 0xf5,
  23686. 0x78, 0xc6, 0xa0, 0xaa, 0xae, 0x50, 0xf2,
  23687. 0x19, 0x1f, 0x90, 0x1a, 0x9f, 0x34, 0xa0,
  23688. 0xa1, 0x95, 0x94, 0x86, 0x30, 0xc2, 0xb2,
  23689. 0x95, 0x82, 0x13, 0xf6, 0x73, 0xe2, 0x03,
  23690. 0xe3, 0x7c, 0x09, 0x8e, 0x5d, 0x07, 0xd6,
  23691. 0x33, 0x93, 0x8a, 0x1b, 0x67, 0xc9, 0xb1,
  23692. 0x76, 0x74, 0x1c, 0x22, 0x58, 0x05, 0x5a,
  23693. 0xa8, 0x83, 0x68, 0xce, 0x64, 0xfc, 0x52,
  23694. 0x7f, 0x35, 0x80, 0x6e, 0xdf, 0xf5, 0x2d,
  23695. 0xd2, 0xd1, 0x17, 0xdc, 0xce, 0x95, 0xe8,
  23696. 0xe6, 0x42, 0xb1, 0xb1, 0x61, 0xc1, 0x24,
  23697. 0x79, 0x1c, 0x51, 0xfc, 0x3c, 0xba, 0x40,
  23698. 0xf7, 0x70, 0x35, 0x22, 0x73, 0x31, 0x53,
  23699. 0x21, 0xea, 0x09, 0xf7, 0xaa, 0x07, 0xb8,
  23700. 0xfa, 0x0b, 0xa0, 0xa9, 0xb4, 0x8c, 0x83,
  23701. 0xbb, 0x25, 0xfe, 0x39, 0x29, 0xef, 0x34,
  23702. 0xd5, 0xe2, 0xc7, 0x9e, 0x87, 0xbd, 0x50,
  23703. 0x86, 0x71, 0x12, 0x3e, 0x8a, 0x78, 0xe2,
  23704. 0xb3, 0xe1, 0xfa, 0x5b, 0x73, 0x3b, 0x34,
  23705. 0x9f, 0x4e, 0x7d, 0xd5, 0x1b, 0xb9, 0x8e,
  23706. 0x43, 0x76, 0xef, 0x3e, 0x37, 0x70, 0x33,
  23707. 0x36, 0xd1, 0xa1, 0xba, 0x1b, 0xb1, 0x79,
  23708. 0xfb, 0x2c, 0xb1, 0x9b, 0xc3, 0x1b, 0x26,
  23709. 0x83, 0x89, 0x4d, 0x53, 0x40, 0xa5, 0xf9,
  23710. 0x8b, 0xe2, 0xec, 0x30, 0x1f, 0xf6, 0x16,
  23711. 0xd6, 0x55, 0xce, 0x0e, 0x1b, 0xed, 0xe0,
  23712. 0xeb, 0xc9, 0x7a, 0x2e, 0x1a, 0x85, 0x81,
  23713. 0xa4, 0xe2, 0xa8, 0xbe, 0x9f, 0xac, 0x0b,
  23714. 0x23, 0xb4, 0xbb, 0xc2, 0x0f, 0x66, 0x43,
  23715. 0x45, 0x93, 0x20, 0x37, 0x4d, 0x47, 0x23,
  23716. 0x7f, 0x4a, 0x5e, 0x8b, 0x19, 0xec, 0xd9,
  23717. 0x57, 0x69, 0xc4, 0x91, 0xb0, 0xcd, 0x25,
  23718. 0x2a, 0x7d, 0x52, 0xdb, 0x59, 0x18, 0x8b,
  23719. 0x96, 0xad, 0x75, 0x21, 0x81, 0x1a, 0x2c,
  23720. 0xb3, 0x26, 0x30, 0x78, 0x19, 0x2b, 0x22,
  23721. 0x74, 0x6e, 0x92, 0x57, 0xec, 0x3c, 0x75,
  23722. 0x8b, 0xd8, 0x4b, 0x7c, 0xd1, 0x72, 0x1b,
  23723. 0x1f, 0xed, 0xae, 0x15, 0x82, 0xd3, 0xf6,
  23724. 0xaf, 0x01, 0x31, 0xec, 0x1b, 0xca, 0xa5,
  23725. 0xf8, 0x78, 0x7f, 0x8a, 0x8a, 0x03, 0xbd,
  23726. 0x03, 0x0a, 0xc5, 0x4e, 0x15, 0xab, 0xa4,
  23727. 0x76, 0x56, 0x5b, 0xf8, 0x50, 0xa9, 0xee,
  23728. 0x61, 0xbd, 0x05, 0xe0, 0xdf, 0xc6, 0xbe,
  23729. 0x4a, 0xaf, 0xdb, 0x96, 0x0a, 0x7e, 0xcb,
  23730. 0x2e, 0xb0, 0x68, 0x4e, 0x2d, 0x88, 0x32,
  23731. 0x1e, 0xe1, 0xbc, 0x08, 0x15, 0x15, 0x71,
  23732. 0xe6, 0x77, 0x2b, 0xeb, 0x47, 0x81, 0xb7,
  23733. 0xe8, 0x82, 0x9f, 0x5f, 0x94, 0xd2, 0xac,
  23734. 0xa5, 0x89, 0x52, 0xe1, 0x3c, 0x59, 0xe0,
  23735. 0x06, 0xe6, 0x66, 0xe1, 0xf9, 0x9d, 0x32,
  23736. 0x42, 0x9d, 0x77, 0xfe, 0x6a, 0x12, 0x4a,
  23737. 0xa3, 0xd2, 0x49, 0xbb, 0x39, 0xad, 0x42,
  23738. 0xb7, 0x37, 0xfb, 0xde, 0x9d, 0xaf, 0x1b,
  23739. 0xd5, 0x5a, 0x3b, 0x06, 0xa6, 0x51, 0x7d,
  23740. 0x6a, 0x5c, 0x32, 0xdb, 0xde, 0x5d, 0x0d,
  23741. 0x20, 0x88, 0xee, 0x8b, 0xa8, 0x49, 0x5b,
  23742. 0x6c, 0x50, 0x72, 0xdb, 0x68, 0x44, 0x17,
  23743. 0x28, 0xd4, 0xbb, 0x43, 0x8e, 0x00, 0xa5,
  23744. 0xc8, 0x27, 0x00, 0xaa, 0x2b, 0xa4, 0xc2,
  23745. 0x16, 0xcd, 0x2d, 0x59, 0xdc, 0x1a, 0xa2,
  23746. 0x66, 0xe2, 0x96, 0x6b, 0xcc, 0x39, 0xc6,
  23747. 0xe9, 0x2b, 0x14, 0xa7, 0x7d, 0x67, 0x5d,
  23748. 0x54, 0xfc, 0x93, 0x73, 0x52, 0x47, 0xc7,
  23749. 0x24, 0x1e, 0x7e, 0xc9, 0x2d, 0x87, 0x60,
  23750. 0xd3, 0xd8, 0x76, 0xf0, 0x51, 0x04, 0xc7,
  23751. 0xcb, 0x68, 0x0f, 0xd8, 0x4b, 0x22, 0xb2,
  23752. 0x51, 0x87, 0xe9, 0x1e, 0x05, 0x3d, 0xe2,
  23753. 0x8a, 0x6b, 0xb8, 0x96, 0xd6, 0xe0, 0x6e,
  23754. 0x38, 0x74, 0x96, 0xad, 0x7e, 0x4f, 0x52,
  23755. 0x35, 0xcf, 0x4a, 0x50, 0xe7, 0x60, 0x2e,
  23756. 0x58, 0xcf, 0xdc, 0x7a, 0x9a, 0x21, 0x76,
  23757. 0x1d, 0x2c, 0xd1, 0x98, 0xab, 0xab, 0xed,
  23758. 0xf9, 0xec, 0xd5, 0x7b, 0x09, 0xad, 0x2e,
  23759. 0xad, 0x5a, 0xdc, 0xad, 0xd6, 0x46, 0xba,
  23760. 0x2d, 0x55, 0xf7, 0x0c, 0x9a, 0x23, 0x10,
  23761. 0x50, 0x3e, 0x4f, 0xe1, 0xeb, 0x58, 0x8a,
  23762. 0xc0, 0x17, 0x48, 0x41, 0x40, 0x65, 0x0b,
  23763. 0xfb, 0x43, 0x9e, 0xf0, 0x37, 0x4a, 0x89,
  23764. 0x4e, 0x71, 0xad, 0x44, 0x19, 0x13, 0xbb,
  23765. 0x4a, 0x63, 0x83, 0x9e, 0x6a, 0x49, 0x1b,
  23766. 0x28, 0xb0, 0x8e, 0x9c, 0x7b, 0xaf, 0xf9,
  23767. 0x57, 0x5d, 0x35, 0x16, 0x5c, 0xa7, 0x5e,
  23768. 0xd1, 0x0d, 0x83, 0xdc, 0x49, 0xdd, 0x40,
  23769. 0x58, 0x9c, 0x97, 0x91, 0xa6, 0xb0, 0x68,
  23770. 0xb0, 0xfa, 0x9e, 0xc0, 0x3f, 0x81, 0xc6,
  23771. 0xce, 0x58, 0xc5, 0x87, 0xc6, 0xf4, 0x06,
  23772. 0xec, 0x91, 0x57, 0x81, 0xce, 0x3a, 0xe8,
  23773. 0xf1, 0x29, 0x3f, 0x01, 0x93, 0xf0, 0x74,
  23774. 0x22, 0xea, 0x6b, 0x06, 0xd8, 0x65, 0xdb,
  23775. 0xd7, 0x41, 0xd9, 0x60, 0x23, 0xe7, 0x83,
  23776. 0xc8, 0x69, 0x6b, 0x90, 0xc5, 0xc7, 0xb9,
  23777. 0xd5, 0xba, 0x79, 0xc9, 0x4a, 0x87, 0x23,
  23778. 0x1c, 0x95, 0x78, 0xf3, 0x73, 0x10, 0xbe,
  23779. 0xb2, 0x0f, 0x32, 0xec, 0xff, 0x15, 0x51,
  23780. 0x4d, 0xb5, 0x48, 0x3c, 0xca, 0x4c, 0x5b,
  23781. 0x32, 0x29, 0x47, 0x21, 0xba, 0x2a, 0x5d,
  23782. 0xc9, 0x59, 0xfa, 0x8f, 0x33, 0x10, 0x83,
  23783. 0x40, 0x80, 0xf3, 0xce, 0xee, 0x6d, 0xcd,
  23784. 0x9c, 0xbb, 0x23, 0x0b, 0x45, 0xba, 0x7a,
  23785. 0x07, 0xdc, 0x4d, 0x57, 0x97, 0xb4, 0xa4,
  23786. 0xef, 0x94, 0xe8, 0x43, 0xfe, 0x18, 0x47,
  23787. 0x1a, 0xb0, 0xf6, 0xb6, 0x0b, 0x55, 0x05,
  23788. 0xbd, 0x67, 0x2d, 0x37, 0x27, 0x17, 0x13,
  23789. 0x65, 0x22, 0xf2, 0x7c, 0xf7, 0x47, 0xd2,
  23790. 0x85, 0x63, 0x98, 0x83, 0xd2, 0xc1, 0xbf,
  23791. 0x8f, 0x4c, 0xda, 0xbf, 0xa4, 0x10, 0x6b,
  23792. 0x4e, 0x6b, 0x78, 0x5e, 0x3f, 0x7a, 0xec,
  23793. 0x15, 0x84, 0xbe, 0x1a, 0x94, 0xa2, 0x2b,
  23794. 0xb5, 0x3e, 0x55, 0x86, 0x51, 0xec, 0x2e,
  23795. 0x62, 0xcb, 0xd6, 0x9f, 0xe5, 0xa4, 0xb8,
  23796. 0xc0, 0xaa, 0x4e, 0x6d, 0x8a, 0xb1, 0xd6,
  23797. 0xf7, 0x8d, 0x1c, 0x04, 0x32, 0x8b, 0x20,
  23798. 0xf5, 0x80, 0x33, 0xbd, 0xcc, 0x3e, 0x4c,
  23799. 0x16, 0x04, 0xab, 0xd8, 0x64, 0x6d, 0xf9,
  23800. 0xc9, 0x15, 0x7d, 0x4b, 0x00, 0x86, 0xb2,
  23801. 0x70, 0x1d, 0x20, 0xcb, 0x7a, 0xed, 0x7e,
  23802. 0x81, 0x7f, 0x41, 0x33, 0xb8, 0x7b, 0xc0,
  23803. 0xa3, 0xbd, 0x12, 0xd1, 0x67, 0x48, 0xa0,
  23804. 0xb9, 0xeb, 0xd5, 0x29, 0xab, 0x91, 0x9c,
  23805. 0xa2, 0x2f, 0x8e, 0x01, 0x1c, 0x88, 0xc1,
  23806. 0x3e, 0x34, 0x47, 0x36, 0x8a, 0x35, 0x6c,
  23807. 0x2f, 0xc1, 0x8a, 0xb6, 0xd0, 0xa5, 0x01,
  23808. 0x82, 0xee, 0x4f, 0x44, 0xb9, 0xcd, 0x16,
  23809. 0x9c, 0x3a, 0xf8, 0xe9, 0x2a, 0xd2, 0xb6,
  23810. 0x1d, 0xfd, 0x3c, 0x06, 0xdc, 0x42, 0xdd,
  23811. 0x2d, 0x60, 0x6a, 0x44, 0x21, 0xc3, 0x37,
  23812. 0x75, 0x79, 0xc5, 0x29, 0x5c, 0x7e, 0xf5,
  23813. 0x86, 0xbb, 0x56, 0x05, 0x21, 0x46, 0xaf,
  23814. 0x6d, 0x3a, 0xa2, 0x9e, 0x11, 0x6d, 0x9e,
  23815. 0x05, 0x74, 0x8a, 0xfe, 0x84, 0x88, 0x3e,
  23816. 0x76, 0xb4, 0xef, 0x2f, 0xeb, 0x52, 0xcd,
  23817. 0x97, 0x82, 0xba, 0x0c, 0xcc, 0xcb, 0x72,
  23818. 0x8d, 0x8d, 0xd2, 0x32, 0x7c, 0x41, 0x39,
  23819. 0xa6, 0x22, 0xb7, 0xdc, 0x3f, 0x39, 0x43,
  23820. 0xf5, 0xee, 0x0c, 0xfc, 0xbb, 0x2b, 0x43,
  23821. 0xe8, 0xce, 0xae, 0x0c, 0xd9, 0x15, 0x22,
  23822. 0x32, 0xbd, 0x69, 0xad, 0x76, 0xd9, 0xdf,
  23823. 0x81, 0xdf, 0x24, 0x76, 0x7b, 0x53, 0x0b,
  23824. 0xe6, 0xc7, 0x6c, 0x38, 0x2c, 0xbf, 0x28,
  23825. 0x95, 0x03, 0x18, 0xef, 0x98, 0x88, 0xc2,
  23826. 0x6b, 0x1a, 0xf5, 0xb4, 0xf9, 0x19, 0x76,
  23827. 0x25, 0x1d, 0xcf, 0x9b, 0xcd, 0x4c, 0x00,
  23828. 0x06, 0xde, 0x55, 0x58, 0x95, 0x9a, 0x06,
  23829. 0xfb, 0xf9, 0x88, 0x20, 0x85, 0x80, 0xe3,
  23830. 0x27, 0xdf, 0xc5, 0x20, 0x29, 0x7c, 0x58,
  23831. 0x02, 0x07, 0x2e, 0xd2, 0xeb, 0xdc, 0x68,
  23832. 0x58, 0x91, 0x08, 0x71, 0x16, 0xb3, 0x82,
  23833. 0x2f, 0x6c, 0x45, 0xcd, 0xbe, 0xe5, 0x0c,
  23834. 0x07, 0x77, 0x95, 0x3b, 0x2c, 0x59, 0x8e,
  23835. 0xba, 0x07, 0xa8, 0xa1, 0xc6, 0xe5, 0x6a,
  23836. 0x49, 0xb5, 0x85, 0xf2, 0x70, 0x05, 0x22,
  23837. 0xc4, 0x2f, 0x8d, 0xdd, 0x48, 0x8d, 0x87,
  23838. 0xfa, 0xb6, 0xf8, 0x59, 0xc8, 0xb1, 0x18,
  23839. 0x03, 0x5f, 0xce, 0x53, 0x28, 0x96, 0x15,
  23840. 0xd4, 0xb4, 0x10, 0x2c, 0xe2, 0x22, 0x9e,
  23841. 0x88, 0xe5, 0xcd, 0xda, 0xfc, 0xf9, 0x64,
  23842. 0xa4, 0x7b, 0xfb, 0xeb, 0xa8, 0x6a, 0xb6,
  23843. 0xf6, 0x17, 0x84, 0x26, 0x3d, 0xe4, 0x66,
  23844. 0x7e, 0x5c, 0x85, 0x01, 0xaf, 0xdc, 0xdb,
  23845. 0x48, 0x33, 0x4a, 0x20, 0x7c, 0x22, 0x1b,
  23846. 0xd5, 0xeb, 0x2d,
  23847. };
  23848. static const byte ml_dsa_44_good_sig[] = {
  23849. 0xfc, 0x2d, 0xa0, 0x06, 0x85, 0xc2, 0xfc,
  23850. 0x92, 0x47, 0x77, 0x0b, 0x39, 0xbf, 0xe5,
  23851. 0xba, 0xd4, 0x44, 0xbf, 0xde, 0xce, 0x1f,
  23852. 0x04, 0xa2, 0x87, 0xed, 0x4a, 0xce, 0x0e,
  23853. 0xf3, 0x95, 0x61, 0x1e, 0x66, 0x4e, 0x9a,
  23854. 0x5d, 0x00, 0x31, 0x32, 0xf0, 0x90, 0x3d,
  23855. 0x7e, 0xf2, 0x9d, 0xe2, 0x93, 0xa1, 0xc8,
  23856. 0x64, 0x36, 0xf2, 0x59, 0xc7, 0x9e, 0xb6,
  23857. 0xb3, 0x6f, 0xe5, 0x80, 0x8d, 0x92, 0x77,
  23858. 0xd6, 0xb6, 0xe4, 0xc5, 0x5e, 0x79, 0x45,
  23859. 0x4b, 0xd0, 0xfe, 0x53, 0x55, 0xb6, 0x66,
  23860. 0x88, 0xfe, 0x95, 0x40, 0x07, 0xfd, 0xdb,
  23861. 0x40, 0x33, 0x39, 0x67, 0x03, 0x30, 0x8e,
  23862. 0x80, 0x4e, 0xa7, 0x0e, 0xe4, 0x05, 0x04,
  23863. 0xc5, 0x33, 0x72, 0x47, 0x5b, 0x85, 0x0f,
  23864. 0xe1, 0xeb, 0x98, 0x1a, 0x76, 0x79, 0x84,
  23865. 0xce, 0x26, 0x66, 0xe8, 0x92, 0xc9, 0x1f,
  23866. 0x40, 0x96, 0x72, 0xfe, 0x61, 0xae, 0xba,
  23867. 0x84, 0x70, 0xb7, 0x92, 0x2f, 0x7e, 0xc8,
  23868. 0xe8, 0xe4, 0x34, 0x73, 0xd4, 0x69, 0x57,
  23869. 0x3e, 0x28, 0x2b, 0x18, 0x0f, 0xef, 0xb1,
  23870. 0x06, 0xe2, 0xf8, 0x79, 0x70, 0x5a, 0x84,
  23871. 0x84, 0x6c, 0xb3, 0x57, 0x5b, 0x18, 0x42,
  23872. 0xdf, 0xd5, 0xdb, 0xf8, 0x35, 0x5f, 0x7b,
  23873. 0x23, 0x25, 0x2f, 0x0f, 0x17, 0x0b, 0x9a,
  23874. 0xb6, 0xe8, 0x31, 0x30, 0x6b, 0x90, 0x06,
  23875. 0x2c, 0xfd, 0xca, 0xaa, 0xa6, 0xc3, 0xdc,
  23876. 0x88, 0xa7, 0x31, 0x74, 0x67, 0xe2, 0x64,
  23877. 0x8f, 0x5c, 0xc4, 0xc0, 0x4d, 0x34, 0x15,
  23878. 0x0d, 0xd2, 0x23, 0x69, 0xfc, 0x6e, 0xbb,
  23879. 0x82, 0xca, 0xc4, 0xee, 0xf1, 0x14, 0xc1,
  23880. 0xd4, 0x5a, 0x71, 0x78, 0x9b, 0x40, 0x01,
  23881. 0xb9, 0xe4, 0x6e, 0x68, 0xf6, 0x13, 0xca,
  23882. 0xc1, 0xea, 0x70, 0x71, 0x3d, 0xc9, 0x1a,
  23883. 0x62, 0xb9, 0xa9, 0xe0, 0x1e, 0xe2, 0x34,
  23884. 0xf2, 0x9a, 0xf7, 0x23, 0xb3, 0xc1, 0xca,
  23885. 0x35, 0x0e, 0x5e, 0xa7, 0xd1, 0x3d, 0xea,
  23886. 0x51, 0xdc, 0xe2, 0x0e, 0xfc, 0x7d, 0x26,
  23887. 0x75, 0xec, 0x9a, 0x6e, 0x40, 0x1f, 0x60,
  23888. 0x06, 0xd7, 0x56, 0xf8, 0xa4, 0x2a, 0x82,
  23889. 0x9c, 0xef, 0x51, 0x4a, 0xe1, 0x01, 0x2b,
  23890. 0xb0, 0x8b, 0x34, 0x7b, 0xe1, 0x63, 0xa4,
  23891. 0xcc, 0x72, 0x81, 0xd9, 0xb4, 0x20, 0xcc,
  23892. 0x60, 0xe2, 0x15, 0x6d, 0xc7, 0x6c, 0x75,
  23893. 0x65, 0x4d, 0xb6, 0xc0, 0x36, 0x49, 0x87,
  23894. 0x06, 0x3e, 0xca, 0x1c, 0x32, 0x36, 0x2f,
  23895. 0xe3, 0xf6, 0x06, 0x0a, 0xb1, 0xd2, 0xfb,
  23896. 0xee, 0x4e, 0xd7, 0xce, 0x65, 0xcc, 0x89,
  23897. 0xf2, 0x77, 0x14, 0x27, 0x27, 0x84, 0x52,
  23898. 0x97, 0x1b, 0x89, 0x17, 0x31, 0x8d, 0xc4,
  23899. 0x0f, 0xc7, 0xc6, 0x45, 0x44, 0x9a, 0x97,
  23900. 0xd3, 0x88, 0x71, 0x73, 0x97, 0x64, 0xa6,
  23901. 0xe6, 0x3d, 0xf2, 0xd3, 0x7f, 0x7f, 0xfa,
  23902. 0x4f, 0xf4, 0xe9, 0x76, 0x8a, 0x2a, 0xfe,
  23903. 0x28, 0x8e, 0xa5, 0xb3, 0x46, 0x2b, 0xad,
  23904. 0x50, 0x5e, 0x12, 0xcd, 0xf8, 0x46, 0xe4,
  23905. 0x06, 0x12, 0xc2, 0xb8, 0xcd, 0x04, 0x76,
  23906. 0x07, 0x7c, 0xed, 0x2f, 0x0f, 0xd6, 0x97,
  23907. 0x31, 0xa4, 0x0c, 0x18, 0x85, 0x75, 0xd3,
  23908. 0x55, 0xfd, 0xe3, 0x1a, 0xbf, 0x43, 0xde,
  23909. 0x20, 0xa9, 0x19, 0xcd, 0x03, 0x01, 0xdf,
  23910. 0x04, 0x71, 0x09, 0x94, 0x99, 0x51, 0xb0,
  23911. 0x8e, 0x32, 0x80, 0xe6, 0x64, 0x4b, 0xdf,
  23912. 0xa5, 0xec, 0xfa, 0xce, 0xf6, 0xf3, 0xce,
  23913. 0x51, 0xe8, 0x6d, 0x03, 0x1e, 0x69, 0x59,
  23914. 0xef, 0x20, 0x98, 0x71, 0xe2, 0xc3, 0xec,
  23915. 0x19, 0x03, 0xa9, 0x2d, 0x12, 0x21, 0x79,
  23916. 0x7e, 0xb0, 0xcb, 0x76, 0x68, 0x2f, 0x11,
  23917. 0x01, 0x2b, 0x11, 0xef, 0xd6, 0xb7, 0x8b,
  23918. 0x5e, 0x31, 0x78, 0x7b, 0x2d, 0xe6, 0x4a,
  23919. 0xfb, 0xc1, 0xbb, 0x78, 0x92, 0x11, 0xcb,
  23920. 0x91, 0x97, 0x52, 0x1b, 0x8e, 0xfb, 0x59,
  23921. 0x02, 0x22, 0xbe, 0xf7, 0x33, 0xaa, 0x7b,
  23922. 0xfd, 0x93, 0xf7, 0xa8, 0x9b, 0xfc, 0x99,
  23923. 0x36, 0x22, 0x04, 0x1e, 0xdc, 0xa3, 0x2b,
  23924. 0xe6, 0xac, 0x2e, 0x4b, 0x38, 0x0a, 0x25,
  23925. 0xde, 0x3d, 0x8e, 0x0b, 0x95, 0x04, 0x48,
  23926. 0x3d, 0x66, 0x52, 0x99, 0x79, 0xe1, 0x8d,
  23927. 0xe0, 0xa7, 0xd5, 0x23, 0x0d, 0x45, 0x89,
  23928. 0x88, 0xa9, 0x59, 0x4e, 0xc7, 0x64, 0x39,
  23929. 0x93, 0xdd, 0xcb, 0xfe, 0x97, 0xe7, 0x7d,
  23930. 0xab, 0x61, 0x08, 0xf7, 0x7a, 0xff, 0x10,
  23931. 0x1d, 0x8d, 0x11, 0xa9, 0x97, 0xbd, 0x16,
  23932. 0xb5, 0x6c, 0x84, 0x71, 0x61, 0x72, 0x36,
  23933. 0x51, 0xe7, 0x43, 0x8b, 0x15, 0xb2, 0x48,
  23934. 0x6a, 0x14, 0x8a, 0xbe, 0x92, 0xa7, 0xfa,
  23935. 0xce, 0x02, 0x1a, 0x7f, 0xc5, 0xdb, 0x76,
  23936. 0x0a, 0x4c, 0xc7, 0x20, 0x2a, 0x34, 0xf4,
  23937. 0x92, 0x3b, 0x34, 0x69, 0x71, 0x3d, 0xe1,
  23938. 0xf2, 0x2f, 0x52, 0xe1, 0x48, 0xbe, 0x27,
  23939. 0x47, 0x1d, 0x55, 0x96, 0x6e, 0xa3, 0x39,
  23940. 0xc6, 0xd8, 0x12, 0xe0, 0xb2, 0x93, 0x56,
  23941. 0xef, 0x10, 0xf4, 0xa6, 0xf4, 0x5f, 0xa9,
  23942. 0xfd, 0x5d, 0x01, 0x87, 0xb8, 0xe5, 0x4f,
  23943. 0x86, 0x2c, 0xa5, 0x09, 0xfc, 0x29, 0x84,
  23944. 0x5b, 0x44, 0xf9, 0x8d, 0x9c, 0xbb, 0x19,
  23945. 0x97, 0x52, 0xbb, 0xac, 0x19, 0x57, 0x68,
  23946. 0x7d, 0x74, 0xb5, 0x4f, 0xda, 0x8a, 0x9c,
  23947. 0xcf, 0x5d, 0x7b, 0xd9, 0xf0, 0xb3, 0x11,
  23948. 0x76, 0x50, 0x03, 0x06, 0x44, 0xe7, 0x68,
  23949. 0x35, 0xe9, 0x14, 0x20, 0xbd, 0x0d, 0x90,
  23950. 0x96, 0x21, 0xa1, 0x17, 0x8f, 0xf7, 0x50,
  23951. 0x6c, 0xc0, 0x76, 0x3d, 0x34, 0x8d, 0xf2,
  23952. 0x75, 0xf0, 0xa2, 0x6c, 0x8a, 0xc1, 0x56,
  23953. 0x95, 0xb2, 0xd9, 0x87, 0xf2, 0xe4, 0x80,
  23954. 0x25, 0xc7, 0x97, 0xd2, 0xd2, 0xf8, 0x1c,
  23955. 0x7a, 0x48, 0x70, 0x99, 0x6b, 0xf2, 0x50,
  23956. 0x83, 0xf3, 0x10, 0xb1, 0x9b, 0x6d, 0x75,
  23957. 0x53, 0x86, 0x23, 0xc9, 0x60, 0x4d, 0x73,
  23958. 0xc7, 0x52, 0x90, 0x12, 0x6b, 0x92, 0x2d,
  23959. 0x35, 0xbc, 0x4d, 0x86, 0x67, 0xfe, 0x35,
  23960. 0x11, 0x6c, 0xbb, 0x9b, 0x76, 0xaf, 0x26,
  23961. 0xae, 0x50, 0x23, 0x76, 0x68, 0x16, 0x80,
  23962. 0xf0, 0xa4, 0xcc, 0x76, 0x6b, 0xf3, 0x99,
  23963. 0x04, 0x8b, 0x39, 0xf2, 0xa6, 0xa9, 0x72,
  23964. 0x6f, 0xbe, 0xa8, 0xdb, 0x53, 0xf3, 0x93,
  23965. 0x00, 0xac, 0x3e, 0x8f, 0xdd, 0x68, 0x9e,
  23966. 0x2f, 0xe3, 0x48, 0x0b, 0x11, 0xe2, 0x9a,
  23967. 0xfa, 0x98, 0x32, 0x40, 0x26, 0xf8, 0x83,
  23968. 0xc6, 0x00, 0x02, 0x7d, 0xb5, 0xd2, 0xd4,
  23969. 0xdd, 0xc6, 0x02, 0xec, 0xb3, 0x98, 0xd6,
  23970. 0x8e, 0xab, 0x75, 0x06, 0x37, 0x97, 0x4c,
  23971. 0x50, 0xc5, 0xe1, 0x43, 0x34, 0xd6, 0xbd,
  23972. 0xb6, 0xfc, 0xb7, 0x04, 0x0d, 0xd8, 0x35,
  23973. 0xb4, 0x3e, 0x0e, 0x43, 0x22, 0x83, 0xf5,
  23974. 0x5d, 0x2e, 0x0a, 0x8f, 0xa0, 0xec, 0x17,
  23975. 0xd9, 0xa1, 0x84, 0x98, 0x32, 0x5c, 0x99,
  23976. 0x66, 0x05, 0x70, 0x9a, 0xa4, 0x9b, 0xbe,
  23977. 0xd3, 0x3d, 0x2a, 0x13, 0xb1, 0x96, 0x37,
  23978. 0x4f, 0xe7, 0x6f, 0x6b, 0x5e, 0x80, 0xe4,
  23979. 0xb6, 0x98, 0x56, 0xed, 0xff, 0x5b, 0x21,
  23980. 0x5f, 0x79, 0x9a, 0x0f, 0x53, 0x69, 0x76,
  23981. 0xdb, 0xc0, 0x12, 0x9c, 0xed, 0xd4, 0x00,
  23982. 0x64, 0xca, 0xf4, 0xc3, 0x17, 0x49, 0xbb,
  23983. 0xef, 0xbc, 0x7b, 0x73, 0x24, 0x4f, 0x6e,
  23984. 0xcf, 0x25, 0x49, 0x30, 0x4f, 0x9a, 0xb7,
  23985. 0x2a, 0x2f, 0xc2, 0x69, 0x74, 0xe5, 0xa6,
  23986. 0xd7, 0x29, 0x4d, 0x80, 0xe6, 0xf6, 0x66,
  23987. 0x4b, 0xdf, 0xef, 0xfd, 0xb5, 0xaa, 0x53,
  23988. 0x75, 0x0e, 0xf5, 0x92, 0xb3, 0x30, 0x3d,
  23989. 0x5d, 0xa8, 0x49, 0x74, 0xa2, 0x13, 0xb3,
  23990. 0x99, 0x7e, 0xf9, 0x34, 0x08, 0xc2, 0xa6,
  23991. 0xc9, 0xd1, 0xb7, 0x65, 0xf4, 0xa9, 0xda,
  23992. 0x11, 0x07, 0x89, 0x08, 0x92, 0xdf, 0x1a,
  23993. 0x8d, 0xd7, 0x1e, 0xe9, 0xa3, 0x5c, 0x66,
  23994. 0x79, 0xa0, 0x2d, 0xd1, 0xd9, 0x65, 0xa2,
  23995. 0xd3, 0x47, 0xb1, 0xa1, 0xf4, 0xa1, 0x18,
  23996. 0x7f, 0xb0, 0xd1, 0x20, 0x05, 0x06, 0x6f,
  23997. 0xda, 0xe3, 0xef, 0xee, 0x47, 0xdf, 0x80,
  23998. 0x22, 0x14, 0x4b, 0xc2, 0xe4, 0xea, 0x02,
  23999. 0xe7, 0x06, 0xc8, 0x2d, 0x2d, 0xbd, 0xd2,
  24000. 0xef, 0xd3, 0x3b, 0xf0, 0xc8, 0xc1, 0x04,
  24001. 0x53, 0x81, 0x27, 0xb2, 0xf2, 0xf8, 0x5d,
  24002. 0xe1, 0x27, 0xd6, 0xd8, 0x2e, 0x0d, 0x43,
  24003. 0xf4, 0xf0, 0x7a, 0x8c, 0x78, 0x3f, 0x23,
  24004. 0x4d, 0x05, 0xf6, 0xc6, 0x9a, 0xc1, 0x19,
  24005. 0x58, 0x48, 0x02, 0x71, 0xc4, 0xf1, 0x16,
  24006. 0xc2, 0xbf, 0xc2, 0xf5, 0xa6, 0x70, 0x25,
  24007. 0x8b, 0x6c, 0x31, 0xa6, 0x2b, 0x6a, 0x1f,
  24008. 0x26, 0x4a, 0x6b, 0x05, 0x67, 0xa9, 0x5e,
  24009. 0xa6, 0xc7, 0x19, 0xf0, 0x6f, 0xc2, 0xa4,
  24010. 0x07, 0xe1, 0xf8, 0xb6, 0x70, 0x79, 0x85,
  24011. 0x97, 0x1f, 0xc2, 0x97, 0xf3, 0x57, 0x3d,
  24012. 0xd1, 0x70, 0xb1, 0xe8, 0x43, 0x11, 0xe6,
  24013. 0x2f, 0x8a, 0x12, 0x97, 0xe1, 0x1f, 0x86,
  24014. 0x33, 0xa1, 0x30, 0xb3, 0x8e, 0xd7, 0x43,
  24015. 0x99, 0x35, 0xd3, 0x48, 0x04, 0x29, 0xa3,
  24016. 0xf3, 0x2c, 0x0e, 0xda, 0x66, 0xc8, 0xa9,
  24017. 0xd3, 0x28, 0x7f, 0xf0, 0xaa, 0x53, 0xc8,
  24018. 0x4b, 0xa3, 0xde, 0xcc, 0x5f, 0xae, 0x9b,
  24019. 0x2f, 0x20, 0xf7, 0x9a, 0x41, 0xb7, 0xdb,
  24020. 0x7a, 0x92, 0xa1, 0x45, 0x2c, 0x49, 0xb7,
  24021. 0x2d, 0xe5, 0x6e, 0x84, 0xb1, 0xa4, 0x6e,
  24022. 0xea, 0xf9, 0xca, 0xc6, 0x0f, 0xd3, 0xdc,
  24023. 0xad, 0x79, 0xf4, 0x3b, 0xc0, 0x8c, 0x8c,
  24024. 0x6f, 0xdf, 0x2c, 0xbb, 0x2a, 0x9f, 0x74,
  24025. 0x2f, 0x31, 0x7b, 0x05, 0xc5, 0xc4, 0x22,
  24026. 0xfd, 0xfc, 0xdf, 0x95, 0x3b, 0x21, 0x41,
  24027. 0xab, 0x5b, 0xe9, 0x37, 0xdf, 0xd3, 0x4c,
  24028. 0x97, 0x73, 0xac, 0xbd, 0x82, 0x97, 0xd0,
  24029. 0x00, 0x55, 0x47, 0xa4, 0x67, 0x12, 0xcd,
  24030. 0xe3, 0x01, 0x31, 0xe6, 0x1c, 0x61, 0xf8,
  24031. 0xa4, 0xce, 0x23, 0xed, 0xeb, 0x8f, 0x57,
  24032. 0xbf, 0x97, 0x4b, 0x26, 0x75, 0xa0, 0x4b,
  24033. 0xf6, 0x7e, 0xda, 0x1c, 0x90, 0x8a, 0xc4,
  24034. 0xf4, 0x60, 0xfc, 0x65, 0x45, 0x8c, 0x1b,
  24035. 0x5c, 0x65, 0xc4, 0x5d, 0x99, 0x9a, 0xed,
  24036. 0x62, 0xd6, 0x74, 0x43, 0xac, 0x5e, 0x0c,
  24037. 0xfd, 0x7d, 0xf2, 0x4f, 0xc3, 0x02, 0xc8,
  24038. 0x24, 0xa7, 0x17, 0x49, 0xf9, 0xdd, 0xc9,
  24039. 0x64, 0x71, 0x08, 0xb9, 0xac, 0x47, 0x3d,
  24040. 0x6a, 0x4f, 0xdc, 0xa6, 0x56, 0x00, 0x01,
  24041. 0x53, 0x2d, 0xca, 0x32, 0x4e, 0x40, 0xec,
  24042. 0x14, 0x03, 0x69, 0x75, 0xfa, 0x93, 0x49,
  24043. 0x98, 0x04, 0x25, 0x29, 0xe5, 0x78, 0x6e,
  24044. 0x3e, 0x99, 0x58, 0x18, 0x48, 0xf3, 0x29,
  24045. 0x56, 0x8d, 0x23, 0xd1, 0x5d, 0xb2, 0x2a,
  24046. 0x74, 0xa7, 0x53, 0xc5, 0xc6, 0xc4, 0x12,
  24047. 0xfe, 0x65, 0x6c, 0xde, 0xc3, 0x6c, 0x18,
  24048. 0xde, 0xc3, 0x97, 0xaa, 0xed, 0x69, 0x3c,
  24049. 0x4d, 0xc9, 0xa9, 0x63, 0xa9, 0x4d, 0x91,
  24050. 0x63, 0xa3, 0x1c, 0x87, 0x36, 0x19, 0x4a,
  24051. 0xc5, 0xd1, 0xcb, 0xf4, 0x88, 0xfd, 0xa0,
  24052. 0x9b, 0x37, 0x9a, 0x7e, 0xcc, 0x09, 0xac,
  24053. 0x3b, 0xf0, 0xf5, 0xb5, 0x15, 0x72, 0x47,
  24054. 0xb0, 0x42, 0x0d, 0xed, 0x19, 0x42, 0x93,
  24055. 0x5a, 0x56, 0xbf, 0x2c, 0x4b, 0xec, 0xf4,
  24056. 0x13, 0x30, 0x0b, 0xdf, 0x0e, 0xc1, 0x22,
  24057. 0xa5, 0x6c, 0xf4, 0xcf, 0x09, 0x83, 0xe2,
  24058. 0xd0, 0x05, 0x62, 0x8d, 0xda, 0xea, 0x79,
  24059. 0xa9, 0x6c, 0xe1, 0x90, 0xc0, 0xe7, 0x6d,
  24060. 0x63, 0x8e, 0xe5, 0xe0, 0xa9, 0x67, 0x80,
  24061. 0xb7, 0x80, 0x43, 0xfc, 0xa1, 0x11, 0x6d,
  24062. 0xc9, 0x91, 0xa3, 0xcb, 0x1a, 0x6e, 0xf8,
  24063. 0x6e, 0xdb, 0xf0, 0x7d, 0xeb, 0x45, 0x6a,
  24064. 0xee, 0xd8, 0x3e, 0x6b, 0x9a, 0xc4, 0xcd,
  24065. 0x7b, 0x35, 0x19, 0x73, 0x14, 0x22, 0x47,
  24066. 0x86, 0x54, 0x34, 0xc9, 0x02, 0x49, 0xe6,
  24067. 0xb9, 0x45, 0xf7, 0x2c, 0xf7, 0xa4, 0x12,
  24068. 0xc4, 0x7a, 0x95, 0x41, 0x54, 0x8d, 0x51,
  24069. 0xc5, 0x1a, 0x19, 0x75, 0x0c, 0x11, 0x03,
  24070. 0xd0, 0x6f, 0x56, 0x04, 0xb2, 0x87, 0x21,
  24071. 0xdb, 0x47, 0xed, 0x9b, 0xe8, 0xec, 0xa3,
  24072. 0xe1, 0x74, 0x3e, 0x21, 0x50, 0xf2, 0x09,
  24073. 0x2c, 0x5e, 0x3d, 0xca, 0xa7, 0x31, 0x7c,
  24074. 0xbd, 0xe4, 0xf1, 0x15, 0x0d, 0xe6, 0x3d,
  24075. 0x4c, 0x8e, 0x36, 0x45, 0xf7, 0x08, 0x3d,
  24076. 0x56, 0x63, 0xb6, 0x99, 0x46, 0x34, 0x48,
  24077. 0xfa, 0xcf, 0x88, 0xe5, 0x43, 0xf7, 0x88,
  24078. 0xd5, 0x55, 0x13, 0xfe, 0x79, 0x02, 0x49,
  24079. 0x1e, 0x82, 0x45, 0x45, 0x29, 0x8e, 0x0a,
  24080. 0x7c, 0x77, 0x51, 0x1b, 0x8e, 0x75, 0xd5,
  24081. 0xd6, 0x61, 0xff, 0xa5, 0xfe, 0x59, 0x48,
  24082. 0xbf, 0xfc, 0xf2, 0xcd, 0x37, 0x09, 0x4c,
  24083. 0xb6, 0xfc, 0xb0, 0x20, 0x5f, 0x12, 0x7a,
  24084. 0x3f, 0x64, 0x96, 0xdb, 0xc8, 0xe8, 0xd0,
  24085. 0x11, 0x19, 0x47, 0x95, 0x2d, 0x3d, 0xbf,
  24086. 0x56, 0x9c, 0x23, 0x30, 0x07, 0x1f, 0x2c,
  24087. 0x04, 0x5c, 0x7e, 0x4e, 0x2e, 0xa6, 0x20,
  24088. 0x38, 0xa0, 0x88, 0x47, 0x8a, 0x3f, 0x8f,
  24089. 0x8a, 0xe7, 0x6d, 0x0a, 0xf1, 0x2d, 0xd6,
  24090. 0x10, 0x23, 0x01, 0x62, 0x71, 0x5a, 0xbb,
  24091. 0x51, 0x98, 0xa1, 0x27, 0x7d, 0xba, 0x5f,
  24092. 0xe4, 0xdc, 0xd6, 0xd7, 0x4c, 0x1a, 0xe0,
  24093. 0x4d, 0xe1, 0xee, 0x61, 0xb7, 0xc5, 0x50,
  24094. 0x92, 0x99, 0xc7, 0x7b, 0x18, 0xf9, 0x89,
  24095. 0x2b, 0x57, 0xf9, 0xe9, 0xac, 0x23, 0x6e,
  24096. 0xff, 0xbd, 0x5a, 0x93, 0xdf, 0x15, 0x74,
  24097. 0x92, 0x0b, 0x76, 0x84, 0x96, 0x94, 0x1b,
  24098. 0x8f, 0xe4, 0x6e, 0x2b, 0xd7, 0x47, 0xfd,
  24099. 0x24, 0x3b, 0xe4, 0xe4, 0x99, 0xf9, 0x0b,
  24100. 0xf9, 0x29, 0x25, 0x25, 0x6a, 0xc0, 0x1e,
  24101. 0xb8, 0x8c, 0xd6, 0xd0, 0x6e, 0x13, 0x86,
  24102. 0xa8, 0x7c, 0xc3, 0x31, 0x30, 0x2e, 0x9b,
  24103. 0x51, 0xc1, 0x21, 0xea, 0x15, 0x8e, 0xd8,
  24104. 0x06, 0xa2, 0xe9, 0x12, 0x9f, 0xcb, 0x6b,
  24105. 0x24, 0xef, 0x4e, 0x19, 0x6c, 0xa5, 0x98,
  24106. 0x47, 0x8b, 0x4d, 0xbe, 0x00, 0x0c, 0x04,
  24107. 0xe3, 0x4d, 0x84, 0x64, 0x36, 0x20, 0x9f,
  24108. 0xde, 0xe2, 0x55, 0x89, 0x3e, 0x40, 0xe1,
  24109. 0xe3, 0x63, 0x0a, 0xe7, 0x15, 0x4c, 0xcd,
  24110. 0x4b, 0x63, 0x6f, 0x70, 0xc2, 0x84, 0x30,
  24111. 0x5d, 0x22, 0xd0, 0xe4, 0x65, 0xfb, 0x8a,
  24112. 0x07, 0x1a, 0x54, 0xf5, 0x4b, 0x65, 0xad,
  24113. 0x64, 0x91, 0x6e, 0x23, 0x98, 0x31, 0x26,
  24114. 0x79, 0x70, 0x94, 0xff, 0xc0, 0x65, 0x70,
  24115. 0xb4, 0x9d, 0x74, 0x8e, 0x76, 0x6b, 0x9a,
  24116. 0x24, 0x28, 0x0d, 0x8a, 0x93, 0x87, 0x18,
  24117. 0x04, 0x10, 0xfd, 0x0b, 0xaf, 0xd3, 0x92,
  24118. 0xbb, 0xb5, 0x41, 0xd2, 0x87, 0xc7, 0x69,
  24119. 0x89, 0x0c, 0x52, 0xf8, 0x46, 0x70, 0x8e,
  24120. 0xf0, 0x99, 0x96, 0x57, 0x40, 0x9c, 0xef,
  24121. 0x9a, 0xc2, 0x63, 0x47, 0x13, 0x11, 0x68,
  24122. 0x40, 0xab, 0x36, 0x16, 0x53, 0xd6, 0x8f,
  24123. 0x58, 0x5a, 0xdd, 0x0c, 0xd7, 0x17, 0x6a,
  24124. 0x39, 0x34, 0xf1, 0xe7, 0x75, 0x3b, 0x41,
  24125. 0x7e, 0x5a, 0x70, 0xfe, 0x5b, 0x08, 0x30,
  24126. 0xf4, 0x7b, 0x1d, 0xd0, 0x70, 0xae, 0x18,
  24127. 0xad, 0xd4, 0xff, 0xbb, 0xa4, 0x31, 0xec,
  24128. 0x28, 0x72, 0x64, 0x9a, 0x24, 0x16, 0x30,
  24129. 0xe4, 0xc5, 0x3c, 0xb0, 0x3c, 0x81, 0x4f,
  24130. 0xb9, 0xfc, 0xe1, 0x3a, 0x05, 0x17, 0xb2,
  24131. 0x18, 0x4c, 0x98, 0x3b, 0xfc, 0x93, 0xf0,
  24132. 0x0d, 0xb6, 0x3c, 0x54, 0x7b, 0x10, 0xfd,
  24133. 0x7f, 0x63, 0xfe, 0xa5, 0xc6, 0xb5, 0x24,
  24134. 0xb4, 0xf2, 0x2f, 0xb2, 0x6f, 0x7f, 0xdb,
  24135. 0x01, 0xdb, 0xaf, 0x57, 0xdc, 0xdb, 0xf4,
  24136. 0xc8, 0x31, 0xb0, 0xdd, 0x05, 0x8b, 0x9b,
  24137. 0x6e, 0x7c, 0x5e, 0x5e, 0x31, 0x2c, 0x7f,
  24138. 0xbb, 0xa4, 0x26, 0x88, 0xe1, 0x55, 0x3d,
  24139. 0x8d, 0x36, 0x69, 0xb7, 0xc8, 0xcc, 0x05,
  24140. 0xfe, 0x6e, 0xcc, 0xcb, 0xfd, 0x81, 0x14,
  24141. 0x8b, 0xbc, 0x0c, 0xd4, 0x7e, 0xb4, 0x13,
  24142. 0xc0, 0xe2, 0x51, 0xf4, 0x07, 0xde, 0xc4,
  24143. 0x1c, 0xf0, 0xc8, 0x92, 0xd4, 0x38, 0xd7,
  24144. 0x1e, 0x57, 0xa7, 0x4b, 0xc7, 0xf8, 0xca,
  24145. 0xcc, 0x61, 0x86, 0x96, 0x50, 0x4e, 0x6a,
  24146. 0x71, 0xbd, 0x5f, 0xc6, 0x48, 0x8b, 0x6f,
  24147. 0xeb, 0x53, 0x50, 0xea, 0x35, 0x39, 0x79,
  24148. 0xcc, 0xee, 0xae, 0x81, 0x0f, 0xe2, 0xd2,
  24149. 0xbb, 0x81, 0x03, 0x8a, 0xeb, 0x98, 0xc2,
  24150. 0xad, 0xb7, 0xc0, 0x14, 0x68, 0xb9, 0x7e,
  24151. 0x8e, 0x30, 0x11, 0x43, 0x59, 0x8f, 0x04,
  24152. 0x2f, 0x7a, 0x99, 0x36, 0xd0, 0x75, 0x07,
  24153. 0x6b, 0x8e, 0xc2, 0x10, 0xac, 0xc6, 0x4d,
  24154. 0x30, 0x91, 0x9f, 0xde, 0x33, 0x0e, 0xe7,
  24155. 0xbc, 0xe5, 0x94, 0xbf, 0x80, 0xdb, 0xb7,
  24156. 0xe0, 0x8f, 0xb7, 0x74, 0xc3, 0x77, 0x18,
  24157. 0x76, 0x33, 0x34, 0xb8, 0xe4, 0x81, 0xa5,
  24158. 0xd9, 0xf7, 0x78, 0xba, 0xec, 0x62, 0x34,
  24159. 0xcb, 0x54, 0xbe, 0x90, 0xc1, 0x56, 0x59,
  24160. 0x22, 0x94, 0x5b, 0x1a, 0x8f, 0xc6, 0x8f,
  24161. 0xf2, 0x84, 0x1f, 0x61, 0x4e, 0xeb, 0x98,
  24162. 0x72, 0xdd, 0xde, 0xc9, 0xc6, 0xab, 0xd1,
  24163. 0xad, 0xd1, 0x02, 0x60, 0x82, 0x89, 0xba,
  24164. 0xf5, 0x1a, 0x6b, 0xde, 0x96, 0x19, 0x64,
  24165. 0x44, 0x0a, 0xd6, 0x27, 0x9f, 0x67, 0x96,
  24166. 0x44, 0xcc, 0x4b, 0xfd, 0x8f, 0x9e, 0xa1,
  24167. 0x1f, 0x06, 0xac, 0x2e, 0xcf, 0xdb, 0xc7,
  24168. 0x08, 0x5e, 0xe3, 0xa2, 0x59, 0xa2, 0x22,
  24169. 0xf1, 0x7a, 0xce, 0xf5, 0x30, 0x53, 0xc0,
  24170. 0xbb, 0x36, 0x13, 0x95, 0x69, 0xe2, 0x28,
  24171. 0x47, 0xad, 0xb1, 0x82, 0xfd, 0x14, 0xf6,
  24172. 0x6e, 0xed, 0x5b, 0xe0, 0xeb, 0x13, 0x5c,
  24173. 0xc6, 0x72, 0xd5, 0x2c, 0xd0, 0xae, 0xc3,
  24174. 0xad, 0xa9, 0x60, 0x2a, 0x68, 0x7b, 0x03,
  24175. 0x54, 0xc5, 0xd1, 0x71, 0xc4, 0x99, 0x48,
  24176. 0x75, 0x18, 0x1e, 0xda, 0x0a, 0x8b, 0xe1,
  24177. 0x2d, 0x67, 0x1f, 0xae, 0x91, 0xc0, 0x37,
  24178. 0x39, 0x8c, 0x7c, 0x9e, 0x42, 0xd3, 0x6e,
  24179. 0xaf, 0x7e, 0x8e, 0xa0, 0x01, 0x45, 0xc1,
  24180. 0xc6, 0xef, 0xc6, 0x2e, 0x87, 0x7b, 0x5a,
  24181. 0x60, 0xe0, 0xec, 0x3b, 0x76, 0x8e, 0xb3,
  24182. 0x0d, 0x57, 0x86, 0xa2, 0xc9, 0x1a, 0x1b,
  24183. 0x38, 0x6b, 0x75, 0x7a, 0x81, 0x8b, 0x97,
  24184. 0x99, 0xa1, 0xbb, 0xd8, 0xda, 0xdf, 0xea,
  24185. 0xef, 0xf0, 0xf9, 0x01, 0x08, 0x11, 0x18,
  24186. 0x26, 0x36, 0x4b, 0x52, 0x56, 0x68, 0x7b,
  24187. 0x98, 0xca, 0xd3, 0xd6, 0xef, 0xf6, 0x05,
  24188. 0x07, 0x09, 0x10, 0x1f, 0x21, 0x28, 0x32,
  24189. 0x39, 0x3a, 0x3c, 0x54, 0x77, 0x7b, 0x81,
  24190. 0x99, 0xa9, 0xb0, 0xb2, 0xce, 0xe5, 0xe7,
  24191. 0x1f, 0x23, 0x3f, 0x4e, 0x50, 0x5d, 0x71,
  24192. 0x7a, 0x7c, 0x91, 0xa7, 0xab, 0xae, 0xd0,
  24193. 0xd6, 0xe1, 0xe6, 0xf1, 0xf3, 0xfa, 0x00,
  24194. 0x00, 0x13, 0x24, 0x3a, 0x4e,
  24195. };
  24196. #endif
  24197. static int test_wc_dilithium(void)
  24198. {
  24199. EXPECT_DECLS;
  24200. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM)
  24201. dilithium_key* key;
  24202. byte level;
  24203. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24204. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24205. WC_RNG rng;
  24206. #endif
  24207. byte* privKey = NULL;
  24208. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24209. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24210. #endif
  24211. byte* pubKey = NULL;
  24212. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24213. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24214. #endif
  24215. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24216. ExpectNotNull(key);
  24217. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24218. DYNAMIC_TYPE_TMP_BUFFER);
  24219. ExpectNotNull(privKey);
  24220. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24221. DYNAMIC_TYPE_TMP_BUFFER);
  24222. ExpectNotNull(pubKey);
  24223. if (key != NULL) {
  24224. XMEMSET(key, 0, sizeof(*key));
  24225. }
  24226. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24227. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24228. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24229. #endif
  24230. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24231. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24232. ExpectIntEQ(wc_InitRng(&rng), 0);
  24233. #endif
  24234. ExpectIntEQ(wc_dilithium_init(NULL), BAD_FUNC_ARG);
  24235. ExpectIntEQ(wc_dilithium_init_ex(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  24236. wc_dilithium_free(NULL);
  24237. ExpectIntEQ(wc_dilithium_init(key), 0);
  24238. wc_dilithium_free(key);
  24239. ExpectIntEQ(wc_dilithium_init_ex(key, NULL, INVALID_DEVID), 0);
  24240. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24241. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  24242. BAD_FUNC_ARG);
  24243. #endif
  24244. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24245. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24246. BAD_FUNC_ARG);
  24247. #endif
  24248. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24249. ExpectIntEQ(wc_dilithium_size(NULL), BAD_FUNC_ARG);
  24250. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24251. ExpectIntEQ(wc_dilithium_priv_size(NULL), BAD_FUNC_ARG);
  24252. #endif
  24253. #endif
  24254. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24255. ExpectIntEQ(wc_dilithium_pub_size(NULL), BAD_FUNC_ARG);
  24256. #endif
  24257. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24258. ExpectIntEQ(wc_dilithium_sig_size(NULL), BAD_FUNC_ARG);
  24259. #endif
  24260. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24261. ExpectIntEQ(wc_dilithium_size(key), BAD_FUNC_ARG);
  24262. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24263. ExpectIntEQ(wc_dilithium_priv_size(key), BAD_FUNC_ARG);
  24264. #endif
  24265. #endif
  24266. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24267. ExpectIntEQ(wc_dilithium_pub_size(key), BAD_FUNC_ARG);
  24268. #endif
  24269. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24270. ExpectIntEQ(wc_dilithium_sig_size(key), BAD_FUNC_ARG);
  24271. #endif
  24272. ExpectIntEQ(wc_dilithium_set_level(NULL, 0), BAD_FUNC_ARG);
  24273. ExpectIntEQ(wc_dilithium_set_level(key, 0), BAD_FUNC_ARG);
  24274. ExpectIntEQ(wc_dilithium_set_level(NULL, WC_ML_DSA_44), BAD_FUNC_ARG);
  24275. ExpectIntEQ(wc_dilithium_set_level(key, 1), BAD_FUNC_ARG);
  24276. ExpectIntEQ(wc_dilithium_set_level(key, 4), BAD_FUNC_ARG);
  24277. ExpectIntEQ(wc_dilithium_get_level(NULL, NULL), BAD_FUNC_ARG);
  24278. ExpectIntEQ(wc_dilithium_get_level(key, NULL), BAD_FUNC_ARG);
  24279. ExpectIntEQ(wc_dilithium_get_level(NULL, &level), BAD_FUNC_ARG);
  24280. ExpectIntEQ(wc_dilithium_get_level(key, &level), BAD_FUNC_ARG);
  24281. #ifndef WOLFSSL_NO_ML_DSA_87
  24282. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24283. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24284. ExpectIntEQ(level, WC_ML_DSA_87);
  24285. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24286. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL5_KEY_SIZE);
  24287. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24288. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL5_PRV_KEY_SIZE);
  24289. #endif
  24290. #endif
  24291. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24292. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24293. #endif
  24294. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24295. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL5_SIG_SIZE);
  24296. #endif
  24297. #else
  24298. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), NOT_COMPILED_IN);
  24299. #endif
  24300. #ifndef WOLFSSL_NO_ML_DSA_65
  24301. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24302. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24303. ExpectIntEQ(level, WC_ML_DSA_65);
  24304. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24305. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL3_KEY_SIZE);
  24306. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24307. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL3_PRV_KEY_SIZE);
  24308. #endif
  24309. #endif
  24310. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24311. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24312. #endif
  24313. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24314. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL3_SIG_SIZE);
  24315. #endif
  24316. #else
  24317. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), NOT_COMPILED_IN);
  24318. #endif
  24319. #ifndef WOLFSSL_NO_ML_DSA_44
  24320. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24321. ExpectIntEQ(wc_dilithium_get_level(key, &level), 0);
  24322. ExpectIntEQ(level, WC_ML_DSA_44);
  24323. #ifdef WOLFSSL_DILITHIUM_PRIVATE_KEY
  24324. ExpectIntEQ(wc_dilithium_size(key), DILITHIUM_LEVEL2_KEY_SIZE);
  24325. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24326. ExpectIntEQ(wc_dilithium_priv_size(key), DILITHIUM_LEVEL2_PRV_KEY_SIZE);
  24327. #endif
  24328. #endif
  24329. #ifdef WOLFSSL_DILITHIUM_PUBLIC_KEY
  24330. ExpectIntEQ(wc_dilithium_pub_size(key), DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24331. #endif
  24332. #if !defined(WOLFSSL_DILITHIUM_NO_SIGN) || !defined(WOLFSSL_DILITHIUM_NO_VERIFY)
  24333. ExpectIntEQ(wc_dilithium_sig_size(key), DILITHIUM_LEVEL2_SIG_SIZE);
  24334. #endif
  24335. #else
  24336. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), NOT_COMPILED_IN);
  24337. #endif
  24338. #ifndef WOLFSSL_DILITHIUM_NO_VERIFY
  24339. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen),
  24340. BAD_FUNC_ARG);
  24341. #endif
  24342. #ifndef WOLFSSL_DILITHIUM_NO_SIGN
  24343. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24344. BAD_FUNC_ARG);
  24345. #endif
  24346. wc_dilithium_free(key);
  24347. #if !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY) || \
  24348. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24349. wc_FreeRng(&rng);
  24350. #endif
  24351. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24352. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24353. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24354. #endif
  24355. return EXPECT_RESULT();
  24356. }
  24357. static int test_wc_dilithium_make_key(void)
  24358. {
  24359. EXPECT_DECLS;
  24360. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24361. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24362. dilithium_key* key;
  24363. WC_RNG rng;
  24364. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24365. ExpectNotNull(key);
  24366. if (key != NULL) {
  24367. XMEMSET(key, 0, sizeof(*key));
  24368. }
  24369. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24370. ExpectIntEQ(wc_InitRng(&rng), 0);
  24371. ExpectIntEQ(wc_dilithium_init(key), 0);
  24372. ExpectIntEQ(wc_dilithium_make_key(key, &rng), BAD_STATE_E);
  24373. #ifndef WOLFSSL_NO_ML_DSA_44
  24374. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24375. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24376. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24377. #else
  24378. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24379. #endif
  24380. ExpectIntEQ(wc_dilithium_make_key(NULL, NULL), BAD_FUNC_ARG);
  24381. ExpectIntEQ(wc_dilithium_make_key(key, NULL), BAD_FUNC_ARG);
  24382. ExpectIntEQ(wc_dilithium_make_key(NULL, &rng), BAD_FUNC_ARG);
  24383. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24384. wc_dilithium_free(key);
  24385. wc_FreeRng(&rng);
  24386. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24387. #endif
  24388. return EXPECT_RESULT();
  24389. }
  24390. static int test_wc_dilithium_sign(void)
  24391. {
  24392. EXPECT_DECLS;
  24393. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24394. !defined(WOLFSSL_DILITHIUM_NO_SIGN)
  24395. dilithium_key* key;
  24396. dilithium_key* importKey = NULL;
  24397. WC_RNG rng;
  24398. byte* privKey = NULL;
  24399. word32 privKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24400. word32 badKeyLen;
  24401. byte msg[32];
  24402. byte* sig = NULL;
  24403. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  24404. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24405. ExpectNotNull(key);
  24406. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  24407. DYNAMIC_TYPE_TMP_BUFFER);
  24408. ExpectNotNull(importKey);
  24409. privKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24410. DYNAMIC_TYPE_TMP_BUFFER);
  24411. ExpectNotNull(privKey);
  24412. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  24413. DYNAMIC_TYPE_TMP_BUFFER);
  24414. ExpectNotNull(sig);
  24415. if (key != NULL) {
  24416. XMEMSET(key, 0, sizeof(*key));
  24417. }
  24418. if (importKey != NULL) {
  24419. XMEMSET(importKey, 0, sizeof(*importKey));
  24420. }
  24421. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24422. XMEMSET(msg, 0x55, sizeof(msg));
  24423. ExpectIntEQ(wc_InitRng(&rng), 0);
  24424. ExpectIntEQ(wc_dilithium_init(key), 0);
  24425. #ifndef WOLFSSL_NO_ML_DSA_44
  24426. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24427. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24428. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24429. #else
  24430. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24431. #endif
  24432. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  24433. #ifndef WOLFSSL_NO_ML_DSA_44
  24434. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level2_key,
  24435. sizeof_bench_dilithium_level2_key, key), 0);
  24436. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24437. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level3_key,
  24438. sizeof_bench_dilithium_level3_key, key), 0);
  24439. #else
  24440. ExpectIntEQ(wc_dilithium_import_private(bench_dilithium_level5_key,
  24441. sizeof_bench_dilithium_level5_key, key), 0);
  24442. #endif
  24443. #else
  24444. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24445. #endif
  24446. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, NULL),
  24447. BAD_FUNC_ARG);
  24448. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, NULL, NULL, NULL),
  24449. BAD_FUNC_ARG);
  24450. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, NULL, NULL, NULL),
  24451. BAD_FUNC_ARG);
  24452. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, &sigLen, NULL, NULL),
  24453. BAD_FUNC_ARG);
  24454. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, key, NULL),
  24455. BAD_FUNC_ARG);
  24456. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, NULL, NULL, NULL, &rng),
  24457. BAD_FUNC_ARG);
  24458. ExpectIntEQ(wc_dilithium_sign_msg(NULL, 32, sig, &sigLen, key, &rng),
  24459. BAD_FUNC_ARG);
  24460. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, NULL, &sigLen, key, &rng),
  24461. BAD_FUNC_ARG);
  24462. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, NULL, key, &rng),
  24463. BAD_FUNC_ARG);
  24464. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, NULL, &rng),
  24465. BAD_FUNC_ARG);
  24466. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, NULL),
  24467. BAD_FUNC_ARG);
  24468. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24469. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, NULL),
  24470. BAD_FUNC_ARG);
  24471. ExpectIntEQ(wc_dilithium_export_private(key, NULL, NULL),
  24472. BAD_FUNC_ARG);
  24473. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, NULL),
  24474. BAD_FUNC_ARG);
  24475. ExpectIntEQ(wc_dilithium_export_private(NULL, NULL, &privKeyLen),
  24476. BAD_FUNC_ARG);
  24477. ExpectIntEQ(wc_dilithium_export_private(NULL, privKey, &privKeyLen),
  24478. BAD_FUNC_ARG);
  24479. ExpectIntEQ(wc_dilithium_export_private(key, NULL, &privKeyLen),
  24480. BAD_FUNC_ARG);
  24481. ExpectIntEQ(wc_dilithium_export_private(key, privKey, NULL),
  24482. BAD_FUNC_ARG);
  24483. badKeyLen = 0;
  24484. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &badKeyLen),
  24485. BUFFER_E);
  24486. #ifndef WOLFSSL_NO_ML_DSA_44
  24487. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  24488. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24489. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  24490. #else
  24491. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  24492. #endif
  24493. ExpectIntEQ(wc_dilithium_export_private(key, privKey, &privKeyLen),
  24494. 0);
  24495. #ifndef WOLFSSL_NO_ML_DSA_44
  24496. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL2_KEY_SIZE);
  24497. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24498. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL3_KEY_SIZE);
  24499. #else
  24500. ExpectIntEQ(privKeyLen, DILITHIUM_LEVEL5_KEY_SIZE);
  24501. #endif
  24502. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  24503. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  24504. BAD_FUNC_ARG);
  24505. #ifndef WOLFSSL_NO_ML_DSA_44
  24506. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  24507. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24508. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  24509. #else
  24510. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  24511. #endif
  24512. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, NULL),
  24513. BAD_FUNC_ARG);
  24514. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, NULL),
  24515. BAD_FUNC_ARG);
  24516. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, NULL),
  24517. BAD_FUNC_ARG);
  24518. ExpectIntEQ(wc_dilithium_import_private(NULL, 0, importKey),
  24519. BAD_FUNC_ARG);
  24520. ExpectIntEQ(wc_dilithium_import_private(NULL, privKeyLen, importKey),
  24521. BAD_FUNC_ARG);
  24522. ExpectIntEQ(wc_dilithium_import_private(privKey, 0, importKey),
  24523. BAD_FUNC_ARG);
  24524. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, NULL),
  24525. BAD_FUNC_ARG);
  24526. ExpectIntEQ(wc_dilithium_import_private(privKey, privKeyLen, importKey),
  24527. 0);
  24528. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24529. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  24530. ExpectIntEQ(wc_dilithium_check_key(importKey), PUBLIC_KEY_E);
  24531. #endif
  24532. wc_dilithium_free(importKey);
  24533. wc_dilithium_free(key);
  24534. wc_FreeRng(&rng);
  24535. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24536. XFREE(privKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24537. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24538. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24539. #endif
  24540. return EXPECT_RESULT();
  24541. }
  24542. static int test_wc_dilithium_verify(void)
  24543. {
  24544. EXPECT_DECLS;
  24545. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24546. !defined(WOLFSSL_DILITHIUM_NO_VERIFY) && \
  24547. (!defined(WOLFSSL_NO_ML_DSA_44) || !defined(WOLFSSL_DILITHIUM_NO_SIGN))
  24548. dilithium_key* key;
  24549. dilithium_key* importKey = NULL;
  24550. WC_RNG rng;
  24551. byte* pubKey = NULL;
  24552. word32 pubKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24553. word32 badKeyLen;
  24554. byte msg[32];
  24555. byte* sig = NULL;
  24556. word32 sigLen = DILITHIUM_MAX_SIG_SIZE;
  24557. int res;
  24558. #ifndef WOLFSSL_NO_ML_DSA_44
  24559. byte b;
  24560. #endif
  24561. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24562. ExpectNotNull(key);
  24563. importKey = (dilithium_key*)XMALLOC(sizeof(*key), NULL,
  24564. DYNAMIC_TYPE_TMP_BUFFER);
  24565. ExpectNotNull(importKey);
  24566. pubKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24567. DYNAMIC_TYPE_TMP_BUFFER);
  24568. ExpectNotNull(pubKey);
  24569. sig = (byte*)XMALLOC(DILITHIUM_MAX_SIG_SIZE, NULL,
  24570. DYNAMIC_TYPE_TMP_BUFFER);
  24571. ExpectNotNull(sig);
  24572. if (key != NULL) {
  24573. XMEMSET(key, 0, sizeof(*key));
  24574. }
  24575. if (importKey != NULL) {
  24576. XMEMSET(importKey, 0, sizeof(*importKey));
  24577. }
  24578. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24579. XMEMSET(msg, 0x55, sizeof(msg));
  24580. ExpectIntEQ(wc_InitRng(&rng), 0);
  24581. ExpectIntEQ(wc_dilithium_init(key), 0);
  24582. #ifndef WOLFSSL_NO_ML_DSA_44
  24583. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24584. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24585. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24586. #else
  24587. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24588. #endif
  24589. #if !defined(WOLFSSL_NO_ML_DSA_44)
  24590. ExpectIntEQ(wc_dilithium_import_public(ml_dsa_44_pub_key,
  24591. (word32)sizeof(ml_dsa_44_pub_key), key), 0);
  24592. XMEMCPY(sig, ml_dsa_44_good_sig, sizeof(ml_dsa_44_good_sig));
  24593. sigLen = (word32)sizeof(ml_dsa_44_good_sig);
  24594. #else
  24595. #ifdef WOLFSSL_DILITHIUM_NO_MAKE_KEY
  24596. #ifndef WOLFSSL_NO_ML_DSA_65
  24597. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level3_pub_key,
  24598. sizeof_bench_dilithium_level3_pub_key, key), 0);
  24599. #else
  24600. ExpectIntEQ(wc_dilithium_import_public(bench_dilithium_level5_pub_key,
  24601. sizeof_bench_dilithium_level5_pub_key, key), 0);
  24602. #endif /* !WOLFSSL_NO_ML_DSA_65 */
  24603. #else
  24604. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24605. #endif /* WOLFSSL_DILITHIUM_NO_MAKE_KEY */
  24606. ExpectIntEQ(wc_dilithium_sign_msg(msg, 32, sig, &sigLen, key, &rng), 0);
  24607. #endif /* !WOLFSSL_NO_ML_DSA_44 */
  24608. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, NULL),
  24609. BAD_FUNC_ARG);
  24610. ExpectIntEQ(wc_dilithium_export_public(key, NULL, NULL),
  24611. BAD_FUNC_ARG);
  24612. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, NULL),
  24613. BAD_FUNC_ARG);
  24614. ExpectIntEQ(wc_dilithium_export_public(NULL, NULL, &pubKeyLen),
  24615. BAD_FUNC_ARG);
  24616. ExpectIntEQ(wc_dilithium_export_public(NULL, pubKey, &pubKeyLen),
  24617. BAD_FUNC_ARG);
  24618. ExpectIntEQ(wc_dilithium_export_public(key, NULL, &pubKeyLen),
  24619. BAD_FUNC_ARG);
  24620. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, NULL),
  24621. BAD_FUNC_ARG);
  24622. badKeyLen = 0;
  24623. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &badKeyLen),
  24624. BUFFER_E);
  24625. #ifndef WOLFSSL_NO_ML_DSA_44
  24626. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24627. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24628. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24629. #else
  24630. ExpectIntEQ(badKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24631. #endif
  24632. ExpectIntEQ(wc_dilithium_export_public(key, pubKey, &pubKeyLen), 0);
  24633. #ifndef WOLFSSL_NO_ML_DSA_44
  24634. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL2_PUB_KEY_SIZE);
  24635. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24636. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL3_PUB_KEY_SIZE);
  24637. #else
  24638. ExpectIntEQ(pubKeyLen, DILITHIUM_LEVEL5_PUB_KEY_SIZE);
  24639. #endif
  24640. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, NULL),
  24641. BAD_FUNC_ARG);
  24642. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, NULL, 32, NULL, NULL),
  24643. BAD_FUNC_ARG);
  24644. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, msg, 32, NULL, NULL),
  24645. BAD_FUNC_ARG);
  24646. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, &res, NULL),
  24647. BAD_FUNC_ARG);
  24648. ExpectIntEQ(wc_dilithium_verify_msg(NULL, 0, NULL, 32, NULL, key),
  24649. BAD_FUNC_ARG);
  24650. ExpectIntEQ(wc_dilithium_verify_msg(NULL, sigLen, msg, 32, &res, key),
  24651. BAD_FUNC_ARG);
  24652. ExpectIntEQ(wc_dilithium_verify_msg(sig, 0, msg, 32, &res, key),
  24653. BUFFER_E);
  24654. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, NULL, 32, &res, key),
  24655. BAD_FUNC_ARG);
  24656. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, NULL, key),
  24657. BAD_FUNC_ARG);
  24658. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, NULL),
  24659. BAD_FUNC_ARG);
  24660. res = 0;
  24661. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key), 0);
  24662. ExpectIntEQ(res, 1);
  24663. ExpectIntEQ(wc_dilithium_init(importKey), 0);
  24664. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey),
  24665. BAD_FUNC_ARG);
  24666. #ifndef WOLFSSL_NO_ML_DSA_44
  24667. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_44), 0);
  24668. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24669. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_65), 0);
  24670. #else
  24671. ExpectIntEQ(wc_dilithium_set_level(importKey, WC_ML_DSA_87), 0);
  24672. #endif
  24673. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, NULL),
  24674. BAD_FUNC_ARG);
  24675. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, NULL),
  24676. BAD_FUNC_ARG);
  24677. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, NULL),
  24678. BAD_FUNC_ARG);
  24679. ExpectIntEQ(wc_dilithium_import_public(NULL, 0, importKey),
  24680. BAD_FUNC_ARG);
  24681. ExpectIntEQ(wc_dilithium_import_public(NULL, pubKeyLen, importKey),
  24682. BAD_FUNC_ARG);
  24683. ExpectIntEQ(wc_dilithium_import_public(pubKey, 0, importKey),
  24684. BAD_FUNC_ARG);
  24685. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, NULL),
  24686. BAD_FUNC_ARG);
  24687. ExpectIntEQ(wc_dilithium_import_public(pubKey, pubKeyLen, importKey), 0);
  24688. res = 0;
  24689. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, importKey),
  24690. 0);
  24691. ExpectIntEQ(res, 1);
  24692. #ifdef WOLFSSL_DILITHIUM_CHECK_KEY
  24693. ExpectIntEQ(wc_dilithium_check_key(importKey), BAD_FUNC_ARG);
  24694. #endif
  24695. wc_dilithium_free(importKey);
  24696. #ifndef WOLFSSL_NO_ML_DSA_44
  24697. if (sig[sigLen - 5] == 0) {
  24698. /* Unused hints meant to be 0. */
  24699. sig[sigLen - 5] = 0xff;
  24700. res = 1;
  24701. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24702. SIG_VERIFY_E);
  24703. ExpectIntEQ(res, 0);
  24704. sig[sigLen - 5] = 0x00;
  24705. }
  24706. /* Last count of hints must be less than PARAMS_ML_DSA_44_OMEGA == 80 */
  24707. b = sig[sigLen - 1];
  24708. sig[sigLen - 1] = 0xff;
  24709. res = 1;
  24710. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24711. SIG_VERIFY_E);
  24712. ExpectIntEQ(res, 0);
  24713. sig[sigLen - 1] = b;
  24714. if (sig[sigLen - 4] > 1) {
  24715. /* Index must be less than previous. */
  24716. b = sig[sigLen - 84];
  24717. sig[sigLen - 84] = 0xff;
  24718. res = 1;
  24719. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key),
  24720. SIG_VERIFY_E);
  24721. ExpectIntEQ(res, 0);
  24722. sig[sigLen - 84] = b;
  24723. }
  24724. /* Mess up commit hash. */
  24725. sig[0] ^= 0x80;
  24726. res = 1;
  24727. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key), 0);
  24728. ExpectIntEQ(res, 0);
  24729. sig[0] ^= 0x80;
  24730. /* Mess up z. */
  24731. sig[100] ^= 0x80;
  24732. res = 1;
  24733. ExpectIntEQ(wc_dilithium_verify_msg(sig, sigLen, msg, 32, &res, key), 0);
  24734. ExpectIntEQ(res, 0);
  24735. sig[100] ^= 0x80;
  24736. #endif
  24737. wc_dilithium_free(key);
  24738. wc_FreeRng(&rng);
  24739. XFREE(sig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24740. XFREE(pubKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24741. XFREE(importKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24742. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24743. #endif
  24744. return EXPECT_RESULT();
  24745. }
  24746. static int test_wc_dilithium_check_key(void)
  24747. {
  24748. EXPECT_DECLS;
  24749. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24750. defined(WOLFSSL_DILITHIUM_CHECK_KEY) && \
  24751. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24752. dilithium_key* checkKey;
  24753. WC_RNG rng;
  24754. byte* privCheckKey = NULL;
  24755. word32 privCheckKeyLen = DILITHIUM_MAX_KEY_SIZE;
  24756. byte* pubCheckKey = NULL;
  24757. word32 pubCheckKeyLen = DILITHIUM_MAX_PUB_KEY_SIZE;
  24758. checkKey = (dilithium_key*)XMALLOC(sizeof(*checkKey), NULL,
  24759. DYNAMIC_TYPE_TMP_BUFFER);
  24760. ExpectNotNull(checkKey);
  24761. privCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_KEY_SIZE, NULL,
  24762. DYNAMIC_TYPE_TMP_BUFFER);
  24763. ExpectNotNull(privCheckKey);
  24764. pubCheckKey = (byte*)XMALLOC(DILITHIUM_MAX_PUB_KEY_SIZE, NULL,
  24765. DYNAMIC_TYPE_TMP_BUFFER);
  24766. ExpectNotNull(pubCheckKey);
  24767. if (checkKey != NULL) {
  24768. XMEMSET(checkKey, 0, sizeof(*checkKey));
  24769. }
  24770. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24771. ExpectIntEQ(wc_InitRng(&rng), 0);
  24772. ExpectIntEQ(wc_dilithium_check_key(NULL), BAD_FUNC_ARG);
  24773. ExpectIntEQ(wc_dilithium_init(checkKey), 0);
  24774. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey,
  24775. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24776. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24777. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL), BAD_FUNC_ARG);
  24778. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24779. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24780. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24781. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  24782. #ifndef WOLFSSL_NO_ML_DSA_44
  24783. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_44), 0);
  24784. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24785. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_65), 0);
  24786. #else
  24787. ExpectIntEQ(wc_dilithium_set_level(checkKey, WC_ML_DSA_87), 0);
  24788. #endif
  24789. ExpectIntEQ(wc_dilithium_make_key(checkKey, &rng), 0);
  24790. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL, NULL),
  24791. BAD_FUNC_ARG);
  24792. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL, NULL, NULL, NULL),
  24793. BAD_FUNC_ARG);
  24794. ExpectIntEQ(wc_dilithium_export_key(NULL, privCheckKey, NULL, NULL, NULL),
  24795. BAD_FUNC_ARG);
  24796. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, &privCheckKeyLen, NULL,
  24797. NULL), BAD_FUNC_ARG);
  24798. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, pubCheckKey, NULL),
  24799. BAD_FUNC_ARG);
  24800. ExpectIntEQ(wc_dilithium_export_key(NULL, NULL, NULL, NULL,
  24801. &pubCheckKeyLen), BAD_FUNC_ARG);
  24802. ExpectIntEQ(wc_dilithium_export_key(NULL , privCheckKey,
  24803. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24804. ExpectIntEQ(wc_dilithium_export_key(checkKey, NULL ,
  24805. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24806. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24807. NULL , pubCheckKey, &pubCheckKeyLen), BAD_FUNC_ARG);
  24808. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24809. &privCheckKeyLen, NULL , &pubCheckKeyLen), BAD_FUNC_ARG);
  24810. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24811. &privCheckKeyLen, pubCheckKey, NULL ), BAD_FUNC_ARG);
  24812. ExpectIntEQ(wc_dilithium_export_key(checkKey, privCheckKey,
  24813. &privCheckKeyLen, pubCheckKey, &pubCheckKeyLen), 0);
  24814. /* Modify hash. */
  24815. pubCheckKey[0] ^= 0x80;
  24816. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, NULL), BAD_FUNC_ARG);
  24817. ExpectIntEQ(wc_dilithium_import_key(privCheckKey, 0, NULL, 0, NULL),
  24818. BAD_FUNC_ARG);
  24819. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, pubCheckKey, 0, NULL),
  24820. BAD_FUNC_ARG);
  24821. ExpectIntEQ(wc_dilithium_import_key(NULL, 0, NULL, 0, checkKey),
  24822. BAD_FUNC_ARG);
  24823. ExpectIntEQ(wc_dilithium_import_key(NULL ,
  24824. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  24825. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24826. 0 , pubCheckKey, pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  24827. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24828. privCheckKeyLen, NULL , pubCheckKeyLen, checkKey), BAD_FUNC_ARG);
  24829. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24830. privCheckKeyLen, pubCheckKey, 0 , checkKey), BAD_FUNC_ARG);
  24831. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24832. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, NULL ), BAD_FUNC_ARG);
  24833. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24834. privCheckKeyLen, pubCheckKey, pubCheckKeyLen, checkKey), 0);
  24835. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  24836. privCheckKey[0] ^= 0x80;
  24837. /* Modify encoded t1. */
  24838. pubCheckKey[48] ^= 0x80;
  24839. ExpectIntEQ(wc_dilithium_import_key(privCheckKey,
  24840. privCheckKeyLen,pubCheckKey, pubCheckKeyLen, checkKey), 0);
  24841. ExpectIntEQ(wc_dilithium_check_key(checkKey), PUBLIC_KEY_E);
  24842. privCheckKey[48] ^= 0x80;
  24843. wc_dilithium_free(checkKey);
  24844. wc_FreeRng(&rng);
  24845. XFREE(pubCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24846. XFREE(privCheckKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24847. XFREE(checkKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24848. #endif
  24849. return EXPECT_RESULT();
  24850. }
  24851. static int test_wc_dilithium_der(void)
  24852. {
  24853. EXPECT_DECLS;
  24854. #if defined(HAVE_DILITHIUM) && defined(WOLFSSL_WC_DILITHIUM) && \
  24855. !defined(WOLFSSL_DILITHIUM_NO_ASN1) && \
  24856. !defined(WOLFSSL_DILITHIUM_NO_MAKE_KEY)
  24857. #define DILITHIUM_MAX_DER_SIZE 8192
  24858. dilithium_key* key;
  24859. WC_RNG rng;
  24860. byte* der = NULL;
  24861. int len;
  24862. int pubLen;
  24863. int pubDerLen;
  24864. int privDerLen;
  24865. int keyDerLen;
  24866. word32 idx;
  24867. #ifndef WOLFSSL_NO_ML_DSA_44
  24868. pubLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE;
  24869. pubDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + 24;
  24870. privDerLen = DILITHIUM_LEVEL2_KEY_SIZE + 30;
  24871. keyDerLen = DILITHIUM_LEVEL2_PUB_KEY_SIZE + DILITHIUM_LEVEL2_KEY_SIZE + 34;
  24872. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24873. pubLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE;
  24874. pubDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + 24;
  24875. privDerLen = DILITHIUM_LEVEL3_KEY_SIZE + 30;
  24876. keyDerLen = DILITHIUM_LEVEL3_PUB_KEY_SIZE + DILITHIUM_LEVEL3_KEY_SIZE + 34;
  24877. #else
  24878. pubLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE;
  24879. pubDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + 24;
  24880. privDerLen = DILITHIUM_LEVEL5_KEY_SIZE + 30;
  24881. keyDerLen = DILITHIUM_LEVEL5_PUB_KEY_SIZE + DILITHIUM_LEVEL5_KEY_SIZE + 34;
  24882. #endif
  24883. key = (dilithium_key*)XMALLOC(sizeof(*key), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24884. ExpectNotNull(key);
  24885. der = (byte*)XMALLOC(DILITHIUM_MAX_DER_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  24886. ExpectNotNull(der);
  24887. if (key != NULL) {
  24888. XMEMSET(key, 0, sizeof(*key));
  24889. }
  24890. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24891. ExpectIntEQ(wc_InitRng(&rng), 0);
  24892. ExpectIntEQ(wc_dilithium_init(key), 0);
  24893. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24894. 0), BAD_FUNC_ARG);
  24895. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24896. 1), BAD_FUNC_ARG);
  24897. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  24898. BAD_FUNC_ARG);
  24899. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, pubDerLen),
  24900. BAD_FUNC_ARG);
  24901. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, privDerLen),
  24902. BAD_FUNC_ARG);
  24903. #ifndef WOLFSSL_NO_ML_DSA_44
  24904. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_44), 0);
  24905. #elif !defined(WOLFSSL_NO_ML_DSA_65)
  24906. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_65), 0);
  24907. #else
  24908. ExpectIntEQ(wc_dilithium_set_level(key, WC_ML_DSA_87), 0);
  24909. #endif
  24910. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24911. 0), BAD_FUNC_ARG);
  24912. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE,
  24913. 1), BAD_FUNC_ARG);
  24914. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  24915. BAD_FUNC_ARG);
  24916. ExpectIntEQ(wc_dilithium_make_key(key, &rng), 0);
  24917. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, 0 ,
  24918. 0), BAD_FUNC_ARG);
  24919. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , 0 ,
  24920. 0), BAD_FUNC_ARG);
  24921. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE,
  24922. 0), BAD_FUNC_ARG);
  24923. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE,
  24924. 0), BAD_FUNC_ARG);
  24925. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , der , 0 ,
  24926. 0), BUFFER_E);
  24927. /* Get length only. */
  24928. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  24929. 0), pubLen);
  24930. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  24931. 0), pubLen);
  24932. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, 0 ,
  24933. 1), pubDerLen);
  24934. ExpectIntEQ(wc_Dilithium_PublicKeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE,
  24935. 1), pubDerLen);
  24936. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  24937. 0 ), BAD_FUNC_ARG);
  24938. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  24939. 0 ), BAD_FUNC_ARG);
  24940. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  24941. 0 ), BAD_FUNC_ARG);
  24942. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, NULL,
  24943. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  24944. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(NULL, der ,
  24945. DILITHIUM_MAX_DER_SIZE), BAD_FUNC_ARG);
  24946. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , der ,
  24947. 0 ), BAD_FUNC_ARG);
  24948. /* Get length only. */
  24949. ExpectIntEQ(wc_Dilithium_PrivateKeyToDer(key , NULL,
  24950. DILITHIUM_MAX_DER_SIZE), privDerLen);
  24951. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, 0 ),
  24952. BAD_FUNC_ARG);
  24953. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, 0 ),
  24954. BAD_FUNC_ARG);
  24955. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , 0 ),
  24956. BAD_FUNC_ARG);
  24957. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, NULL, DILITHIUM_MAX_DER_SIZE),
  24958. BAD_FUNC_ARG);
  24959. ExpectIntEQ(wc_Dilithium_KeyToDer(NULL, der , DILITHIUM_MAX_DER_SIZE),
  24960. BAD_FUNC_ARG);
  24961. ExpectIntEQ(wc_Dilithium_KeyToDer(key , der , 0 ),
  24962. BAD_FUNC_ARG);
  24963. /* Get length only. */
  24964. ExpectIntEQ(wc_Dilithium_KeyToDer(key , NULL, DILITHIUM_MAX_DER_SIZE),
  24965. keyDerLen);
  24966. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, 0 ),
  24967. BAD_FUNC_ARG);
  24968. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, NULL, 0 ),
  24969. BAD_FUNC_ARG);
  24970. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, NULL, 0 ),
  24971. BAD_FUNC_ARG);
  24972. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, key , 0 ),
  24973. BAD_FUNC_ARG);
  24974. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, NULL, NULL, pubDerLen),
  24975. BAD_FUNC_ARG);
  24976. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(NULL, &idx, key , pubDerLen),
  24977. BAD_FUNC_ARG);
  24978. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , NULL, key , pubDerLen),
  24979. BAD_FUNC_ARG);
  24980. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, NULL, pubDerLen),
  24981. BAD_FUNC_ARG);
  24982. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der , &idx, key , 0 ),
  24983. BAD_FUNC_ARG);
  24984. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, 0 ),
  24985. BAD_FUNC_ARG);
  24986. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, NULL, 0 ),
  24987. BAD_FUNC_ARG);
  24988. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, NULL, 0 ),
  24989. BAD_FUNC_ARG);
  24990. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, key , 0 ),
  24991. BAD_FUNC_ARG);
  24992. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, NULL, NULL, privDerLen),
  24993. BAD_FUNC_ARG);
  24994. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(NULL, &idx, key , privDerLen),
  24995. BAD_FUNC_ARG);
  24996. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , NULL, key , privDerLen),
  24997. BAD_FUNC_ARG);
  24998. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, NULL, privDerLen),
  24999. BAD_FUNC_ARG);
  25000. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der , &idx, key , 0 ),
  25001. BAD_FUNC_ARG);
  25002. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  25003. DILITHIUM_MAX_DER_SIZE, 0), pubLen);
  25004. ExpectIntEQ(wc_dilithium_import_public(der, len, key), 0);
  25005. ExpectIntEQ(len = wc_Dilithium_PublicKeyToDer(key, der,
  25006. DILITHIUM_MAX_DER_SIZE, 1), pubDerLen);
  25007. idx = 0;
  25008. ExpectIntEQ(wc_Dilithium_PublicKeyDecode(der, &idx, key, len), 0);
  25009. ExpectIntEQ(len = wc_Dilithium_PrivateKeyToDer(key, der,
  25010. DILITHIUM_MAX_DER_SIZE), privDerLen);
  25011. idx = 0;
  25012. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  25013. ExpectIntEQ(len = wc_Dilithium_KeyToDer(key, der, DILITHIUM_MAX_DER_SIZE),
  25014. keyDerLen);
  25015. idx = 0;
  25016. ExpectIntEQ(wc_Dilithium_PrivateKeyDecode(der, &idx, key, len), 0);
  25017. wc_dilithium_free(key);
  25018. wc_FreeRng(&rng);
  25019. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25020. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  25021. #endif
  25022. return EXPECT_RESULT();
  25023. }
  25024. /*
  25025. * Testing wc_SetSubjectBuffer
  25026. */
  25027. static int test_wc_SetSubjectBuffer(void)
  25028. {
  25029. EXPECT_DECLS;
  25030. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  25031. Cert cert;
  25032. XFILE file = XBADFILE;
  25033. byte* der = NULL;
  25034. word32 derSz;
  25035. derSz = FOURK_BUF;
  25036. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25037. DYNAMIC_TYPE_TMP_BUFFER));
  25038. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  25039. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  25040. if (file != XBADFILE)
  25041. XFCLOSE(file);
  25042. ExpectIntEQ(wc_InitCert(&cert), 0);
  25043. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  25044. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  25045. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25046. #endif
  25047. return EXPECT_RESULT();
  25048. } /* End test_wc_SetSubjectBuffer*/
  25049. /*
  25050. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  25051. */
  25052. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  25053. {
  25054. EXPECT_DECLS;
  25055. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25056. WC_RNG rng;
  25057. Cert cert;
  25058. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25059. RsaKey rsaKey;
  25060. int bits = 2048;
  25061. #endif
  25062. #if defined(HAVE_ECC)
  25063. ecc_key eccKey;
  25064. int ret;
  25065. #endif
  25066. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25067. ed25519_key ed25519Key;
  25068. #endif
  25069. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25070. ed448_key ed448Key;
  25071. #endif
  25072. #ifndef HAVE_FIPS
  25073. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  25074. #else
  25075. ExpectIntEQ(wc_InitRng(&rng), 0);
  25076. #endif
  25077. ExpectIntEQ(wc_InitCert(&cert), 0);
  25078. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25079. /* RSA */
  25080. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  25081. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  25082. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  25083. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  25084. 0);
  25085. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  25086. #endif
  25087. #if defined(HAVE_ECC)
  25088. /* ECC */
  25089. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  25090. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  25091. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25092. #if defined(WOLFSSL_ASYNC_CRYPT)
  25093. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25094. #endif
  25095. ExpectIntEQ(ret, 0);
  25096. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  25097. 0);
  25098. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  25099. #endif
  25100. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25101. /* ED25519 */
  25102. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  25103. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  25104. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  25105. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25106. &ed25519Key), 0);
  25107. wc_ed25519_free(&ed25519Key);
  25108. #endif
  25109. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25110. /* ED448 */
  25111. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  25112. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  25113. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  25114. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  25115. &ed448Key), 0);
  25116. wc_ed448_free(&ed448Key);
  25117. #endif
  25118. wc_FreeRng(&rng);
  25119. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25120. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  25121. return EXPECT_RESULT();
  25122. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  25123. /*
  25124. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  25125. */
  25126. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  25127. {
  25128. EXPECT_DECLS;
  25129. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  25130. WC_RNG rng;
  25131. Cert cert;
  25132. #if !defined(NO_RSA) && defined(HAVE_RSA)
  25133. RsaKey rsaKey;
  25134. int bits = 2048;
  25135. #endif
  25136. #if defined(HAVE_ECC)
  25137. ecc_key eccKey;
  25138. int ret;
  25139. #endif
  25140. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25141. ed25519_key ed25519Key;
  25142. #endif
  25143. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25144. ed448_key ed448Key;
  25145. #endif
  25146. #ifndef HAVE_FIPS
  25147. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  25148. #else
  25149. ExpectIntEQ(wc_InitRng(&rng), 0);
  25150. #endif
  25151. ExpectIntEQ(wc_InitCert(&cert), 0);
  25152. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  25153. /* RSA */
  25154. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  25155. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  25156. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  25157. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  25158. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  25159. #endif
  25160. #if defined(HAVE_ECC)
  25161. /* ECC */
  25162. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  25163. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  25164. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  25165. #if defined(WOLFSSL_ASYNC_CRYPT)
  25166. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  25167. #endif
  25168. ExpectIntEQ(ret, 0);
  25169. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  25170. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  25171. #endif
  25172. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  25173. /* ED25519 */
  25174. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  25175. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  25176. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  25177. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  25178. &ed25519Key), 0);
  25179. wc_ed25519_free(&ed25519Key);
  25180. #endif
  25181. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  25182. /* ED448 */
  25183. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  25184. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  25185. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  25186. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  25187. 0);
  25188. wc_ed448_free(&ed448Key);
  25189. #endif
  25190. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25191. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  25192. return EXPECT_RESULT();
  25193. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  25194. /*
  25195. * Testing wc_PKCS7_New()
  25196. */
  25197. static int test_wc_PKCS7_New(void)
  25198. {
  25199. EXPECT_DECLS;
  25200. #if defined(HAVE_PKCS7)
  25201. PKCS7* pkcs7 = NULL;
  25202. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  25203. wc_PKCS7_Free(pkcs7);
  25204. #endif
  25205. return EXPECT_RESULT();
  25206. } /* END test-wc_PKCS7_New */
  25207. /*
  25208. * Testing wc_PKCS7_Init()
  25209. */
  25210. static int test_wc_PKCS7_Init(void)
  25211. {
  25212. EXPECT_DECLS;
  25213. #if defined(HAVE_PKCS7)
  25214. PKCS7* pkcs7 = NULL;
  25215. void* heap = NULL;
  25216. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  25217. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25218. /* Pass in bad args. */
  25219. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  25220. wc_PKCS7_Free(pkcs7);
  25221. #endif
  25222. return EXPECT_RESULT();
  25223. } /* END test-wc_PKCS7_Init */
  25224. /*
  25225. * Testing wc_PKCS7_InitWithCert()
  25226. */
  25227. static int test_wc_PKCS7_InitWithCert(void)
  25228. {
  25229. EXPECT_DECLS;
  25230. #if defined(HAVE_PKCS7)
  25231. PKCS7* pkcs7 = NULL;
  25232. #ifndef NO_RSA
  25233. #if defined(USE_CERT_BUFFERS_2048)
  25234. unsigned char cert[sizeof(client_cert_der_2048)];
  25235. int certSz = (int)sizeof(cert);
  25236. XMEMSET(cert, 0, certSz);
  25237. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  25238. #elif defined(USE_CERT_BUFFERS_1024)
  25239. unsigned char cert[sizeof(client_cert_der_1024)];
  25240. int certSz = (int)sizeof(cert);
  25241. XMEMSET(cert, 0, certSz);
  25242. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  25243. #else
  25244. unsigned char cert[ONEK_BUF];
  25245. XFILE fp = XBADFILE;
  25246. int certSz;
  25247. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25248. XBADFILE);
  25249. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25250. fp), 0);
  25251. if (fp != XBADFILE)
  25252. XFCLOSE(fp);
  25253. #endif
  25254. #elif defined(HAVE_ECC)
  25255. #if defined(USE_CERT_BUFFERS_256)
  25256. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25257. int certSz = (int)sizeof(cert);
  25258. XMEMSET(cert, 0, certSz);
  25259. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  25260. #else
  25261. unsigned char cert[ONEK_BUF];
  25262. XFILE fp = XBADFILE;
  25263. int certSz;
  25264. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25265. XBADFILE);
  25266. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  25267. fp), 0);
  25268. if (fp != XBADFILE)
  25269. XFCLOSE(fp);
  25270. #endif
  25271. #else
  25272. #error PKCS7 requires ECC or RSA
  25273. #endif
  25274. #ifdef HAVE_ECC
  25275. {
  25276. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  25277. static unsigned char certWithInvalidEccKey[] = {
  25278. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  25279. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  25280. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  25281. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  25282. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25283. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  25284. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25285. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25286. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25287. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25288. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25289. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25290. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25291. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25292. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25293. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  25294. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  25295. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  25296. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  25297. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  25298. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  25299. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  25300. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  25301. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  25302. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  25303. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  25304. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25305. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  25306. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25307. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  25308. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  25309. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  25310. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  25311. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  25312. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  25313. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  25314. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  25315. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  25316. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  25317. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  25318. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25319. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  25320. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  25321. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  25322. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  25323. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  25324. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  25325. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  25326. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  25327. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  25328. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  25329. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  25330. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  25331. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25332. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25333. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25334. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  25335. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  25336. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  25337. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  25338. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  25339. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  25340. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  25341. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  25342. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  25343. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  25344. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  25345. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  25346. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  25347. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  25348. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  25349. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  25350. 0x08, 0xA8, 0xB4
  25351. };
  25352. #endif
  25353. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25354. /* If initialization is not successful, it's free'd in init func. */
  25355. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  25356. 0);
  25357. wc_PKCS7_Free(pkcs7);
  25358. pkcs7 = NULL;
  25359. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25360. /* Valid initialization usage. */
  25361. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25362. /* Pass in bad args. No need free for null checks, free at end.*/
  25363. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  25364. BAD_FUNC_ARG);
  25365. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  25366. BAD_FUNC_ARG);
  25367. #ifdef HAVE_ECC
  25368. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  25369. sizeof(certWithInvalidEccKey)), 0);
  25370. }
  25371. #endif
  25372. wc_PKCS7_Free(pkcs7);
  25373. #endif
  25374. return EXPECT_RESULT();
  25375. } /* END test_wc_PKCS7_InitWithCert */
  25376. /*
  25377. * Testing wc_PKCS7_EncodeData()
  25378. */
  25379. static int test_wc_PKCS7_EncodeData(void)
  25380. {
  25381. EXPECT_DECLS;
  25382. #if defined(HAVE_PKCS7)
  25383. PKCS7* pkcs7 = NULL;
  25384. byte output[FOURK_BUF];
  25385. byte data[] = "My encoded DER cert.";
  25386. #ifndef NO_RSA
  25387. #if defined(USE_CERT_BUFFERS_2048)
  25388. unsigned char cert[sizeof(client_cert_der_2048)];
  25389. unsigned char key[sizeof(client_key_der_2048)];
  25390. int certSz = (int)sizeof(cert);
  25391. int keySz = (int)sizeof(key);
  25392. XMEMSET(cert, 0, certSz);
  25393. XMEMSET(key, 0, keySz);
  25394. XMEMCPY(cert, client_cert_der_2048, certSz);
  25395. XMEMCPY(key, client_key_der_2048, keySz);
  25396. #elif defined(USE_CERT_BUFFERS_1024)
  25397. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  25398. unsigned char key[sizeof_client_key_der_1024];
  25399. int certSz = (int)sizeof(cert);
  25400. int keySz = (int)sizeof(key);
  25401. XMEMSET(cert, 0, certSz);
  25402. XMEMSET(key, 0, keySz);
  25403. XMEMCPY(cert, client_cert_der_1024, certSz);
  25404. XMEMCPY(key, client_key_der_1024, keySz);
  25405. #else
  25406. unsigned char cert[ONEK_BUF];
  25407. unsigned char key[ONEK_BUF];
  25408. XFILE fp = XBADFILE;
  25409. int certSz;
  25410. int keySz;
  25411. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25412. XBADFILE);
  25413. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25414. fp), 0);
  25415. if (fp != XBADFILE) {
  25416. XFCLOSE(fp);
  25417. fp = XBADFILE;
  25418. }
  25419. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25420. XBADFILE);
  25421. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25422. 0);
  25423. if (fp != XBADFILE)
  25424. XFCLOSE(fp);
  25425. #endif
  25426. #elif defined(HAVE_ECC)
  25427. #if defined(USE_CERT_BUFFERS_256)
  25428. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25429. unsigned char key[sizeof(ecc_clikey_der_256)];
  25430. int certSz = (int)sizeof(cert);
  25431. int keySz = (int)sizeof(key);
  25432. XMEMSET(cert, 0, certSz);
  25433. XMEMSET(key, 0, keySz);
  25434. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25435. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25436. #else
  25437. unsigned char cert[ONEK_BUF];
  25438. unsigned char key[ONEK_BUF];
  25439. XFILE fp = XBADFILE;
  25440. int certSz, keySz;
  25441. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25442. XBADFILE);
  25443. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  25444. fp), 0);
  25445. if (fp != XBADFILE) {
  25446. XFCLOSE(fp);
  25447. fp = XBADFILE;
  25448. }
  25449. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25450. XBADFILE);
  25451. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  25452. 0);
  25453. if (fp != XBADFILE)
  25454. XFCLOSE(fp);
  25455. #endif
  25456. #endif
  25457. XMEMSET(output, 0, sizeof(output));
  25458. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25459. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25460. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  25461. if (pkcs7 != NULL) {
  25462. pkcs7->content = data;
  25463. pkcs7->contentSz = sizeof(data);
  25464. pkcs7->privateKey = key;
  25465. pkcs7->privateKeySz = (word32)keySz;
  25466. }
  25467. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  25468. /* Test bad args. */
  25469. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  25470. BAD_FUNC_ARG);
  25471. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  25472. BAD_FUNC_ARG);
  25473. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  25474. wc_PKCS7_Free(pkcs7);
  25475. #endif
  25476. return EXPECT_RESULT();
  25477. } /* END test_wc_PKCS7_EncodeData */
  25478. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25479. !defined(NO_RSA) && !defined(NO_SHA256)
  25480. /* RSA sign raw digest callback */
  25481. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  25482. byte* out, word32 outSz, byte* privateKey,
  25483. word32 privateKeySz, int devid, int hashOID)
  25484. {
  25485. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  25486. byte digInfoEncoding[] = {
  25487. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  25488. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  25489. 0x00, 0x04, 0x20
  25490. };
  25491. int ret;
  25492. byte digestInfo[ONEK_BUF];
  25493. byte sig[FOURK_BUF];
  25494. word32 digestInfoSz = 0;
  25495. word32 idx = 0;
  25496. RsaKey rsa;
  25497. /* SHA-256 required only for this example callback due to above
  25498. * digInfoEncoding[] */
  25499. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  25500. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  25501. (hashOID != SHA256h)) {
  25502. return -1;
  25503. }
  25504. /* build DigestInfo */
  25505. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  25506. digestInfoSz += sizeof(digInfoEncoding);
  25507. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  25508. digestInfoSz += digestSz;
  25509. /* set up RSA key */
  25510. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  25511. if (ret != 0) {
  25512. return ret;
  25513. }
  25514. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  25515. /* sign DigestInfo */
  25516. if (ret == 0) {
  25517. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  25518. &rsa, pkcs7->rng);
  25519. if (ret > 0) {
  25520. if (ret > (int)outSz) {
  25521. /* output buffer too small */
  25522. ret = -1;
  25523. }
  25524. else {
  25525. /* success, ret holds sig size */
  25526. XMEMCPY(out, sig, ret);
  25527. }
  25528. }
  25529. }
  25530. wc_FreeRsaKey(&rsa);
  25531. return ret;
  25532. }
  25533. #endif
  25534. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  25535. typedef struct encodeSignedDataStream {
  25536. byte out[FOURK_BUF*3];
  25537. int idx;
  25538. word32 outIdx;
  25539. } encodeSignedDataStream;
  25540. /* content is 8k of partially created bundle */
  25541. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  25542. {
  25543. int ret = 0;
  25544. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  25545. if (strm->outIdx < pkcs7->contentSz) {
  25546. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  25547. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  25548. *content = strm->out + strm->outIdx;
  25549. strm->outIdx += ret;
  25550. }
  25551. (void)pkcs7;
  25552. return ret;
  25553. }
  25554. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  25555. void* ctx)
  25556. {
  25557. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  25558. XMEMCPY(strm->out + strm->idx, output, outputSz);
  25559. strm->idx += outputSz;
  25560. (void)pkcs7;
  25561. return 0;
  25562. }
  25563. #endif
  25564. /*
  25565. * Testing wc_PKCS7_EncodeSignedData()
  25566. */
  25567. static int test_wc_PKCS7_EncodeSignedData(void)
  25568. {
  25569. EXPECT_DECLS;
  25570. #if defined(HAVE_PKCS7)
  25571. PKCS7* pkcs7 = NULL;
  25572. WC_RNG rng;
  25573. byte output[FOURK_BUF];
  25574. byte badOut[1];
  25575. word32 outputSz = (word32)sizeof(output);
  25576. word32 badOutSz = 0;
  25577. byte data[] = "Test data to encode.";
  25578. #ifndef NO_RSA
  25579. #if defined(USE_CERT_BUFFERS_2048)
  25580. byte key[sizeof(client_key_der_2048)];
  25581. byte cert[sizeof(client_cert_der_2048)];
  25582. word32 keySz = (word32)sizeof(key);
  25583. word32 certSz = (word32)sizeof(cert);
  25584. XMEMSET(key, 0, keySz);
  25585. XMEMSET(cert, 0, certSz);
  25586. XMEMCPY(key, client_key_der_2048, keySz);
  25587. XMEMCPY(cert, client_cert_der_2048, certSz);
  25588. #elif defined(USE_CERT_BUFFERS_1024)
  25589. byte key[sizeof_client_key_der_1024];
  25590. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25591. word32 keySz = (word32)sizeof(key);
  25592. word32 certSz = (word32)sizeof(cert);
  25593. XMEMSET(key, 0, keySz);
  25594. XMEMSET(cert, 0, certSz);
  25595. XMEMCPY(key, client_key_der_1024, keySz);
  25596. XMEMCPY(cert, client_cert_der_1024, certSz);
  25597. #else
  25598. unsigned char cert[ONEK_BUF];
  25599. unsigned char key[ONEK_BUF];
  25600. XFILE fp = XBADFILE;
  25601. int certSz;
  25602. int keySz;
  25603. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25604. XBADFILE);
  25605. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25606. fp), 0);
  25607. if (fp != XBADFILE) {
  25608. XFCLOSE(fp);
  25609. fp = XBADFILE;
  25610. }
  25611. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25612. XBADFILE);
  25613. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25614. 0);
  25615. if (fp != XBADFILE)
  25616. XFCLOSE(fp);
  25617. #endif
  25618. #elif defined(HAVE_ECC)
  25619. #if defined(USE_CERT_BUFFERS_256)
  25620. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25621. unsigned char key[sizeof(ecc_clikey_der_256)];
  25622. int certSz = (int)sizeof(cert);
  25623. int keySz = (int)sizeof(key);
  25624. XMEMSET(cert, 0, certSz);
  25625. XMEMSET(key, 0, keySz);
  25626. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  25627. XMEMCPY(key, ecc_clikey_der_256, keySz);
  25628. #else
  25629. unsigned char cert[ONEK_BUF];
  25630. unsigned char key[ONEK_BUF];
  25631. XFILE fp = XBADFILE;
  25632. int certSz;
  25633. int keySz;
  25634. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25635. XBADFILE);
  25636. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  25637. if (fp != XBADFILE) {
  25638. XFCLOSE(fp);
  25639. fp = XBADFILE;
  25640. }
  25641. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25642. XBADFILE);
  25643. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  25644. if (fp != XBADFILE)
  25645. XFCLOSE(fp);
  25646. #endif
  25647. #endif
  25648. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25649. XMEMSET(output, 0, outputSz);
  25650. ExpectIntEQ(wc_InitRng(&rng), 0);
  25651. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25652. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25653. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25654. if (pkcs7 != NULL) {
  25655. pkcs7->content = data;
  25656. pkcs7->contentSz = (word32)sizeof(data);
  25657. pkcs7->privateKey = key;
  25658. pkcs7->privateKeySz = (word32)sizeof(key);
  25659. pkcs7->encryptOID = RSAk;
  25660. #ifdef NO_SHA
  25661. pkcs7->hashOID = SHA256h;
  25662. #else
  25663. pkcs7->hashOID = SHAh;
  25664. #endif
  25665. pkcs7->rng = &rng;
  25666. }
  25667. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25668. wc_PKCS7_Free(pkcs7);
  25669. pkcs7 = NULL;
  25670. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25671. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25672. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25673. #ifdef ASN_BER_TO_DER
  25674. wc_PKCS7_Free(pkcs7);
  25675. /* reinitialize and test setting stream mode */
  25676. {
  25677. int signedSz = 0;
  25678. encodeSignedDataStream strm;
  25679. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25680. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25681. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25682. if (pkcs7 != NULL) {
  25683. pkcs7->content = data;
  25684. pkcs7->contentSz = (word32)sizeof(data);
  25685. pkcs7->privateKey = key;
  25686. pkcs7->privateKeySz = (word32)sizeof(key);
  25687. pkcs7->encryptOID = RSAk;
  25688. #ifdef NO_SHA
  25689. pkcs7->hashOID = SHA256h;
  25690. #else
  25691. pkcs7->hashOID = SHAh;
  25692. #endif
  25693. pkcs7->rng = &rng;
  25694. }
  25695. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25696. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25697. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  25698. BAD_FUNC_ARG);
  25699. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  25700. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  25701. outputSz), 0);
  25702. wc_PKCS7_Free(pkcs7);
  25703. pkcs7 = NULL;
  25704. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25705. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25706. /* use exact signed buffer size since BER encoded */
  25707. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  25708. wc_PKCS7_Free(pkcs7);
  25709. /* now try with using callbacks for IO */
  25710. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25711. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25712. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25713. if (pkcs7 != NULL) {
  25714. pkcs7->contentSz = FOURK_BUF*2;
  25715. pkcs7->privateKey = key;
  25716. pkcs7->privateKeySz = (word32)sizeof(key);
  25717. pkcs7->encryptOID = RSAk;
  25718. #ifdef NO_SHA
  25719. pkcs7->hashOID = SHA256h;
  25720. #else
  25721. pkcs7->hashOID = SHAh;
  25722. #endif
  25723. pkcs7->rng = &rng;
  25724. }
  25725. XMEMSET(&strm, 0, sizeof(strm));
  25726. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25727. StreamOutputCB, (void*)&strm), 0);
  25728. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  25729. wc_PKCS7_Free(pkcs7);
  25730. pkcs7 = NULL;
  25731. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25732. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25733. /* use exact signed buffer size since BER encoded */
  25734. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  25735. }
  25736. #endif
  25737. #ifndef NO_PKCS7_STREAM
  25738. wc_PKCS7_Free(pkcs7);
  25739. pkcs7 = NULL;
  25740. {
  25741. word32 z;
  25742. int ret;
  25743. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25744. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25745. /* test for streaming mode */
  25746. ret = -1;
  25747. for (z = 0; z < outputSz && ret != 0; z++) {
  25748. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25749. if (ret < 0){
  25750. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25751. }
  25752. }
  25753. ExpectIntEQ(ret, 0);
  25754. ExpectIntNE(pkcs7->contentSz, 0);
  25755. ExpectNotNull(pkcs7->contentDynamic);
  25756. }
  25757. #endif /* !NO_PKCS7_STREAM */
  25758. /* Pass in bad args. */
  25759. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  25760. BAD_FUNC_ARG);
  25761. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  25762. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  25763. badOutSz), BAD_FUNC_ARG);
  25764. if (pkcs7 != NULL) {
  25765. pkcs7->hashOID = 0; /* bad hashOID */
  25766. }
  25767. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  25768. BAD_FUNC_ARG);
  25769. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  25770. !defined(NO_RSA) && !defined(NO_SHA256)
  25771. /* test RSA sign raw digest callback, if using RSA and compiled in.
  25772. * Example callback assumes SHA-256, so only run test if compiled in. */
  25773. wc_PKCS7_Free(pkcs7);
  25774. pkcs7 = NULL;
  25775. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25776. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25777. if (pkcs7 != NULL) {
  25778. pkcs7->content = data;
  25779. pkcs7->contentSz = (word32)sizeof(data);
  25780. pkcs7->privateKey = key;
  25781. pkcs7->privateKeySz = (word32)sizeof(key);
  25782. pkcs7->encryptOID = RSAk;
  25783. pkcs7->hashOID = SHA256h;
  25784. pkcs7->rng = &rng;
  25785. }
  25786. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  25787. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  25788. #endif
  25789. wc_PKCS7_Free(pkcs7);
  25790. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25791. #endif
  25792. return EXPECT_RESULT();
  25793. } /* END test_wc_PKCS7_EncodeSignedData */
  25794. /*
  25795. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  25796. */
  25797. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  25798. {
  25799. EXPECT_DECLS;
  25800. #if defined(HAVE_PKCS7)
  25801. int i;
  25802. PKCS7* pkcs7 = NULL;
  25803. WC_RNG rng;
  25804. byte outputHead[FOURK_BUF/2];
  25805. byte outputFoot[FOURK_BUF/2];
  25806. word32 outputHeadSz = (word32)sizeof(outputHead);
  25807. word32 outputFootSz = (word32)sizeof(outputFoot);
  25808. byte data[FOURK_BUF];
  25809. wc_HashAlg hash;
  25810. #ifdef NO_SHA
  25811. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25812. #else
  25813. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25814. #endif
  25815. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25816. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  25817. #ifndef NO_RSA
  25818. #if defined(USE_CERT_BUFFERS_2048)
  25819. byte key[sizeof(client_key_der_2048)];
  25820. byte cert[sizeof(client_cert_der_2048)];
  25821. word32 keySz = (word32)sizeof(key);
  25822. word32 certSz = (word32)sizeof(cert);
  25823. XMEMSET(key, 0, keySz);
  25824. XMEMSET(cert, 0, certSz);
  25825. XMEMCPY(key, client_key_der_2048, keySz);
  25826. XMEMCPY(cert, client_cert_der_2048, certSz);
  25827. #elif defined(USE_CERT_BUFFERS_1024)
  25828. byte key[sizeof_client_key_der_1024];
  25829. byte cert[sizeof(sizeof_client_cert_der_1024)];
  25830. word32 keySz = (word32)sizeof(key);
  25831. word32 certSz = (word32)sizeof(cert);
  25832. XMEMSET(key, 0, keySz);
  25833. XMEMSET(cert, 0, certSz);
  25834. XMEMCPY(key, client_key_der_1024, keySz);
  25835. XMEMCPY(cert, client_cert_der_1024, certSz);
  25836. #else
  25837. unsigned char cert[ONEK_BUF];
  25838. unsigned char key[ONEK_BUF];
  25839. XFILE fp = XBADFILE;
  25840. int certSz;
  25841. int keySz;
  25842. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  25843. XBADFILE);
  25844. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  25845. fp), 0);
  25846. if (fp != XBADFILE) {
  25847. XFCLOSE(fp);
  25848. fp = XBADFILE;
  25849. }
  25850. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  25851. XBADFILE);
  25852. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  25853. 0);
  25854. if (fp != XBADFILE)
  25855. XFCLOSE(fp);
  25856. #endif
  25857. #elif defined(HAVE_ECC)
  25858. #if defined(USE_CERT_BUFFERS_256)
  25859. unsigned char cert[sizeof(cliecc_cert_der_256)];
  25860. unsigned char key[sizeof(ecc_clikey_der_256)];
  25861. int certSz = (int)sizeof(cert);
  25862. int keySz = (int)sizeof(key);
  25863. XMEMSET(cert, 0, certSz);
  25864. XMEMSET(key, 0, keySz);
  25865. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  25866. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  25867. #else
  25868. unsigned char cert[ONEK_BUF];
  25869. unsigned char key[ONEK_BUF];
  25870. XFILE fp = XBADFILE;
  25871. int certSz;
  25872. int keySz;
  25873. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  25874. XBADFILE);
  25875. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  25876. fp), 0);
  25877. if (fp != XBADFILE) {
  25878. XFCLOSE(fp);
  25879. fp = XBADFILE;
  25880. }
  25881. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  25882. XBADFILE);
  25883. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  25884. 0);
  25885. if (fp != XBADFILE)
  25886. XFCLOSE(fp);
  25887. #endif
  25888. #endif
  25889. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25890. /* initialize large data with sequence */
  25891. for (i=0; i<(int)sizeof(data); i++)
  25892. data[i] = i & 0xff;
  25893. XMEMSET(outputHead, 0, outputHeadSz);
  25894. XMEMSET(outputFoot, 0, outputFootSz);
  25895. ExpectIntEQ(wc_InitRng(&rng), 0);
  25896. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25897. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25898. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25899. if (pkcs7 != NULL) {
  25900. pkcs7->content = NULL; /* not used for ex */
  25901. pkcs7->contentSz = (word32)sizeof(data);
  25902. pkcs7->privateKey = key;
  25903. pkcs7->privateKeySz = (word32)sizeof(key);
  25904. pkcs7->encryptOID = RSAk;
  25905. #ifdef NO_SHA
  25906. pkcs7->hashOID = SHA256h;
  25907. #else
  25908. pkcs7->hashOID = SHAh;
  25909. #endif
  25910. pkcs7->rng = &rng;
  25911. }
  25912. /* calculate hash for content */
  25913. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25914. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25915. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25916. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25917. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25918. /* Perform PKCS7 sign using hash directly */
  25919. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25920. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  25921. ExpectIntGT(outputHeadSz, 0);
  25922. ExpectIntGT(outputFootSz, 0);
  25923. wc_PKCS7_Free(pkcs7);
  25924. pkcs7 = NULL;
  25925. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25926. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25927. /* required parameter even on verify when using _ex, if using outputHead
  25928. * and outputFoot */
  25929. if (pkcs7 != NULL) {
  25930. pkcs7->contentSz = (word32)sizeof(data);
  25931. }
  25932. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25933. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  25934. wc_PKCS7_Free(pkcs7);
  25935. pkcs7 = NULL;
  25936. /* assembly complete PKCS7 sign and use normal verify */
  25937. {
  25938. byte* output = NULL;
  25939. word32 outputSz = 0;
  25940. #ifndef NO_PKCS7_STREAM
  25941. word32 z;
  25942. int ret;
  25943. #endif /* !NO_PKCS7_STREAM */
  25944. ExpectNotNull(output = (byte*)XMALLOC(
  25945. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  25946. DYNAMIC_TYPE_TMP_BUFFER));
  25947. if (output != NULL) {
  25948. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  25949. outputSz += outputHeadSz;
  25950. XMEMCPY(&output[outputSz], data, sizeof(data));
  25951. outputSz += sizeof(data);
  25952. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  25953. outputSz += outputFootSz;
  25954. }
  25955. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25956. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25957. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25958. #ifndef NO_PKCS7_STREAM
  25959. wc_PKCS7_Free(pkcs7);
  25960. pkcs7 = NULL;
  25961. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25962. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25963. /* test for streaming mode */
  25964. ret = -1;
  25965. for (z = 0; z < outputSz && ret != 0; z++) {
  25966. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25967. if (ret < 0){
  25968. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25969. }
  25970. }
  25971. ExpectIntEQ(ret, 0);
  25972. ExpectIntNE(pkcs7->contentSz, 0);
  25973. ExpectNotNull(pkcs7->contentDynamic);
  25974. wc_PKCS7_Free(pkcs7);
  25975. pkcs7 = NULL;
  25976. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25977. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25978. #endif /* !NO_PKCS7_STREAM */
  25979. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25980. }
  25981. /* Pass in bad args. */
  25982. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  25983. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25984. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  25985. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25986. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  25987. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25988. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  25989. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25990. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25991. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  25992. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25993. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  25994. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  25995. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  25996. if (pkcs7 != NULL) {
  25997. pkcs7->hashOID = 0; /* bad hashOID */
  25998. }
  25999. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  26000. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  26001. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  26002. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26003. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  26004. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26005. #ifndef NO_PKCS7_STREAM
  26006. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  26007. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  26008. #else
  26009. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  26010. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  26011. #endif
  26012. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  26013. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26014. #ifndef NO_PKCS7_STREAM
  26015. /* can pass in 0 buffer length with streaming API */
  26016. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26017. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  26018. #else
  26019. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26020. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  26021. #endif
  26022. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26023. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  26024. #ifndef NO_PKCS7_STREAM
  26025. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26026. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  26027. #else
  26028. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26029. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  26030. #endif
  26031. wc_PKCS7_Free(pkcs7);
  26032. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26033. #endif
  26034. return EXPECT_RESULT();
  26035. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  26036. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26037. /**
  26038. * Loads certs/keys from files or buffers into the argument buffers,
  26039. * helper function called by CreatePKCS7SignedData().
  26040. *
  26041. * Returns 0 on success, negative on error.
  26042. */
  26043. static int LoadPKCS7SignedDataCerts(
  26044. int useIntermediateCertChain, int pkAlgoType,
  26045. byte* intCARoot, word32* intCARootSz,
  26046. byte* intCA1, word32* intCA1Sz,
  26047. byte* intCA2, word32* intCA2Sz,
  26048. byte* cert, word32* certSz,
  26049. byte* key, word32* keySz)
  26050. {
  26051. EXPECT_DECLS;
  26052. int ret = 0;
  26053. XFILE fp = XBADFILE;
  26054. #ifndef NO_RSA
  26055. const char* intCARootRSA = "./certs/ca-cert.der";
  26056. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  26057. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  26058. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  26059. const char* intServKeyRSA = "./certs/server-key.der";
  26060. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  26061. const char* cli1024Cert = "./certs/1024/client-cert.der";
  26062. const char* cli1024Key = "./certs/1024/client-key.der";
  26063. #endif
  26064. #endif
  26065. #ifdef HAVE_ECC
  26066. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  26067. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  26068. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  26069. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  26070. const char* intServKeyECC = "./certs/ecc-key.der";
  26071. #ifndef USE_CERT_BUFFERS_256
  26072. const char* cliEccCert = "./certs/client-ecc-cert.der";
  26073. const char* cliEccKey = "./certs/client-ecc-key.der";
  26074. #endif
  26075. #endif
  26076. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  26077. ((useIntermediateCertChain == 1) &&
  26078. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  26079. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  26080. return BAD_FUNC_ARG;
  26081. }
  26082. /* Read/load certs and keys to use for signing based on PK type and chain */
  26083. switch (pkAlgoType) {
  26084. #ifndef NO_RSA
  26085. case RSA_TYPE:
  26086. if (useIntermediateCertChain == 1) {
  26087. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  26088. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  26089. if (fp != XBADFILE) {
  26090. XFCLOSE(fp);
  26091. fp = XBADFILE;
  26092. }
  26093. ExpectIntGT(*intCARootSz, 0);
  26094. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  26095. if (fp != XBADFILE) {
  26096. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  26097. XFCLOSE(fp);
  26098. fp = XBADFILE;
  26099. }
  26100. ExpectIntGT(*intCA1Sz, 0);
  26101. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  26102. if (fp != XBADFILE) {
  26103. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  26104. XFCLOSE(fp);
  26105. fp = XBADFILE;
  26106. }
  26107. ExpectIntGT(*intCA2Sz, 0);
  26108. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  26109. if (fp != XBADFILE) {
  26110. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26111. XFCLOSE(fp);
  26112. fp = XBADFILE;
  26113. }
  26114. ExpectIntGT(*certSz, 0);
  26115. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  26116. if (fp != XBADFILE) {
  26117. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26118. XFCLOSE(fp);
  26119. fp = XBADFILE;
  26120. }
  26121. ExpectIntGT(*keySz, 0);
  26122. }
  26123. else {
  26124. #if defined(USE_CERT_BUFFERS_2048)
  26125. *keySz = sizeof_client_key_der_2048;
  26126. *certSz = sizeof_client_cert_der_2048;
  26127. XMEMCPY(key, client_key_der_2048, *keySz);
  26128. XMEMCPY(cert, client_cert_der_2048, *certSz);
  26129. #elif defined(USE_CERT_BUFFERS_1024)
  26130. *keySz = sizeof_client_key_der_1024;
  26131. *certSz = sizeof_client_cert_der_1024;
  26132. XMEMCPY(key, client_key_der_1024, *keySz);
  26133. XMEMCPY(cert, client_cert_der_1024, *certSz);
  26134. #else
  26135. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  26136. if (fp != XBADFILE) {
  26137. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26138. XFCLOSE(fp);
  26139. fp = XBADFILE;
  26140. }
  26141. ExpectIntGT(*keySz, 0);
  26142. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  26143. if (fp != XBADFILE) {
  26144. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26145. XFCLOSE(fp);
  26146. fp = XBADFILE;
  26147. }
  26148. ExpectIntGT(*certSz, 0);
  26149. #endif /* USE_CERT_BUFFERS_2048 */
  26150. }
  26151. break;
  26152. #endif /* !NO_RSA */
  26153. #ifdef HAVE_ECC
  26154. case ECC_TYPE:
  26155. if (useIntermediateCertChain == 1) {
  26156. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  26157. if (fp != XBADFILE) {
  26158. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  26159. fp);
  26160. XFCLOSE(fp);
  26161. fp = XBADFILE;
  26162. }
  26163. ExpectIntGT(*intCARootSz, 0);
  26164. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  26165. if (fp != XBADFILE) {
  26166. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  26167. XFCLOSE(fp);
  26168. fp = XBADFILE;
  26169. }
  26170. ExpectIntGT(*intCA1Sz, 0);
  26171. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  26172. if (fp != XBADFILE) {
  26173. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  26174. XFCLOSE(fp);
  26175. fp = XBADFILE;
  26176. }
  26177. ExpectIntGT(*intCA2Sz, 0);
  26178. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  26179. if (fp != XBADFILE) {
  26180. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26181. XFCLOSE(fp);
  26182. fp = XBADFILE;
  26183. }
  26184. ExpectIntGT(*certSz, 0);
  26185. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  26186. if (fp != XBADFILE) {
  26187. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26188. XFCLOSE(fp);
  26189. fp = XBADFILE;
  26190. }
  26191. ExpectIntGT(*keySz, 0);
  26192. }
  26193. else {
  26194. #if defined(USE_CERT_BUFFERS_256)
  26195. *keySz = sizeof_ecc_clikey_der_256;
  26196. *certSz = sizeof_cliecc_cert_der_256;
  26197. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  26198. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  26199. #else
  26200. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  26201. if (fp != XBADFILE) {
  26202. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  26203. XFCLOSE(fp);
  26204. fp = XBADFILE;
  26205. }
  26206. ExpectIntGT(*keySz, 0);
  26207. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  26208. if (fp != XBADFILE) {
  26209. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  26210. XFCLOSE(fp);
  26211. fp = XBADFILE;
  26212. }
  26213. ExpectIntGT(*certSz, 0);
  26214. #endif /* USE_CERT_BUFFERS_256 */
  26215. }
  26216. break;
  26217. #endif /* HAVE_ECC */
  26218. default:
  26219. WOLFSSL_MSG("Unsupported SignedData PK type");
  26220. ret = BAD_FUNC_ARG;
  26221. break;
  26222. }
  26223. if (EXPECT_FAIL() && (ret == 0)) {
  26224. ret = BAD_FUNC_ARG;
  26225. }
  26226. return ret;
  26227. }
  26228. /**
  26229. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  26230. *
  26231. * output output buffer to place SignedData
  26232. * outputSz size of output buffer
  26233. * data data buffer to be signed
  26234. * dataSz size of data buffer
  26235. * withAttribs [1/0] include attributes in SignedData message
  26236. * detachedSig [1/0] create detached signature, no content
  26237. * useIntCertChain [1/0] use certificate chain and include intermediate and
  26238. * root CAs in bundle
  26239. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  26240. *
  26241. * Return size of bundle created on success, negative on error */
  26242. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  26243. byte* data, word32 dataSz,
  26244. int withAttribs, int detachedSig,
  26245. int useIntermediateCertChain,
  26246. int pkAlgoType)
  26247. {
  26248. EXPECT_DECLS;
  26249. int ret = 0;
  26250. WC_RNG rng;
  26251. PKCS7* pkcs7 = NULL;
  26252. static byte messageTypeOid[] =
  26253. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  26254. 0x09, 0x02 };
  26255. static byte messageType[] = { 0x13, 2, '1', '9' };
  26256. PKCS7Attrib attribs[] =
  26257. {
  26258. { messageTypeOid, sizeof(messageTypeOid), messageType,
  26259. sizeof(messageType) }
  26260. };
  26261. byte intCARoot[TWOK_BUF];
  26262. byte intCA1[TWOK_BUF];
  26263. byte intCA2[TWOK_BUF];
  26264. byte cert[TWOK_BUF];
  26265. byte key[TWOK_BUF];
  26266. word32 intCARootSz = sizeof(intCARoot);
  26267. word32 intCA1Sz = sizeof(intCA1);
  26268. word32 intCA2Sz = sizeof(intCA2);
  26269. word32 certSz = sizeof(cert);
  26270. word32 keySz = sizeof(key);
  26271. XMEMSET(intCARoot, 0, intCARootSz);
  26272. XMEMSET(intCA1, 0, intCA1Sz);
  26273. XMEMSET(intCA2, 0, intCA2Sz);
  26274. XMEMSET(cert, 0, certSz);
  26275. XMEMSET(key, 0, keySz);
  26276. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  26277. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  26278. cert, &certSz, key, &keySz);
  26279. ExpectIntEQ(ret, 0);
  26280. XMEMSET(output, 0, outputSz);
  26281. ExpectIntEQ(wc_InitRng(&rng), 0);
  26282. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26283. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26284. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26285. if (useIntermediateCertChain == 1) {
  26286. /* Add intermediate and root CA certs into SignedData Certs SET */
  26287. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  26288. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  26289. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  26290. }
  26291. if (pkcs7 != NULL) {
  26292. pkcs7->content = data;
  26293. pkcs7->contentSz = dataSz;
  26294. pkcs7->privateKey = key;
  26295. pkcs7->privateKeySz = (word32)sizeof(key);
  26296. if (pkAlgoType == RSA_TYPE) {
  26297. pkcs7->encryptOID = RSAk;
  26298. }
  26299. else {
  26300. pkcs7->encryptOID = ECDSAk;
  26301. }
  26302. #ifdef NO_SHA
  26303. pkcs7->hashOID = SHA256h;
  26304. #else
  26305. pkcs7->hashOID = SHAh;
  26306. #endif
  26307. pkcs7->rng = &rng;
  26308. if (withAttribs) {
  26309. /* include a signed attribute */
  26310. pkcs7->signedAttribs = attribs;
  26311. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  26312. }
  26313. }
  26314. if (detachedSig) {
  26315. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  26316. }
  26317. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  26318. ExpectIntGT(outputSz, 0);
  26319. wc_PKCS7_Free(pkcs7);
  26320. pkcs7 = NULL;
  26321. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26322. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26323. if (detachedSig && (pkcs7 != NULL)) {
  26324. pkcs7->content = data;
  26325. pkcs7->contentSz = dataSz;
  26326. }
  26327. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  26328. wc_PKCS7_Free(pkcs7);
  26329. wc_FreeRng(&rng);
  26330. if (EXPECT_FAIL()) {
  26331. outputSz = 0;
  26332. }
  26333. return outputSz;
  26334. }
  26335. #endif
  26336. /*
  26337. * Testing wc_PKCS_VerifySignedData()
  26338. */
  26339. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  26340. {
  26341. EXPECT_DECLS;
  26342. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  26343. PKCS7* pkcs7 = NULL;
  26344. byte output[6000]; /* Large size needed for bundles with int CA certs */
  26345. word32 outputSz = sizeof(output);
  26346. byte data[] = "Test data to encode.";
  26347. byte badOut[1];
  26348. word32 badOutSz = 0;
  26349. byte badContent[] = "This is different content than was signed";
  26350. wc_HashAlg hash;
  26351. #ifdef NO_SHA
  26352. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  26353. #else
  26354. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  26355. #endif
  26356. byte hashBuf[WC_MAX_DIGEST_SIZE];
  26357. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  26358. #ifndef NO_RSA
  26359. PKCS7DecodedAttrib* decodedAttrib = NULL;
  26360. /* contentType OID (1.2.840.113549.1.9.3) */
  26361. static const byte contentTypeOid[] =
  26362. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  26363. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  26364. static const byte dataType[] =
  26365. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  26366. /* messageDigest OID (1.2.840.113549.1.9.4) */
  26367. static const byte messageDigestOid[] =
  26368. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  26369. #ifndef NO_ASN_TIME
  26370. /* signingTime OID () */
  26371. static const byte signingTimeOid[] =
  26372. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  26373. #endif
  26374. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26375. int dateLength = 0;
  26376. byte dateFormat;
  26377. const byte* datePart = NULL;
  26378. struct tm timearg;
  26379. time_t now;
  26380. struct tm* nowTm = NULL;
  26381. #ifdef NEED_TMP_TIME
  26382. struct tm tmpTimeStorage;
  26383. struct tm* tmpTime = &tmpTimeStorage;
  26384. #endif
  26385. #endif /* !NO_ASN && !NO_ASN_TIME */
  26386. #ifndef NO_PKCS7_STREAM
  26387. word32 z;
  26388. int ret;
  26389. #endif /* !NO_PKCS7_STREAM */
  26390. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  26391. /* Success test with RSA certs/key */
  26392. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26393. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  26394. /* calculate hash for content, used later */
  26395. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  26396. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  26397. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  26398. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  26399. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26400. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26401. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26402. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26403. #ifndef NO_PKCS7_STREAM
  26404. wc_PKCS7_Free(pkcs7);
  26405. pkcs7 = NULL;
  26406. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26407. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26408. /* test for streaming */
  26409. ret = -1;
  26410. for (z = 0; z < outputSz && ret != 0; z++) {
  26411. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26412. if (ret < 0){
  26413. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26414. }
  26415. }
  26416. ExpectIntEQ(ret, 0);
  26417. ExpectIntNE(pkcs7->contentSz, 0);
  26418. ExpectNotNull(pkcs7->contentDynamic);
  26419. #endif /* !NO_PKCS7_STREAM */
  26420. /* Check that decoded signed attributes are correct */
  26421. /* messageDigest should be first */
  26422. if (pkcs7 != NULL) {
  26423. decodedAttrib = pkcs7->decodedAttrib;
  26424. }
  26425. ExpectNotNull(decodedAttrib);
  26426. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  26427. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  26428. decodedAttrib->oidSz), 0);
  26429. /* + 2 for OCTET STRING and length bytes */
  26430. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  26431. ExpectNotNull(decodedAttrib->value);
  26432. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  26433. #ifndef NO_ASN_TIME
  26434. /* signingTime should be second */
  26435. if (decodedAttrib != NULL) {
  26436. decodedAttrib = decodedAttrib->next;
  26437. }
  26438. ExpectNotNull(decodedAttrib);
  26439. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  26440. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  26441. decodedAttrib->oidSz), 0);
  26442. ExpectIntGT(decodedAttrib->valueSz, 0);
  26443. ExpectNotNull(decodedAttrib->value);
  26444. #endif
  26445. /* Verify signingTime if ASN and time are available */
  26446. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  26447. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  26448. &datePart, &dateFormat, &dateLength), 0);
  26449. ExpectNotNull(datePart);
  26450. ExpectIntGT(dateLength, 0);
  26451. XMEMSET(&timearg, 0, sizeof(timearg));
  26452. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  26453. &timearg), 0);
  26454. /* Get current time and compare year/month/day against attribute value */
  26455. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  26456. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  26457. ExpectNotNull(nowTm);
  26458. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  26459. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  26460. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  26461. #endif /* !NO_ASN && !NO_ASN_TIME */
  26462. /* contentType should be third */
  26463. if (decodedAttrib != NULL) {
  26464. decodedAttrib = decodedAttrib->next;
  26465. }
  26466. ExpectNotNull(decodedAttrib);
  26467. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  26468. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  26469. decodedAttrib->oidSz), 0);
  26470. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  26471. ExpectNotNull(decodedAttrib->value);
  26472. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  26473. 0);
  26474. #endif /* !NO_RSA */
  26475. /* Test bad args. */
  26476. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  26477. BAD_FUNC_ARG);
  26478. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  26479. BAD_FUNC_ARG);
  26480. #ifndef NO_PKCS7_STREAM
  26481. /* can pass in 0 buffer length with streaming API */
  26482. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26483. badOutSz), WC_PKCS7_WANT_READ_E);
  26484. #else
  26485. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  26486. badOutSz), BAD_FUNC_ARG);
  26487. #endif
  26488. wc_PKCS7_Free(pkcs7);
  26489. pkcs7 = NULL;
  26490. #ifndef NO_RSA
  26491. /* Try RSA certs/key/sig first */
  26492. outputSz = sizeof(output);
  26493. XMEMSET(output, 0, outputSz);
  26494. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26495. (word32)sizeof(data),
  26496. 1, 1, 0, RSA_TYPE)), 0);
  26497. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26498. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26499. if (pkcs7 != NULL) {
  26500. pkcs7->content = badContent;
  26501. pkcs7->contentSz = sizeof(badContent);
  26502. }
  26503. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26504. SIG_VERIFY_E);
  26505. wc_PKCS7_Free(pkcs7);
  26506. pkcs7 = NULL;
  26507. #ifndef NO_PKCS7_STREAM
  26508. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26509. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26510. if (pkcs7 != NULL) {
  26511. pkcs7->content = badContent;
  26512. pkcs7->contentSz = sizeof(badContent);
  26513. }
  26514. /* test for streaming */
  26515. ret = -1;
  26516. for (z = 0; z < outputSz && ret != 0; z++) {
  26517. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26518. if (ret == WC_PKCS7_WANT_READ_E){
  26519. continue;
  26520. }
  26521. else if (ret < 0) {
  26522. break;
  26523. }
  26524. }
  26525. ExpectIntEQ(ret, SIG_VERIFY_E);
  26526. ExpectIntNE(pkcs7->contentSz, 0);
  26527. ExpectNotNull(pkcs7->contentDynamic);
  26528. wc_PKCS7_Free(pkcs7);
  26529. pkcs7 = NULL;
  26530. #endif /* !NO_PKCS7_STREAM */
  26531. /* Test success case with detached signature and valid content */
  26532. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26533. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26534. if (pkcs7 != NULL) {
  26535. pkcs7->content = data;
  26536. pkcs7->contentSz = sizeof(data);
  26537. }
  26538. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26539. wc_PKCS7_Free(pkcs7);
  26540. pkcs7 = NULL;
  26541. #ifndef NO_PKCS7_STREAM
  26542. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26543. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26544. if (pkcs7 != NULL) {
  26545. pkcs7->content = data;
  26546. pkcs7->contentSz = sizeof(data);
  26547. }
  26548. /* test for streaming */
  26549. ret = -1;
  26550. for (z = 0; z < outputSz && ret != 0; z++) {
  26551. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26552. if (ret < 0){
  26553. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26554. }
  26555. }
  26556. ExpectIntEQ(ret, 0);
  26557. ExpectIntNE(pkcs7->contentSz, 0);
  26558. ExpectNotNull(pkcs7->contentDynamic);
  26559. wc_PKCS7_Free(pkcs7);
  26560. pkcs7 = NULL;
  26561. #endif /* !NO_PKCS7_STREAM */
  26562. /* verify using pre-computed content digest only (no content) */
  26563. {
  26564. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26565. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26566. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26567. output, outputSz, NULL, 0), 0);
  26568. wc_PKCS7_Free(pkcs7);
  26569. pkcs7 = NULL;
  26570. }
  26571. #endif /* !NO_RSA */
  26572. /* Test verify on signedData containing intermediate/root CA certs */
  26573. #ifndef NO_RSA
  26574. outputSz = sizeof(output);
  26575. XMEMSET(output, 0, outputSz);
  26576. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26577. (word32)sizeof(data),
  26578. 0, 0, 1, RSA_TYPE)), 0);
  26579. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26580. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26581. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26582. wc_PKCS7_Free(pkcs7);
  26583. pkcs7 = NULL;
  26584. #ifndef NO_PKCS7_STREAM
  26585. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26586. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26587. /* test for streaming */
  26588. ret = -1;
  26589. for (z = 0; z < outputSz && ret != 0; z++) {
  26590. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26591. if (ret < 0){
  26592. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26593. }
  26594. }
  26595. ExpectIntEQ(ret, 0);
  26596. ExpectIntNE(pkcs7->contentSz, 0);
  26597. ExpectNotNull(pkcs7->contentDynamic);
  26598. wc_PKCS7_Free(pkcs7);
  26599. pkcs7 = NULL;
  26600. #endif /* !NO_PKCS7_STREAM */
  26601. #endif /* !NO_RSA */
  26602. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  26603. !defined(NO_FILESYSTEM)
  26604. {
  26605. XFILE signedBundle = XBADFILE;
  26606. int signedBundleSz = 0;
  26607. int chunkSz = 1;
  26608. int i, rc = 0;
  26609. byte* buf = NULL;
  26610. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  26611. "rb")) != XBADFILE);
  26612. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  26613. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  26614. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  26615. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  26616. DYNAMIC_TYPE_FILE));
  26617. if (buf != NULL) {
  26618. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  26619. signedBundleSz);
  26620. }
  26621. if (signedBundle != XBADFILE) {
  26622. XFCLOSE(signedBundle);
  26623. signedBundle = XBADFILE;
  26624. }
  26625. if (buf != NULL) {
  26626. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26627. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26628. for (i = 0; i < signedBundleSz;) {
  26629. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  26630. chunkSz;
  26631. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  26632. if (rc < 0 ) {
  26633. if (rc == WC_PKCS7_WANT_READ_E) {
  26634. i += sz;
  26635. continue;
  26636. }
  26637. break;
  26638. }
  26639. else {
  26640. break;
  26641. }
  26642. }
  26643. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  26644. wc_PKCS7_Free(pkcs7);
  26645. pkcs7 = NULL;
  26646. }
  26647. /* now try with malformed bundle */
  26648. if (buf != NULL) {
  26649. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26650. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26651. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  26652. for (i = 0; i < signedBundleSz;) {
  26653. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  26654. chunkSz;
  26655. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  26656. if (rc < 0 ) {
  26657. if (rc == WC_PKCS7_WANT_READ_E) {
  26658. i += sz;
  26659. continue;
  26660. }
  26661. break;
  26662. }
  26663. else {
  26664. break;
  26665. }
  26666. }
  26667. ExpectIntEQ(rc, ASN_PARSE_E);
  26668. wc_PKCS7_Free(pkcs7);
  26669. pkcs7 = NULL;
  26670. }
  26671. if (buf != NULL)
  26672. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  26673. }
  26674. #endif /* BER and stream */
  26675. #endif
  26676. return EXPECT_RESULT();
  26677. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  26678. /*
  26679. * Testing wc_PKCS_VerifySignedData()
  26680. */
  26681. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  26682. {
  26683. EXPECT_DECLS;
  26684. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  26685. PKCS7* pkcs7 = NULL;
  26686. byte output[6000]; /* Large size needed for bundles with int CA certs */
  26687. word32 outputSz = sizeof(output);
  26688. byte data[] = "Test data to encode.";
  26689. byte badContent[] = "This is different content than was signed";
  26690. wc_HashAlg hash;
  26691. #ifndef NO_PKCS7_STREAM
  26692. word32 z;
  26693. int ret;
  26694. #endif /* !NO_PKCS7_STREAM */
  26695. #ifdef NO_SHA
  26696. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  26697. #else
  26698. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  26699. #endif
  26700. byte hashBuf[WC_MAX_DIGEST_SIZE];
  26701. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  26702. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  26703. /* Success test with ECC certs/key */
  26704. outputSz = sizeof(output);
  26705. XMEMSET(output, 0, outputSz);
  26706. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26707. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  26708. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26709. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26710. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26711. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26712. wc_PKCS7_Free(pkcs7);
  26713. pkcs7 = NULL;
  26714. #ifndef NO_PKCS7_STREAM
  26715. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26716. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26717. /* test for streaming */
  26718. ret = -1;
  26719. for (z = 0; z < outputSz && ret != 0; z++) {
  26720. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26721. if (ret < 0){
  26722. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26723. }
  26724. }
  26725. ExpectIntEQ(ret, 0);
  26726. ExpectIntNE(pkcs7->contentSz, 0);
  26727. ExpectNotNull(pkcs7->contentDynamic);
  26728. wc_PKCS7_Free(pkcs7);
  26729. pkcs7 = NULL;
  26730. #endif /* !NO_PKCS7_STREAM */
  26731. /* Invalid content should error, use detached signature so we can
  26732. * easily change content */
  26733. outputSz = sizeof(output);
  26734. XMEMSET(output, 0, outputSz);
  26735. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26736. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  26737. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26738. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26739. if (pkcs7 != NULL) {
  26740. pkcs7->content = badContent;
  26741. pkcs7->contentSz = sizeof(badContent);
  26742. }
  26743. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  26744. SIG_VERIFY_E);
  26745. wc_PKCS7_Free(pkcs7);
  26746. pkcs7 = NULL;
  26747. #ifndef NO_PKCS7_STREAM
  26748. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26749. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26750. if (pkcs7 != NULL) {
  26751. pkcs7->content = badContent;
  26752. pkcs7->contentSz = sizeof(badContent);
  26753. }
  26754. /* test for streaming */
  26755. ret = -1;
  26756. for (z = 0; z < outputSz && ret != 0; z++) {
  26757. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26758. if (ret == WC_PKCS7_WANT_READ_E){
  26759. continue;
  26760. }
  26761. else if (ret < 0) {
  26762. break;
  26763. }
  26764. }
  26765. ExpectIntEQ(ret, SIG_VERIFY_E);
  26766. ExpectIntNE(pkcs7->contentSz, 0);
  26767. ExpectNotNull(pkcs7->contentDynamic);
  26768. wc_PKCS7_Free(pkcs7);
  26769. pkcs7 = NULL;
  26770. #endif /* !NO_PKCS7_STREAM */
  26771. /* Test success case with detached signature and valid content */
  26772. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26773. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26774. if (pkcs7 != NULL) {
  26775. pkcs7->content = data;
  26776. pkcs7->contentSz = sizeof(data);
  26777. }
  26778. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26779. wc_PKCS7_Free(pkcs7);
  26780. pkcs7 = NULL;
  26781. #ifndef NO_PKCS7_STREAM
  26782. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26783. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26784. if (pkcs7 != NULL) {
  26785. pkcs7->content = data;
  26786. pkcs7->contentSz = sizeof(data);
  26787. }
  26788. /* test for streaming */
  26789. ret = -1;
  26790. for (z = 0; z < outputSz && ret != 0; z++) {
  26791. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26792. if (ret < 0){
  26793. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26794. }
  26795. }
  26796. ExpectIntEQ(ret, 0);
  26797. ExpectIntNE(pkcs7->contentSz, 0);
  26798. ExpectNotNull(pkcs7->contentDynamic);
  26799. wc_PKCS7_Free(pkcs7);
  26800. pkcs7 = NULL;
  26801. #endif /* !NO_PKCS7_STREAM */
  26802. /* verify using pre-computed content digest only (no content) */
  26803. {
  26804. /* calculate hash for content */
  26805. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  26806. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  26807. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  26808. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  26809. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26810. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  26811. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  26812. output, outputSz, NULL, 0), 0);
  26813. wc_PKCS7_Free(pkcs7);
  26814. pkcs7 = NULL;
  26815. }
  26816. /* Test verify on signedData containing intermediate/root CA certs */
  26817. outputSz = sizeof(output);
  26818. XMEMSET(output, 0, outputSz);
  26819. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  26820. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  26821. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26822. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26823. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  26824. wc_PKCS7_Free(pkcs7);
  26825. pkcs7 = NULL;
  26826. #ifndef NO_PKCS7_STREAM
  26827. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26828. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26829. /* test for streaming */
  26830. ret = -1;
  26831. for (z = 0; z < outputSz && ret != 0; z++) {
  26832. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  26833. if (ret < 0){
  26834. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26835. }
  26836. }
  26837. ExpectIntEQ(ret, 0);
  26838. ExpectIntNE(pkcs7->contentSz, 0);
  26839. ExpectNotNull(pkcs7->contentDynamic);
  26840. wc_PKCS7_Free(pkcs7);
  26841. pkcs7 = NULL;
  26842. #endif /* !NO_PKCS7_STREAM */
  26843. #endif
  26844. return EXPECT_RESULT();
  26845. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  26846. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  26847. !defined(NO_AES_256)
  26848. static const byte defKey[] = {
  26849. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26850. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26851. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  26852. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  26853. };
  26854. static byte aesHandle[32]; /* simulated hardware key handle */
  26855. /* return 0 on success */
  26856. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  26857. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  26858. byte* in, int inSz, byte* out, void* usrCtx)
  26859. {
  26860. int ret;
  26861. Aes aes;
  26862. if (usrCtx == NULL) {
  26863. /* no simulated handle passed in */
  26864. return -1;
  26865. }
  26866. switch (encryptOID) {
  26867. case AES256CBCb:
  26868. if (ivSz != AES_BLOCK_SIZE)
  26869. return BAD_FUNC_ARG;
  26870. break;
  26871. default:
  26872. WOLFSSL_MSG("Unsupported content cipher type for test");
  26873. return ALGO_ID_E;
  26874. };
  26875. /* simulate using handle to get key */
  26876. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  26877. if (ret == 0) {
  26878. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  26879. if (ret == 0)
  26880. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  26881. wc_AesFree(&aes);
  26882. }
  26883. (void)aad;
  26884. (void)aadSz;
  26885. (void)authTag;
  26886. (void)authTagSz;
  26887. (void)pkcs7;
  26888. return ret;
  26889. }
  26890. /* returns key size on success */
  26891. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  26892. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  26893. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  26894. {
  26895. int ret = -1;
  26896. (void)cekSz;
  26897. (void)cek;
  26898. (void)outSz;
  26899. (void)keyIdSz;
  26900. (void)direction;
  26901. (void)orginKey; /* used with KAKRI */
  26902. (void)orginKeySz;
  26903. if (out == NULL)
  26904. return BAD_FUNC_ARG;
  26905. if (keyId[0] != 0x00) {
  26906. return -1;
  26907. }
  26908. if (type != (int)PKCS7_KEKRI) {
  26909. return -1;
  26910. }
  26911. switch (keyWrapAlgo) {
  26912. case AES256_WRAP:
  26913. /* simulate setting a handle for later decryption but use key
  26914. * as handle in the test case here */
  26915. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  26916. aesHandle, sizeof(aesHandle), NULL);
  26917. if (ret < 0)
  26918. return ret;
  26919. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  26920. if (ret < 0)
  26921. return ret;
  26922. /* return key size on success */
  26923. return sizeof(defKey);
  26924. default:
  26925. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  26926. return BAD_KEYWRAP_ALG_E;
  26927. };
  26928. }
  26929. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  26930. /*
  26931. * Testing wc_PKCS7_EncodeEnvelopedData()
  26932. */
  26933. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  26934. {
  26935. EXPECT_DECLS;
  26936. #if defined(HAVE_PKCS7)
  26937. PKCS7* pkcs7 = NULL;
  26938. #ifdef ASN_BER_TO_DER
  26939. int encodedSz = 0;
  26940. #endif
  26941. #ifdef ECC_TIMING_RESISTANT
  26942. WC_RNG rng;
  26943. #endif
  26944. word32 tempWrd32 = 0;
  26945. byte* tmpBytePtr = NULL;
  26946. const char input[] = "Test data to encode.";
  26947. int i;
  26948. int testSz = 0;
  26949. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  26950. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26951. byte* rsaCert = NULL;
  26952. byte* rsaPrivKey = NULL;
  26953. word32 rsaCertSz;
  26954. word32 rsaPrivKeySz;
  26955. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  26956. !defined(USE_CERT_BUFFERS_2048) )
  26957. static const char* rsaClientCert = "./certs/client-cert.der";
  26958. static const char* rsaClientKey = "./certs/client-key.der";
  26959. rsaCertSz = (word32)sizeof(rsaClientCert);
  26960. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  26961. #endif
  26962. #endif
  26963. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26964. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26965. byte* eccCert = NULL;
  26966. byte* eccPrivKey = NULL;
  26967. word32 eccCertSz;
  26968. word32 eccPrivKeySz;
  26969. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  26970. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  26971. static const char* eccClientKey = "./certs/ecc-client-key.der";
  26972. #endif
  26973. #endif
  26974. /* Generic buffer size. */
  26975. byte output[ONEK_BUF];
  26976. byte decoded[sizeof(input)/sizeof(char)];
  26977. int decodedSz = 0;
  26978. #ifndef NO_FILESYSTEM
  26979. XFILE certFile = XBADFILE;
  26980. XFILE keyFile = XBADFILE;
  26981. #endif
  26982. #ifdef ECC_TIMING_RESISTANT
  26983. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26984. #endif
  26985. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  26986. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  26987. /* RSA certs and keys. */
  26988. #if defined(USE_CERT_BUFFERS_1024)
  26989. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  26990. /* Allocate buffer space. */
  26991. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  26992. DYNAMIC_TYPE_TMP_BUFFER));
  26993. /* Init buffer. */
  26994. if (rsaCert != NULL) {
  26995. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  26996. }
  26997. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  26998. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  26999. DYNAMIC_TYPE_TMP_BUFFER));
  27000. if (rsaPrivKey != NULL) {
  27001. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  27002. }
  27003. #elif defined(USE_CERT_BUFFERS_2048)
  27004. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  27005. /* Allocate buffer */
  27006. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  27007. DYNAMIC_TYPE_TMP_BUFFER));
  27008. /* Init buffer. */
  27009. if (rsaCert != NULL) {
  27010. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  27011. }
  27012. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  27013. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  27014. DYNAMIC_TYPE_TMP_BUFFER));
  27015. if (rsaPrivKey != NULL) {
  27016. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  27017. }
  27018. #else
  27019. /* File system. */
  27020. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  27021. rsaCertSz = (word32)FOURK_BUF;
  27022. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27023. DYNAMIC_TYPE_TMP_BUFFER));
  27024. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  27025. certFile)) > 0);
  27026. if (certFile != XBADFILE)
  27027. XFCLOSE(certFile);
  27028. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  27029. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27030. DYNAMIC_TYPE_TMP_BUFFER));
  27031. rsaPrivKeySz = (word32)FOURK_BUF;
  27032. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  27033. keyFile)) > 0);
  27034. if (keyFile != XBADFILE)
  27035. XFCLOSE(keyFile);
  27036. #endif /* USE_CERT_BUFFERS */
  27037. #endif /* NO_RSA */
  27038. /* ECC */
  27039. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  27040. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  27041. #ifdef USE_CERT_BUFFERS_256
  27042. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  27043. DYNAMIC_TYPE_TMP_BUFFER));
  27044. /* Init buffer. */
  27045. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  27046. if (eccCert != NULL) {
  27047. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  27048. }
  27049. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  27050. DYNAMIC_TYPE_TMP_BUFFER));
  27051. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  27052. if (eccPrivKey != NULL) {
  27053. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  27054. }
  27055. #else /* File system. */
  27056. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  27057. eccCertSz = (word32)FOURK_BUF;
  27058. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27059. DYNAMIC_TYPE_TMP_BUFFER));
  27060. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  27061. certFile)) > 0);
  27062. if (certFile != XBADFILE) {
  27063. XFCLOSE(certFile);
  27064. }
  27065. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  27066. eccPrivKeySz = (word32)FOURK_BUF;
  27067. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27068. DYNAMIC_TYPE_TMP_BUFFER));
  27069. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  27070. keyFile)) > 0);
  27071. if (keyFile != XBADFILE) {
  27072. XFCLOSE(keyFile);
  27073. }
  27074. #endif /* USE_CERT_BUFFERS_256 */
  27075. #endif /* END HAVE_ECC */
  27076. #ifndef NO_FILESYSTEM
  27077. /* Silence. */
  27078. (void)keyFile;
  27079. (void)certFile;
  27080. #endif
  27081. {
  27082. const pkcs7EnvelopedVector testVectors[] = {
  27083. /* DATA is a global variable defined in the makefile. */
  27084. #if !defined(NO_RSA)
  27085. #ifndef NO_DES3
  27086. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  27087. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27088. #endif /* NO_DES3 */
  27089. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27090. #ifndef NO_AES_128
  27091. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  27092. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27093. #endif
  27094. #ifndef NO_AES_192
  27095. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  27096. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27097. #endif
  27098. #ifndef NO_AES_256
  27099. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  27100. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  27101. #endif
  27102. #endif /* NO_AES && HAVE_AES_CBC */
  27103. #endif /* NO_RSA */
  27104. #if defined(HAVE_ECC)
  27105. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27106. #if !defined(NO_SHA) && !defined(NO_AES_128)
  27107. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27108. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  27109. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27110. #endif
  27111. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  27112. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27113. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  27114. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27115. #endif
  27116. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  27117. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  27118. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  27119. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  27120. #endif
  27121. #endif /* NO_AES && HAVE_AES_CBC*/
  27122. #endif /* END HAVE_ECC */
  27123. }; /* END pkcs7EnvelopedVector */
  27124. #ifdef ECC_TIMING_RESISTANT
  27125. ExpectIntEQ(wc_InitRng(&rng), 0);
  27126. #endif
  27127. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27128. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27129. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  27130. for (i = 0; i < testSz; i++) {
  27131. #ifdef ASN_BER_TO_DER
  27132. encodeSignedDataStream strm;
  27133. /* test setting stream mode, the first one using IO callbacks */
  27134. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  27135. (word32)(testVectors + i)->certSz), 0);
  27136. if (pkcs7 != NULL) {
  27137. #ifdef ECC_TIMING_RESISTANT
  27138. pkcs7->rng = &rng;
  27139. #endif
  27140. if (i != 0)
  27141. pkcs7->content = (byte*)(testVectors + i)->content;
  27142. pkcs7->contentSz = (testVectors + i)->contentSz;
  27143. pkcs7->contentOID = (testVectors + i)->contentOID;
  27144. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  27145. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  27146. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  27147. pkcs7->privateKey = (testVectors + i)->privateKey;
  27148. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  27149. }
  27150. if (i == 0) {
  27151. XMEMSET(&strm, 0, sizeof(strm));
  27152. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  27153. StreamOutputCB, (void*)&strm), 0);
  27154. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  27155. }
  27156. else {
  27157. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  27158. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27159. (word32)sizeof(output));
  27160. }
  27161. switch ((testVectors + i)->encryptOID) {
  27162. #ifndef NO_DES3
  27163. case DES3b:
  27164. case DESb:
  27165. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27166. break;
  27167. #endif
  27168. #ifdef HAVE_AESCCM
  27169. #ifdef WOLFSSL_AES_128
  27170. case AES128CCMb:
  27171. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27172. break;
  27173. #endif
  27174. #ifdef WOLFSSL_AES_192
  27175. case AES192CCMb:
  27176. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27177. break;
  27178. #endif
  27179. #ifdef WOLFSSL_AES_256
  27180. case AES256CCMb:
  27181. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  27182. break;
  27183. #endif
  27184. #endif
  27185. default:
  27186. ExpectIntGE(encodedSz, 0);
  27187. }
  27188. if (encodedSz > 0) {
  27189. if (i == 0) {
  27190. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  27191. strm.out, (word32)encodedSz, decoded,
  27192. (word32)sizeof(decoded));
  27193. }
  27194. else {
  27195. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27196. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  27197. }
  27198. ExpectIntGE(decodedSz, 0);
  27199. /* Verify the size of each buffer. */
  27200. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  27201. }
  27202. wc_PKCS7_Free(pkcs7);
  27203. pkcs7 = NULL;
  27204. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27205. #endif
  27206. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  27207. (word32)(testVectors + i)->certSz), 0);
  27208. if (pkcs7 != NULL) {
  27209. #ifdef ECC_TIMING_RESISTANT
  27210. pkcs7->rng = &rng;
  27211. #endif
  27212. pkcs7->content = (byte*)(testVectors + i)->content;
  27213. pkcs7->contentSz = (testVectors + i)->contentSz;
  27214. pkcs7->contentOID = (testVectors + i)->contentOID;
  27215. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  27216. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  27217. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  27218. pkcs7->privateKey = (testVectors + i)->privateKey;
  27219. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  27220. }
  27221. #ifdef ASN_BER_TO_DER
  27222. /* test without setting stream mode */
  27223. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  27224. #endif
  27225. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27226. (word32)sizeof(output)), 0);
  27227. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27228. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  27229. ExpectIntGE(decodedSz, 0);
  27230. /* Verify the size of each buffer. */
  27231. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  27232. /* Don't free the last time through the loop. */
  27233. if (i < testSz - 1) {
  27234. wc_PKCS7_Free(pkcs7);
  27235. pkcs7 = NULL;
  27236. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27237. }
  27238. } /* END test loop. */
  27239. }
  27240. /* Test bad args. */
  27241. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  27242. (word32)sizeof(output)), BAD_FUNC_ARG);
  27243. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  27244. (word32)sizeof(output)), BAD_FUNC_ARG);
  27245. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  27246. /* Decode. */
  27247. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  27248. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27249. BAD_FUNC_ARG);
  27250. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27251. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  27252. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27253. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  27254. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  27255. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27256. BAD_FUNC_ARG);
  27257. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  27258. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  27259. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  27260. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  27261. /* only a failure for KARI test cases */
  27262. if (pkcs7 != NULL) {
  27263. tempWrd32 = pkcs7->singleCertSz;
  27264. pkcs7->singleCertSz = 0;
  27265. }
  27266. #if defined(WOLFSSL_ASN_TEMPLATE)
  27267. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27268. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27269. BUFFER_E);
  27270. #else
  27271. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27272. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27273. ASN_PARSE_E);
  27274. #endif
  27275. if (pkcs7 != NULL) {
  27276. pkcs7->singleCertSz = tempWrd32;
  27277. tmpBytePtr = pkcs7->singleCert;
  27278. pkcs7->singleCert = NULL;
  27279. }
  27280. #ifndef NO_RSA
  27281. #if defined(NO_PKCS7_STREAM)
  27282. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  27283. * is returned from kari parse which gets set to bad func arg */
  27284. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27285. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27286. BAD_FUNC_ARG);
  27287. #else
  27288. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27289. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27290. ASN_PARSE_E);
  27291. #endif
  27292. #endif /* !NO_RSA */
  27293. if (pkcs7 != NULL) {
  27294. pkcs7->singleCert = tmpBytePtr;
  27295. }
  27296. #endif
  27297. if (pkcs7 != NULL) {
  27298. tempWrd32 = pkcs7->privateKeySz;
  27299. pkcs7->privateKeySz = 0;
  27300. }
  27301. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27302. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27303. BAD_FUNC_ARG);
  27304. if (pkcs7 != NULL) {
  27305. pkcs7->privateKeySz = tempWrd32;
  27306. tmpBytePtr = pkcs7->privateKey;
  27307. pkcs7->privateKey = NULL;
  27308. }
  27309. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27310. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  27311. BAD_FUNC_ARG);
  27312. if (pkcs7 != NULL) {
  27313. pkcs7->privateKey = tmpBytePtr;
  27314. }
  27315. wc_PKCS7_Free(pkcs7);
  27316. pkcs7 = NULL;
  27317. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  27318. /* test of decrypt callback with KEKRI enveloped data */
  27319. {
  27320. int envelopedSz = 0;
  27321. const byte keyId[] = { 0x00 };
  27322. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27323. if (pkcs7 != NULL) {
  27324. pkcs7->content = (byte*)input;
  27325. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  27326. pkcs7->contentOID = DATA;
  27327. pkcs7->encryptOID = AES256CBCb;
  27328. }
  27329. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  27330. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  27331. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  27332. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  27333. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  27334. (word32)sizeof(output))), 0);
  27335. wc_PKCS7_Free(pkcs7);
  27336. pkcs7 = NULL;
  27337. /* decode envelopedData */
  27338. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27339. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  27340. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  27341. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  27342. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  27343. wc_PKCS7_Free(pkcs7);
  27344. pkcs7 = NULL;
  27345. }
  27346. #endif /* !NO_AES && !NO_AES_256 */
  27347. #ifndef NO_RSA
  27348. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27349. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27350. #endif /* NO_RSA */
  27351. #ifdef HAVE_ECC
  27352. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27353. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27354. #endif /* HAVE_ECC */
  27355. #ifdef ECC_TIMING_RESISTANT
  27356. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  27357. #endif
  27358. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  27359. !defined(NO_RSA) && !defined(NO_SHA)
  27360. {
  27361. byte out[7];
  27362. byte *cms = NULL;
  27363. word32 cmsSz;
  27364. XFILE cmsFile = XBADFILE;
  27365. XMEMSET(out, 0, sizeof(out));
  27366. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27367. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  27368. != XBADFILE);
  27369. cmsSz = (word32)FOURK_BUF;
  27370. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  27371. DYNAMIC_TYPE_TMP_BUFFER));
  27372. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  27373. if (cmsFile != XBADFILE)
  27374. XFCLOSE(cmsFile);
  27375. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  27376. sizeof_client_cert_der_2048), 0);
  27377. if (pkcs7 != NULL) {
  27378. pkcs7->privateKey = (byte*)client_key_der_2048;
  27379. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  27380. }
  27381. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  27382. 2), 0);
  27383. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  27384. sizeof(out)), 0);
  27385. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27386. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  27387. wc_PKCS7_Free(pkcs7);
  27388. pkcs7 = NULL;
  27389. }
  27390. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  27391. #endif /* HAVE_PKCS7 */
  27392. return EXPECT_RESULT();
  27393. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  27394. /*
  27395. * Testing wc_PKCS7_EncodeEncryptedData()
  27396. */
  27397. static int test_wc_PKCS7_EncodeEncryptedData(void)
  27398. {
  27399. EXPECT_DECLS;
  27400. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  27401. PKCS7* pkcs7 = NULL;
  27402. byte* tmpBytePtr = NULL;
  27403. byte encrypted[TWOK_BUF];
  27404. byte decoded[TWOK_BUF];
  27405. word32 tmpWrd32 = 0;
  27406. int tmpInt = 0;
  27407. int decodedSz = 0;
  27408. int encryptedSz = 0;
  27409. int testSz = 0;
  27410. int i = 0;
  27411. const byte data[] = { /* Hello World */
  27412. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  27413. 0x72,0x6c,0x64
  27414. };
  27415. #ifndef NO_DES3
  27416. byte desKey[] = {
  27417. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  27418. };
  27419. byte des3Key[] = {
  27420. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  27421. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  27422. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  27423. };
  27424. #endif
  27425. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27426. #ifndef NO_AES_128
  27427. byte aes128Key[] = {
  27428. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27429. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27430. };
  27431. #endif
  27432. #ifndef NO_AES_192
  27433. byte aes192Key[] = {
  27434. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27435. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27436. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27437. };
  27438. #endif
  27439. #ifndef NO_AES_256
  27440. byte aes256Key[] = {
  27441. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27442. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27443. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  27444. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  27445. };
  27446. #endif
  27447. #endif /* !NO_AES && HAVE_AES_CBC */
  27448. const pkcs7EncryptedVector testVectors[] =
  27449. {
  27450. #ifndef NO_DES3
  27451. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  27452. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  27453. #endif /* !NO_DES3 */
  27454. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  27455. #ifndef NO_AES_128
  27456. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  27457. sizeof(aes128Key)},
  27458. #endif
  27459. #ifndef NO_AES_192
  27460. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  27461. sizeof(aes192Key)},
  27462. #endif
  27463. #ifndef NO_AES_256
  27464. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  27465. sizeof(aes256Key)},
  27466. #endif
  27467. #endif /* !NO_AES && HAVE_AES_CBC */
  27468. };
  27469. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  27470. for (i = 0; i < testSz; i++) {
  27471. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27472. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27473. if (pkcs7 != NULL) {
  27474. pkcs7->content = (byte*)testVectors[i].content;
  27475. pkcs7->contentSz = testVectors[i].contentSz;
  27476. pkcs7->contentOID = testVectors[i].contentOID;
  27477. pkcs7->encryptOID = testVectors[i].encryptOID;
  27478. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  27479. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  27480. pkcs7->heap = HEAP_HINT;
  27481. }
  27482. /* encode encryptedData */
  27483. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27484. sizeof(encrypted)), 0);
  27485. /* Decode encryptedData */
  27486. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  27487. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  27488. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  27489. /* Keep values for last itr. */
  27490. if (i < testSz - 1) {
  27491. wc_PKCS7_Free(pkcs7);
  27492. pkcs7 = NULL;
  27493. }
  27494. }
  27495. if (pkcs7 == NULL || testSz == 0) {
  27496. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27497. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  27498. }
  27499. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  27500. sizeof(encrypted)),BAD_FUNC_ARG);
  27501. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  27502. sizeof(encrypted)), BAD_FUNC_ARG);
  27503. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27504. 0), BAD_FUNC_ARG);
  27505. /* Testing the struct. */
  27506. if (pkcs7 != NULL) {
  27507. tmpBytePtr = pkcs7->content;
  27508. pkcs7->content = NULL;
  27509. }
  27510. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27511. sizeof(encrypted)), BAD_FUNC_ARG);
  27512. if (pkcs7 != NULL) {
  27513. pkcs7->content = tmpBytePtr;
  27514. tmpWrd32 = pkcs7->contentSz;
  27515. pkcs7->contentSz = 0;
  27516. }
  27517. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27518. sizeof(encrypted)), BAD_FUNC_ARG);
  27519. if (pkcs7 != NULL) {
  27520. pkcs7->contentSz = tmpWrd32;
  27521. tmpInt = pkcs7->encryptOID;
  27522. pkcs7->encryptOID = 0;
  27523. }
  27524. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27525. sizeof(encrypted)), BAD_FUNC_ARG);
  27526. if (pkcs7 != NULL) {
  27527. pkcs7->encryptOID = tmpInt;
  27528. tmpBytePtr = pkcs7->encryptionKey;
  27529. pkcs7->encryptionKey = NULL;
  27530. }
  27531. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27532. sizeof(encrypted)), BAD_FUNC_ARG);
  27533. if (pkcs7 != NULL) {
  27534. pkcs7->encryptionKey = tmpBytePtr;
  27535. tmpWrd32 = pkcs7->encryptionKeySz;
  27536. pkcs7->encryptionKeySz = 0;
  27537. }
  27538. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  27539. sizeof(encrypted)), BAD_FUNC_ARG);
  27540. if (pkcs7 != NULL) {
  27541. pkcs7->encryptionKeySz = tmpWrd32;
  27542. }
  27543. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  27544. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27545. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  27546. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27547. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  27548. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27549. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27550. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  27551. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27552. decoded, 0), BAD_FUNC_ARG);
  27553. /* Test struct fields */
  27554. if (pkcs7 != NULL) {
  27555. tmpBytePtr = pkcs7->encryptionKey;
  27556. pkcs7->encryptionKey = NULL;
  27557. }
  27558. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27559. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27560. if (pkcs7 != NULL) {
  27561. pkcs7->encryptionKey = tmpBytePtr;
  27562. pkcs7->encryptionKeySz = 0;
  27563. }
  27564. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  27565. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  27566. wc_PKCS7_Free(pkcs7);
  27567. #endif
  27568. return EXPECT_RESULT();
  27569. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  27570. /*
  27571. * Testing wc_PKCS7_Degenerate()
  27572. */
  27573. static int test_wc_PKCS7_Degenerate(void)
  27574. {
  27575. EXPECT_DECLS;
  27576. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  27577. PKCS7* pkcs7 = NULL;
  27578. char fName[] = "./certs/test-degenerate.p7b";
  27579. XFILE f = XBADFILE;
  27580. byte der[4096];
  27581. word32 derSz = 0;
  27582. #ifndef NO_PKCS7_STREAM
  27583. word32 z;
  27584. int ret;
  27585. #endif /* !NO_PKCS7_STREAM */
  27586. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  27587. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27588. if (f != XBADFILE)
  27589. XFCLOSE(f);
  27590. /* test degenerate success */
  27591. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27592. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27593. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27594. #ifndef NO_RSA
  27595. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27596. #ifndef NO_PKCS7_STREAM
  27597. wc_PKCS7_Free(pkcs7);
  27598. pkcs7 = NULL;
  27599. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27600. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27601. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27602. /* test for streaming */
  27603. ret = -1;
  27604. for (z = 0; z < derSz && ret != 0; z++) {
  27605. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27606. if (ret < 0){
  27607. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  27608. }
  27609. }
  27610. ExpectIntEQ(ret, 0);
  27611. #endif /* !NO_PKCS7_STREAM */
  27612. #else
  27613. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27614. #endif /* NO_RSA */
  27615. wc_PKCS7_Free(pkcs7);
  27616. pkcs7 = NULL;
  27617. /* test with turning off degenerate cases */
  27618. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27619. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27620. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27621. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  27622. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  27623. PKCS7_NO_SIGNER_E);
  27624. #ifndef NO_PKCS7_STREAM
  27625. wc_PKCS7_Free(pkcs7);
  27626. pkcs7 = NULL;
  27627. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27628. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27629. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27630. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  27631. /* test for streaming */
  27632. ret = -1;
  27633. for (z = 0; z < derSz && ret != 0; z++) {
  27634. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27635. if (ret == WC_PKCS7_WANT_READ_E){
  27636. continue;
  27637. }
  27638. else
  27639. break;
  27640. }
  27641. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  27642. #endif /* !NO_PKCS7_STREAM */
  27643. wc_PKCS7_Free(pkcs7);
  27644. #endif
  27645. return EXPECT_RESULT();
  27646. } /* END test_wc_PKCS7_Degenerate() */
  27647. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  27648. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  27649. static byte berContent[] = {
  27650. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  27651. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  27652. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  27653. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  27654. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  27655. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  27656. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  27657. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  27658. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  27659. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  27660. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  27661. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  27662. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  27663. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  27664. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  27665. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  27666. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  27667. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  27668. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  27669. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  27670. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  27671. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  27672. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  27673. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  27674. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  27675. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  27676. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  27677. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  27678. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  27679. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  27680. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  27681. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  27682. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  27683. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  27684. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  27685. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  27686. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  27687. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  27688. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  27689. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  27690. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  27691. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  27692. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  27693. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  27694. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  27695. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  27696. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  27697. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  27698. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  27699. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  27700. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  27701. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  27702. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  27703. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  27704. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  27705. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  27706. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  27707. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  27708. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  27709. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  27710. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  27711. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  27712. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  27713. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  27714. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  27715. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  27716. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  27717. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  27718. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  27719. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  27720. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  27721. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  27722. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  27723. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  27724. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  27725. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  27726. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  27727. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  27728. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  27729. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  27730. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  27731. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  27732. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  27733. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  27734. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  27735. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  27736. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  27737. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  27738. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  27739. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  27740. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  27741. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  27742. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  27743. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  27744. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  27745. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  27746. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  27747. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  27748. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  27749. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  27750. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  27751. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  27752. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  27753. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  27754. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  27755. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  27756. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  27757. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  27758. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  27759. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  27760. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  27761. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  27762. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  27763. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  27764. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  27765. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  27766. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  27767. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  27768. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  27769. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  27770. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  27771. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  27772. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  27773. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  27774. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  27775. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  27776. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  27777. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  27778. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  27779. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  27780. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  27781. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  27782. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  27783. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  27784. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  27785. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  27786. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  27787. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  27788. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  27789. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  27790. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  27791. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  27792. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  27793. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  27794. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  27795. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  27796. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  27797. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  27798. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  27799. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  27800. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  27801. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  27802. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  27803. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  27804. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  27805. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  27806. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  27807. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  27808. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  27809. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  27810. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  27811. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  27812. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  27813. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  27814. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  27815. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  27816. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  27817. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  27818. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  27819. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  27820. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  27821. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  27822. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  27823. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  27824. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  27825. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  27826. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  27827. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  27828. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  27829. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  27830. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  27831. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  27832. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  27833. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  27834. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  27835. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  27836. 0x00, 0x00, 0x00, 0x00, 0x00
  27837. };
  27838. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  27839. * !NO_DES3 && !NO_SHA
  27840. */
  27841. /*
  27842. * Testing wc_PKCS7_BER()
  27843. */
  27844. static int test_wc_PKCS7_BER(void)
  27845. {
  27846. EXPECT_DECLS;
  27847. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  27848. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  27849. PKCS7* pkcs7 = NULL;
  27850. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  27851. XFILE f = XBADFILE;
  27852. byte der[4096];
  27853. #ifndef NO_DES3
  27854. byte decoded[2048];
  27855. #endif
  27856. word32 derSz = 0;
  27857. #ifndef NO_PKCS7_STREAM
  27858. word32 z;
  27859. int ret;
  27860. #endif /* !NO_PKCS7_STREAM */
  27861. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  27862. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27863. if (f != XBADFILE) {
  27864. XFCLOSE(f);
  27865. f = XBADFILE;
  27866. }
  27867. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27868. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27869. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27870. #ifndef NO_RSA
  27871. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27872. #ifndef NO_PKCS7_STREAM
  27873. wc_PKCS7_Free(pkcs7);
  27874. pkcs7 = NULL;
  27875. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27876. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  27877. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  27878. /* test for streaming */
  27879. ret = -1;
  27880. for (z = 0; z < derSz && ret != 0; z++) {
  27881. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  27882. if (ret < 0){
  27883. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  27884. }
  27885. }
  27886. ExpectIntEQ(ret, 0);
  27887. #endif /* !NO_PKCS7_STREAM */
  27888. #else
  27889. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  27890. #endif
  27891. wc_PKCS7_Free(pkcs7);
  27892. pkcs7 = NULL;
  27893. #ifndef NO_DES3
  27894. /* decode BER content */
  27895. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  27896. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27897. if (f != XBADFILE) {
  27898. XFCLOSE(f);
  27899. f = XBADFILE;
  27900. }
  27901. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  27902. #ifndef NO_RSA
  27903. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27904. #else
  27905. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  27906. #endif
  27907. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  27908. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  27909. if (f != XBADFILE) {
  27910. XFCLOSE(f);
  27911. f = XBADFILE;
  27912. }
  27913. if (pkcs7 != NULL) {
  27914. pkcs7->privateKey = der;
  27915. pkcs7->privateKeySz = derSz;
  27916. }
  27917. #ifndef NO_RSA
  27918. #ifdef WOLFSSL_SP_MATH
  27919. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27920. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  27921. #else
  27922. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27923. sizeof(berContent), decoded, sizeof(decoded)), 0);
  27924. #endif
  27925. #else
  27926. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  27927. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  27928. #endif
  27929. wc_PKCS7_Free(pkcs7);
  27930. #endif /* !NO_DES3 */
  27931. #endif
  27932. return EXPECT_RESULT();
  27933. } /* END test_wc_PKCS7_BER() */
  27934. static int test_wc_PKCS7_signed_enveloped(void)
  27935. {
  27936. EXPECT_DECLS;
  27937. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  27938. !defined(NO_FILESYSTEM)
  27939. XFILE f = XBADFILE;
  27940. PKCS7* pkcs7 = NULL;
  27941. #ifdef HAVE_AES_CBC
  27942. PKCS7* inner = NULL;
  27943. #endif
  27944. WC_RNG rng;
  27945. unsigned char key[FOURK_BUF/2];
  27946. unsigned char cert[FOURK_BUF/2];
  27947. unsigned char env[FOURK_BUF];
  27948. int envSz = FOURK_BUF;
  27949. int keySz = 0;
  27950. int certSz = 0;
  27951. unsigned char sig[FOURK_BUF * 2];
  27952. int sigSz = FOURK_BUF * 2;
  27953. #ifdef HAVE_AES_CBC
  27954. unsigned char decoded[FOURK_BUF];
  27955. int decodedSz = FOURK_BUF;
  27956. #endif
  27957. #ifndef NO_PKCS7_STREAM
  27958. int z;
  27959. int ret;
  27960. #endif /* !NO_PKCS7_STREAM */
  27961. XMEMSET(&rng, 0, sizeof(WC_RNG));
  27962. /* load cert */
  27963. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  27964. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  27965. if (f != XBADFILE) {
  27966. XFCLOSE(f);
  27967. f = XBADFILE;
  27968. }
  27969. /* load key */
  27970. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  27971. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  27972. if (f != XBADFILE) {
  27973. XFCLOSE(f);
  27974. f = XBADFILE;
  27975. }
  27976. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  27977. /* sign cert for envelope */
  27978. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27979. ExpectIntEQ(wc_InitRng(&rng), 0);
  27980. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  27981. if (pkcs7 != NULL) {
  27982. pkcs7->content = cert;
  27983. pkcs7->contentSz = (word32)certSz;
  27984. pkcs7->contentOID = DATA;
  27985. pkcs7->privateKey = key;
  27986. pkcs7->privateKeySz = (word32)keySz;
  27987. pkcs7->encryptOID = RSAk;
  27988. pkcs7->hashOID = SHA256h;
  27989. pkcs7->rng = &rng;
  27990. }
  27991. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  27992. wc_PKCS7_Free(pkcs7);
  27993. pkcs7 = NULL;
  27994. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  27995. #ifdef HAVE_AES_CBC
  27996. /* create envelope */
  27997. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  27998. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  27999. if (pkcs7 != NULL) {
  28000. pkcs7->content = sig;
  28001. pkcs7->contentSz = (word32)sigSz;
  28002. pkcs7->contentOID = DATA;
  28003. pkcs7->encryptOID = AES256CBCb;
  28004. pkcs7->privateKey = key;
  28005. pkcs7->privateKeySz = (word32)keySz;
  28006. }
  28007. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  28008. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  28009. wc_PKCS7_Free(pkcs7);
  28010. pkcs7 = NULL;
  28011. #endif
  28012. /* create bad signed enveloped data */
  28013. sigSz = FOURK_BUF * 2;
  28014. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28015. ExpectIntEQ(wc_InitRng(&rng), 0);
  28016. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28017. if (pkcs7 != NULL) {
  28018. pkcs7->content = env;
  28019. pkcs7->contentSz = (word32)envSz;
  28020. pkcs7->contentOID = DATA;
  28021. pkcs7->privateKey = key;
  28022. pkcs7->privateKeySz = (word32)keySz;
  28023. pkcs7->encryptOID = RSAk;
  28024. pkcs7->hashOID = SHA256h;
  28025. pkcs7->rng = &rng;
  28026. }
  28027. /* Set no certs in bundle for this test. */
  28028. if (pkcs7 != NULL) {
  28029. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  28030. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  28031. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  28032. }
  28033. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28034. wc_PKCS7_Free(pkcs7);
  28035. pkcs7 = NULL;
  28036. /* check verify fails */
  28037. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28038. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28039. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  28040. PKCS7_SIGNEEDS_CHECK);
  28041. /* try verifying the signature manually */
  28042. {
  28043. RsaKey rKey;
  28044. word32 idx = 0;
  28045. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  28046. WC_MAX_DIGEST_SIZE];
  28047. int digestSz = 0;
  28048. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  28049. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  28050. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  28051. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  28052. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  28053. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  28054. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  28055. /* verify was success */
  28056. }
  28057. wc_PKCS7_Free(pkcs7);
  28058. pkcs7 = NULL;
  28059. /* initializing the PKCS7 struct with the signing certificate should pass */
  28060. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28061. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28062. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  28063. #ifndef NO_PKCS7_STREAM
  28064. wc_PKCS7_Free(pkcs7);
  28065. pkcs7 = NULL;
  28066. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  28067. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  28068. /* test for streaming */
  28069. ret = -1;
  28070. for (z = 0; z < sigSz && ret != 0; z++) {
  28071. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  28072. if (ret < 0){
  28073. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28074. }
  28075. }
  28076. ExpectIntEQ(ret, 0);
  28077. #endif /* !NO_PKCS7_STREAM */
  28078. wc_PKCS7_Free(pkcs7);
  28079. pkcs7 = NULL;
  28080. /* create valid degenerate bundle */
  28081. sigSz = FOURK_BUF * 2;
  28082. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28083. if (pkcs7 != NULL) {
  28084. pkcs7->content = env;
  28085. pkcs7->contentSz = (word32)envSz;
  28086. pkcs7->contentOID = DATA;
  28087. pkcs7->privateKey = key;
  28088. pkcs7->privateKeySz = (word32)keySz;
  28089. pkcs7->encryptOID = RSAk;
  28090. pkcs7->hashOID = SHA256h;
  28091. pkcs7->rng = &rng;
  28092. }
  28093. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  28094. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28095. wc_PKCS7_Free(pkcs7);
  28096. pkcs7 = NULL;
  28097. wc_FreeRng(&rng);
  28098. /* check verify */
  28099. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28100. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  28101. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  28102. ExpectNotNull(pkcs7->content);
  28103. #ifndef NO_PKCS7_STREAM
  28104. wc_PKCS7_Free(pkcs7);
  28105. pkcs7 = NULL;
  28106. /* create valid degenerate bundle */
  28107. sigSz = FOURK_BUF * 2;
  28108. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28109. if (pkcs7 != NULL) {
  28110. pkcs7->content = env;
  28111. pkcs7->contentSz = (word32)envSz;
  28112. pkcs7->contentOID = DATA;
  28113. pkcs7->privateKey = key;
  28114. pkcs7->privateKeySz = (word32)keySz;
  28115. pkcs7->encryptOID = RSAk;
  28116. pkcs7->hashOID = SHA256h;
  28117. pkcs7->rng = &rng;
  28118. }
  28119. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  28120. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  28121. wc_PKCS7_Free(pkcs7);
  28122. pkcs7 = NULL;
  28123. wc_FreeRng(&rng);
  28124. /* check verify */
  28125. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28126. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  28127. /* test for streaming */
  28128. ret = -1;
  28129. for (z = 0; z < sigSz && ret != 0; z++) {
  28130. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  28131. if (ret < 0){
  28132. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28133. }
  28134. }
  28135. ExpectIntEQ(ret, 0);
  28136. #endif /* !NO_PKCS7_STREAM */
  28137. #ifdef HAVE_AES_CBC
  28138. /* check decode */
  28139. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  28140. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  28141. if (inner != NULL) {
  28142. inner->privateKey = key;
  28143. inner->privateKeySz = (word32)keySz;
  28144. }
  28145. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  28146. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  28147. wc_PKCS7_Free(inner);
  28148. inner = NULL;
  28149. #endif
  28150. wc_PKCS7_Free(pkcs7);
  28151. pkcs7 = NULL;
  28152. #ifdef HAVE_AES_CBC
  28153. /* check cert set */
  28154. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28155. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28156. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  28157. ExpectNotNull(pkcs7->singleCert);
  28158. ExpectIntNE(pkcs7->singleCertSz, 0);
  28159. wc_PKCS7_Free(pkcs7);
  28160. pkcs7 = NULL;
  28161. #ifndef NO_PKCS7_STREAM
  28162. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  28163. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  28164. /* test for streaming */
  28165. ret = -1;
  28166. for (z = 0; z < decodedSz && ret != 0; z++) {
  28167. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  28168. if (ret < 0){
  28169. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  28170. }
  28171. }
  28172. ExpectIntEQ(ret, 0);
  28173. ExpectNotNull(pkcs7->singleCert);
  28174. ExpectIntNE(pkcs7->singleCertSz, 0);
  28175. wc_PKCS7_Free(pkcs7);
  28176. pkcs7 = NULL;
  28177. #endif /* !NO_PKCS7_STREAM */
  28178. #endif
  28179. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  28180. return EXPECT_RESULT();
  28181. }
  28182. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  28183. {
  28184. EXPECT_DECLS;
  28185. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28186. && !defined(NO_AES)
  28187. PKCS7* pkcs7 = NULL;
  28188. void* heap = NULL;
  28189. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28190. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28191. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  28192. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  28193. wc_PKCS7_Free(pkcs7);
  28194. #endif
  28195. return EXPECT_RESULT();
  28196. }
  28197. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  28198. {
  28199. EXPECT_DECLS;
  28200. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28201. && !defined(NO_AES)
  28202. PKCS7* pkcs7 = NULL;
  28203. void* heap = NULL;
  28204. WOLFSSL_CTX* ctx = NULL;
  28205. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28206. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28207. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  28208. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  28209. wc_PKCS7_Free(pkcs7);
  28210. #endif
  28211. return EXPECT_RESULT();
  28212. }
  28213. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  28214. {
  28215. EXPECT_DECLS;
  28216. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28217. && !defined(NO_AES)
  28218. PKCS7* pkcs7 = NULL;
  28219. void* heap = NULL;
  28220. WOLFSSL_CTX* ctx = NULL;
  28221. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  28222. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  28223. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  28224. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  28225. wc_PKCS7_Free(pkcs7);
  28226. #endif
  28227. return EXPECT_RESULT();
  28228. }
  28229. static int test_wc_PKCS7_DecodeCompressedData(void)
  28230. {
  28231. EXPECT_DECLS;
  28232. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28233. && !defined(NO_AES) && defined(HAVE_LIBZ)
  28234. PKCS7* pkcs7 = NULL;
  28235. void* heap = NULL;
  28236. byte out[4096];
  28237. byte* decompressed = NULL;
  28238. int outSz;
  28239. int decompressedSz;
  28240. const char* cert = "./certs/client-cert.pem";
  28241. byte* cert_buf = NULL;
  28242. size_t cert_sz = 0;
  28243. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  28244. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  28245. DYNAMIC_TYPE_TMP_BUFFER)));
  28246. decompressedSz = (int)cert_sz;
  28247. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  28248. if (pkcs7 != NULL) {
  28249. pkcs7->content = (byte*)cert_buf;
  28250. pkcs7->contentSz = (word32)cert_sz;
  28251. pkcs7->contentOID = DATA;
  28252. }
  28253. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  28254. sizeof(out))), 0);
  28255. wc_PKCS7_Free(pkcs7);
  28256. pkcs7 = NULL;
  28257. /* compressed key should be smaller than when started */
  28258. ExpectIntLT(outSz, cert_sz);
  28259. /* test decompression */
  28260. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  28261. ExpectIntEQ(pkcs7->contentOID, 0);
  28262. /* fail case with out buffer too small */
  28263. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  28264. decompressed, outSz), 0);
  28265. /* success case */
  28266. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  28267. decompressed, decompressedSz), cert_sz);
  28268. ExpectIntEQ(pkcs7->contentOID, DATA);
  28269. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28270. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28271. decompressed = NULL;
  28272. /* test decompression function with different 'max' inputs */
  28273. outSz = sizeof(out);
  28274. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  28275. 0);
  28276. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  28277. out, outSz, 0, heap), 0);
  28278. ExpectNull(decompressed);
  28279. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  28280. out, outSz, 0, heap), 0);
  28281. ExpectNotNull(decompressed);
  28282. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28283. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28284. decompressed = NULL;
  28285. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  28286. out, outSz, 0, heap), 0);
  28287. ExpectNotNull(decompressed);
  28288. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  28289. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  28290. if (cert_buf != NULL)
  28291. free(cert_buf);
  28292. wc_PKCS7_Free(pkcs7);
  28293. #endif
  28294. return EXPECT_RESULT();
  28295. }
  28296. static int test_wc_i2d_PKCS12(void)
  28297. {
  28298. EXPECT_DECLS;
  28299. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  28300. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  28301. && !defined(NO_AES) && !defined(NO_SHA)
  28302. WC_PKCS12* pkcs12 = NULL;
  28303. unsigned char der[FOURK_BUF * 2];
  28304. unsigned char* pt;
  28305. int derSz = 0;
  28306. unsigned char out[FOURK_BUF * 2];
  28307. int outSz = FOURK_BUF * 2;
  28308. const char p12_f[] = "./certs/test-servercert.p12";
  28309. XFILE f = XBADFILE;
  28310. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  28311. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  28312. if (f != XBADFILE)
  28313. XFCLOSE(f);
  28314. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  28315. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  28316. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28317. ExpectIntEQ(outSz, derSz);
  28318. outSz = derSz - 1;
  28319. pt = out;
  28320. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  28321. outSz = derSz;
  28322. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  28323. ExpectIntEQ((pt == out), 0);
  28324. pt = NULL;
  28325. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  28326. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  28327. wc_PKCS12_free(pkcs12);
  28328. pkcs12 = NULL;
  28329. /* Run the same test but use wc_d2i_PKCS12_fp. */
  28330. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  28331. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  28332. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28333. ExpectIntEQ(outSz, derSz);
  28334. wc_PKCS12_free(pkcs12);
  28335. pkcs12 = NULL;
  28336. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  28337. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  28338. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  28339. ExpectIntEQ(outSz, derSz);
  28340. wc_PKCS12_free(pkcs12);
  28341. pkcs12 = NULL;
  28342. #endif
  28343. return EXPECT_RESULT();
  28344. }
  28345. /* Testing wc_SignatureGetSize() for signature type ECC */
  28346. static int test_wc_SignatureGetSize_ecc(void)
  28347. {
  28348. EXPECT_DECLS;
  28349. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  28350. enum wc_SignatureType sig_type;
  28351. word32 key_len;
  28352. ecc_key ecc;
  28353. const char* qx =
  28354. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  28355. const char* qy =
  28356. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  28357. const char* d =
  28358. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  28359. XMEMSET(&ecc, 0, sizeof(ecc_key));
  28360. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  28361. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  28362. /* Input for signature type ECC */
  28363. sig_type = WC_SIGNATURE_TYPE_ECC;
  28364. key_len = sizeof(ecc_key);
  28365. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  28366. /* Test bad args */
  28367. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28368. sig_type = (enum wc_SignatureType) 100;
  28369. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28370. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  28371. sig_type = WC_SIGNATURE_TYPE_ECC;
  28372. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  28373. key_len = (word32)0;
  28374. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  28375. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  28376. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  28377. return EXPECT_RESULT();
  28378. } /* END test_wc_SignatureGetSize_ecc() */
  28379. /* Testing wc_SignatureGetSize() for signature type rsa */
  28380. static int test_wc_SignatureGetSize_rsa(void)
  28381. {
  28382. EXPECT_DECLS;
  28383. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  28384. enum wc_SignatureType sig_type;
  28385. word32 key_len;
  28386. word32 idx = 0;
  28387. RsaKey rsa_key;
  28388. byte* tmp = NULL;
  28389. size_t bytes;
  28390. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  28391. #ifdef USE_CERT_BUFFERS_1024
  28392. bytes = (size_t)sizeof_client_key_der_1024;
  28393. if (bytes < (size_t)sizeof_client_key_der_1024)
  28394. bytes = (size_t)sizeof_client_cert_der_1024;
  28395. #elif defined(USE_CERT_BUFFERS_2048)
  28396. bytes = (size_t)sizeof_client_key_der_2048;
  28397. if (bytes < (size_t)sizeof_client_cert_der_2048)
  28398. bytes = (size_t)sizeof_client_cert_der_2048;
  28399. #else
  28400. bytes = FOURK_BUF;
  28401. #endif
  28402. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  28403. DYNAMIC_TYPE_TMP_BUFFER));
  28404. if (tmp != NULL) {
  28405. #ifdef USE_CERT_BUFFERS_1024
  28406. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  28407. #elif defined(USE_CERT_BUFFERS_2048)
  28408. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  28409. #elif !defined(NO_FILESYSTEM)
  28410. XFILE file = XBADFILE;
  28411. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  28412. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  28413. if (file != XBADFILE)
  28414. XFCLOSE(file);
  28415. }
  28416. #else
  28417. ExpectFail();
  28418. #endif
  28419. }
  28420. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  28421. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  28422. /* Input for signature type RSA */
  28423. sig_type = WC_SIGNATURE_TYPE_RSA;
  28424. key_len = sizeof(RsaKey);
  28425. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  28426. /* Test bad args */
  28427. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28428. sig_type = (enum wc_SignatureType)100;
  28429. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  28430. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  28431. sig_type = WC_SIGNATURE_TYPE_RSA;
  28432. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  28433. key_len = (word32)0;
  28434. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  28435. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  28436. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28437. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  28438. return EXPECT_RESULT();
  28439. } /* END test_wc_SignatureGetSize_rsa(void) */
  28440. /*----------------------------------------------------------------------------*
  28441. | hash.h Tests
  28442. *----------------------------------------------------------------------------*/
  28443. static int test_wc_HashInit(void)
  28444. {
  28445. EXPECT_DECLS;
  28446. int i; /* 0 indicates tests passed, 1 indicates failure */
  28447. wc_HashAlg hash;
  28448. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28449. enum wc_HashType enumArray[] = {
  28450. #ifndef NO_MD5
  28451. WC_HASH_TYPE_MD5,
  28452. #endif
  28453. #ifndef NO_SHA
  28454. WC_HASH_TYPE_SHA,
  28455. #endif
  28456. #ifdef WOLFSSL_SHA224
  28457. WC_HASH_TYPE_SHA224,
  28458. #endif
  28459. #ifndef NO_SHA256
  28460. WC_HASH_TYPE_SHA256,
  28461. #endif
  28462. #ifdef WOLFSSL_SHA384
  28463. WC_HASH_TYPE_SHA384,
  28464. #endif
  28465. #ifdef WOLFSSL_SHA512
  28466. WC_HASH_TYPE_SHA512,
  28467. #endif
  28468. };
  28469. /* dynamically finds the length */
  28470. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28471. /* For loop to test various arguments... */
  28472. for (i = 0; i < enumlen; i++) {
  28473. /* check for bad args */
  28474. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28475. wc_HashFree(&hash, enumArray[i]);
  28476. /* check for null ptr */
  28477. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  28478. } /* end of for loop */
  28479. return EXPECT_RESULT();
  28480. } /* end of test_wc_HashInit */
  28481. /*
  28482. * Unit test function for wc_HashSetFlags()
  28483. */
  28484. static int test_wc_HashSetFlags(void)
  28485. {
  28486. EXPECT_DECLS;
  28487. #ifdef WOLFSSL_HASH_FLAGS
  28488. wc_HashAlg hash;
  28489. word32 flags = 0;
  28490. int i, j;
  28491. int notSupportedLen;
  28492. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28493. enum wc_HashType enumArray[] = {
  28494. #ifndef NO_MD5
  28495. WC_HASH_TYPE_MD5,
  28496. #endif
  28497. #ifndef NO_SHA
  28498. WC_HASH_TYPE_SHA,
  28499. #endif
  28500. #ifdef WOLFSSL_SHA224
  28501. WC_HASH_TYPE_SHA224,
  28502. #endif
  28503. #ifndef NO_SHA256
  28504. WC_HASH_TYPE_SHA256,
  28505. #endif
  28506. #ifdef WOLFSSL_SHA384
  28507. WC_HASH_TYPE_SHA384,
  28508. #endif
  28509. #ifdef WOLFSSL_SHA512
  28510. WC_HASH_TYPE_SHA512,
  28511. #endif
  28512. #ifdef WOLFSSL_SHA3
  28513. WC_HASH_TYPE_SHA3_224,
  28514. #endif
  28515. };
  28516. enum wc_HashType notSupported[] = {
  28517. WC_HASH_TYPE_MD5_SHA,
  28518. WC_HASH_TYPE_MD2,
  28519. WC_HASH_TYPE_MD4,
  28520. WC_HASH_TYPE_BLAKE2B,
  28521. WC_HASH_TYPE_BLAKE2S,
  28522. WC_HASH_TYPE_NONE,
  28523. };
  28524. /* dynamically finds the length */
  28525. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28526. /* For loop to test various arguments... */
  28527. for (i = 0; i < enumlen; i++) {
  28528. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28529. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  28530. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  28531. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  28532. wc_HashFree(&hash, enumArray[i]);
  28533. }
  28534. /* For loop to test not supported cases */
  28535. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  28536. for (j = 0; j < notSupportedLen; j++) {
  28537. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  28538. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  28539. BAD_FUNC_ARG);
  28540. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  28541. }
  28542. #endif
  28543. return EXPECT_RESULT();
  28544. } /* END test_wc_HashSetFlags */
  28545. /*
  28546. * Unit test function for wc_HashGetFlags()
  28547. */
  28548. static int test_wc_HashGetFlags(void)
  28549. {
  28550. EXPECT_DECLS;
  28551. #ifdef WOLFSSL_HASH_FLAGS
  28552. wc_HashAlg hash;
  28553. word32 flags = 0;
  28554. int i, j;
  28555. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  28556. enum wc_HashType enumArray[] = {
  28557. #ifndef NO_MD5
  28558. WC_HASH_TYPE_MD5,
  28559. #endif
  28560. #ifndef NO_SHA
  28561. WC_HASH_TYPE_SHA,
  28562. #endif
  28563. #ifdef WOLFSSL_SHA224
  28564. WC_HASH_TYPE_SHA224,
  28565. #endif
  28566. #ifndef NO_SHA256
  28567. WC_HASH_TYPE_SHA256,
  28568. #endif
  28569. #ifdef WOLFSSL_SHA384
  28570. WC_HASH_TYPE_SHA384,
  28571. #endif
  28572. #ifdef WOLFSSL_SHA512
  28573. WC_HASH_TYPE_SHA512,
  28574. #endif
  28575. #ifdef WOLFSSL_SHA3
  28576. WC_HASH_TYPE_SHA3_224,
  28577. #endif
  28578. };
  28579. enum wc_HashType notSupported[] = {
  28580. WC_HASH_TYPE_MD5_SHA,
  28581. WC_HASH_TYPE_MD2,
  28582. WC_HASH_TYPE_MD4,
  28583. WC_HASH_TYPE_BLAKE2B,
  28584. WC_HASH_TYPE_BLAKE2S,
  28585. WC_HASH_TYPE_NONE,
  28586. };
  28587. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  28588. int notSupportedLen;
  28589. /* For loop to test various arguments... */
  28590. for (i = 0; i < enumlen; i++) {
  28591. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  28592. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  28593. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  28594. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  28595. wc_HashFree(&hash, enumArray[i]);
  28596. }
  28597. /* For loop to test not supported cases */
  28598. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  28599. for (j = 0; j < notSupportedLen; j++) {
  28600. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  28601. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  28602. BAD_FUNC_ARG);
  28603. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  28604. }
  28605. #endif
  28606. return EXPECT_RESULT();
  28607. } /* END test_wc_HashGetFlags */
  28608. /*----------------------------------------------------------------------------*
  28609. | Compatibility Tests
  28610. *----------------------------------------------------------------------------*/
  28611. /*----------------------------------------------------------------------------*
  28612. | ASN.1 Tests
  28613. *----------------------------------------------------------------------------*/
  28614. static int test_wolfSSL_ASN1_BIT_STRING(void)
  28615. {
  28616. EXPECT_DECLS;
  28617. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  28618. ASN1_BIT_STRING* str = NULL;
  28619. ExpectNotNull(str = ASN1_BIT_STRING_new());
  28620. /* Empty data testing. */
  28621. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  28622. ASN1_BIT_STRING_free(str);
  28623. str = NULL;
  28624. ExpectNotNull(str = ASN1_BIT_STRING_new());
  28625. /* Invalid parameter testing. */
  28626. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  28627. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  28628. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  28629. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  28630. /* No bit string - bit is always 0. */
  28631. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  28632. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  28633. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  28634. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  28635. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  28636. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  28637. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  28638. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  28639. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  28640. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  28641. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  28642. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  28643. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  28644. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  28645. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  28646. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  28647. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  28648. ASN1_BIT_STRING_free(str);
  28649. ASN1_BIT_STRING_free(NULL);
  28650. #endif
  28651. return EXPECT_RESULT();
  28652. }
  28653. static int test_wolfSSL_ASN1_INTEGER(void)
  28654. {
  28655. EXPECT_DECLS;
  28656. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28657. ASN1_INTEGER* a = NULL;
  28658. ASN1_INTEGER* dup = NULL;
  28659. const unsigned char invalidLenDer[] = {
  28660. 0x02, 0x20, 0x00
  28661. };
  28662. const unsigned char longDer[] = {
  28663. 0x02, 0x20,
  28664. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28665. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28666. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  28667. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  28668. };
  28669. const unsigned char* p;
  28670. /* Invalid parameter testing. */
  28671. ASN1_INTEGER_free(NULL);
  28672. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  28673. ExpectNotNull(a = ASN1_INTEGER_new());
  28674. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28675. ASN1_INTEGER_free(dup);
  28676. dup = NULL;
  28677. ASN1_INTEGER_free(a);
  28678. a = NULL;
  28679. p = longDer;
  28680. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  28681. p = longDer;
  28682. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  28683. ExpectPtrNE(p, longDer);
  28684. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  28685. ASN1_INTEGER_free(dup);
  28686. ASN1_INTEGER_free(a);
  28687. #endif
  28688. return EXPECT_RESULT();
  28689. }
  28690. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  28691. {
  28692. EXPECT_DECLS;
  28693. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28694. ASN1_INTEGER* a = NULL;
  28695. ASN1_INTEGER* b = NULL;
  28696. ExpectNotNull(a = ASN1_INTEGER_new());
  28697. ExpectNotNull(b = ASN1_INTEGER_new());
  28698. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  28699. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28700. /* Invalid parameter testing. */
  28701. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  28702. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  28703. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  28704. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28705. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  28706. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28707. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  28708. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28709. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  28710. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28711. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  28712. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  28713. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  28714. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  28715. ASN1_INTEGER_free(b);
  28716. ASN1_INTEGER_free(a);
  28717. #endif
  28718. return EXPECT_RESULT();
  28719. }
  28720. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  28721. {
  28722. EXPECT_DECLS;
  28723. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28724. ASN1_INTEGER* ai = NULL;
  28725. ASN1_INTEGER* ai2 = NULL;
  28726. BIGNUM* bn = NULL;
  28727. BIGNUM* bn2 = NULL;
  28728. ExpectNotNull(ai = ASN1_INTEGER_new());
  28729. ExpectNotNull(bn2 = BN_new());
  28730. /* Invalid parameter testing. */
  28731. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  28732. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  28733. /* at the moment hard setting since no set function */
  28734. if (ai != NULL) {
  28735. ai->data[0] = 0xff; /* No DER encoding. */
  28736. ai->length = 1;
  28737. }
  28738. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28739. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28740. BN_free(bn);
  28741. bn = NULL;
  28742. #else
  28743. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28744. #endif
  28745. if (ai != NULL) {
  28746. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28747. ai->data[1] = 0x04; /* bad length of integer */
  28748. ai->data[2] = 0x03;
  28749. ai->length = 3;
  28750. }
  28751. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28752. /* Interpreted as a number 0x020403. */
  28753. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28754. BN_free(bn);
  28755. bn = NULL;
  28756. #else
  28757. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  28758. #endif
  28759. if (ai != NULL) {
  28760. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28761. ai->data[1] = 0x01; /* length of integer */
  28762. ai->data[2] = 0x03;
  28763. ai->length = 3;
  28764. }
  28765. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  28766. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  28767. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28768. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28769. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28770. if (ai != NULL) {
  28771. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28772. ai->data[1] = 0x02; /* length of integer */
  28773. ai->data[2] = 0x00; /* padding byte to ensure positive */
  28774. ai->data[3] = 0xff;
  28775. ai->length = 4;
  28776. }
  28777. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28778. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28779. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28780. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28781. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28782. if (ai != NULL) {
  28783. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28784. ai->data[1] = 0x01; /* length of integer */
  28785. ai->data[2] = 0x00;
  28786. ai->length = 3;
  28787. }
  28788. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28789. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28790. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28791. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28792. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28793. if (ai != NULL) {
  28794. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  28795. ai->data[1] = 0x01; /* length of integer */
  28796. ai->data[2] = 0x01;
  28797. ai->length = 3;
  28798. ai->negative = 1;
  28799. }
  28800. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  28801. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  28802. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  28803. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  28804. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  28805. BN_free(bn2);
  28806. BN_free(bn);
  28807. ASN1_INTEGER_free(ai2);
  28808. ASN1_INTEGER_free(ai);
  28809. #endif
  28810. return EXPECT_RESULT();
  28811. }
  28812. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  28813. {
  28814. EXPECT_DECLS;
  28815. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28816. ASN1_INTEGER *a = NULL;
  28817. long val;
  28818. ExpectNotNull(a = ASN1_INTEGER_new());
  28819. /* Invalid parameter testing. */
  28820. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  28821. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  28822. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  28823. #else
  28824. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  28825. #endif
  28826. ASN1_INTEGER_free(a);
  28827. a = NULL;
  28828. ExpectNotNull(a = ASN1_INTEGER_new());
  28829. val = 0;
  28830. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  28831. ASN1_INTEGER_free(a);
  28832. a = NULL;
  28833. /* 0 */
  28834. ExpectNotNull(a = ASN1_INTEGER_new());
  28835. val = 0;
  28836. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28837. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28838. ASN1_INTEGER_free(a);
  28839. a = NULL;
  28840. /* 40 */
  28841. ExpectNotNull(a = ASN1_INTEGER_new());
  28842. val = 40;
  28843. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28844. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28845. ASN1_INTEGER_free(a);
  28846. a = NULL;
  28847. /* -40 */
  28848. ExpectNotNull(a = ASN1_INTEGER_new());
  28849. val = -40;
  28850. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28851. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28852. ASN1_INTEGER_free(a);
  28853. a = NULL;
  28854. /* 128 */
  28855. ExpectNotNull(a = ASN1_INTEGER_new());
  28856. val = 128;
  28857. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28858. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28859. ASN1_INTEGER_free(a);
  28860. a = NULL;
  28861. /* -128 */
  28862. ExpectNotNull(a = ASN1_INTEGER_new());
  28863. val = -128;
  28864. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28865. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28866. ASN1_INTEGER_free(a);
  28867. a = NULL;
  28868. /* 200 */
  28869. ExpectNotNull(a = ASN1_INTEGER_new());
  28870. val = 200;
  28871. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28872. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28873. ASN1_INTEGER_free(a);
  28874. a = NULL;
  28875. /* int max (2147483647) */
  28876. ExpectNotNull(a = ASN1_INTEGER_new());
  28877. val = 2147483647;
  28878. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28879. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28880. ASN1_INTEGER_free(a);
  28881. a = NULL;
  28882. /* int min (-2147483648) */
  28883. ExpectNotNull(a = ASN1_INTEGER_new());
  28884. val = -2147483647 - 1;
  28885. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28886. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28887. ASN1_INTEGER_free(a);
  28888. a = NULL;
  28889. /* long max positive */
  28890. ExpectNotNull(a = ASN1_INTEGER_new());
  28891. val = (long)(((unsigned long)-1) >> 1);
  28892. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  28893. ExpectTrue(ASN1_INTEGER_get(a) == val);
  28894. ASN1_INTEGER_free(a);
  28895. #endif
  28896. return EXPECT_RESULT();
  28897. }
  28898. #if defined(OPENSSL_EXTRA)
  28899. typedef struct ASN1IntTestVector {
  28900. const byte* der;
  28901. const size_t derSz;
  28902. const long value;
  28903. } ASN1IntTestVector;
  28904. #endif
  28905. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  28906. {
  28907. EXPECT_DECLS;
  28908. #if defined(OPENSSL_EXTRA)
  28909. size_t i;
  28910. WOLFSSL_ASN1_INTEGER* a = NULL;
  28911. WOLFSSL_ASN1_INTEGER* b = NULL;
  28912. WOLFSSL_ASN1_INTEGER* c = NULL;
  28913. const byte* p = NULL;
  28914. byte* p2 = NULL;
  28915. byte* reEncoded = NULL;
  28916. int reEncodedSz = 0;
  28917. static const byte zeroDer[] = {
  28918. 0x02, 0x01, 0x00
  28919. };
  28920. static const byte oneDer[] = {
  28921. 0x02, 0x01, 0x01
  28922. };
  28923. static const byte negativeDer[] = {
  28924. 0x02, 0x03, 0xC1, 0x16, 0x0D
  28925. };
  28926. static const byte positiveDer[] = {
  28927. 0x02, 0x03, 0x01, 0x00, 0x01
  28928. };
  28929. static const byte primeDer[] = {
  28930. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  28931. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  28932. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  28933. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  28934. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  28935. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  28936. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  28937. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  28938. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  28939. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  28940. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  28941. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  28942. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  28943. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  28944. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  28945. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  28946. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  28947. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  28948. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  28949. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  28950. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  28951. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  28952. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  28953. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  28954. };
  28955. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  28956. static const ASN1IntTestVector testVectors[] = {
  28957. {zeroDer, sizeof(zeroDer), 0},
  28958. {oneDer, sizeof(oneDer), 1},
  28959. {negativeDer, sizeof(negativeDer), -4123123},
  28960. {positiveDer, sizeof(positiveDer), 65537},
  28961. {primeDer, sizeof(primeDer), 0}
  28962. };
  28963. static const size_t NUM_TEST_VECTORS =
  28964. sizeof(testVectors)/sizeof(testVectors[0]);
  28965. /* Check d2i error conditions */
  28966. /* NULL pointer to input. */
  28967. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  28968. ExpectNull(b);
  28969. /* NULL input. */
  28970. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  28971. ExpectNull(b);
  28972. /* 0 length. */
  28973. p = testVectors[0].der;
  28974. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  28975. ExpectNull(b);
  28976. /* Negative length. */
  28977. p = testVectors[0].der;
  28978. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  28979. ExpectNull(b);
  28980. /* Garbage DER input. */
  28981. p = garbageDer;
  28982. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  28983. ExpectNull(b);
  28984. /* Check i2d error conditions */
  28985. /* NULL input. */
  28986. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  28987. /* 0 length input data buffer (a->length == 0). */
  28988. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  28989. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  28990. if (a != NULL)
  28991. a->data = NULL;
  28992. /* NULL input data buffer. */
  28993. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  28994. if (a != NULL) {
  28995. /* Reset a->data. */
  28996. a->data = a->intData;
  28997. }
  28998. /* Set a to valid value. */
  28999. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  29000. /* NULL output buffer. */
  29001. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  29002. wolfSSL_ASN1_INTEGER_free(a);
  29003. a = NULL;
  29004. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  29005. p = testVectors[i].der;
  29006. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  29007. testVectors[i].derSz));
  29008. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  29009. if (testVectors[i].derSz <= sizeof(long)) {
  29010. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  29011. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  29012. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  29013. wolfSSL_ASN1_INTEGER_free(c);
  29014. c = NULL;
  29015. }
  29016. /* Convert to DER without a pre-allocated output buffer. */
  29017. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  29018. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  29019. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  29020. /* Convert to DER with a pre-allocated output buffer. In this case, the
  29021. * output buffer pointer should be incremented just past the end of the
  29022. * encoded data. */
  29023. p2 = reEncoded;
  29024. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  29025. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  29026. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  29027. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  29028. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  29029. reEncoded = NULL;
  29030. wolfSSL_ASN1_INTEGER_free(a);
  29031. a = NULL;
  29032. }
  29033. #endif /* OPENSSL_EXTRA */
  29034. return EXPECT_RESULT();
  29035. }
  29036. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  29037. {
  29038. EXPECT_DECLS;
  29039. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  29040. BIO* bio = NULL;
  29041. BIO* out = NULL;
  29042. BIO* fixed = NULL;
  29043. ASN1_INTEGER* ai = NULL;
  29044. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  29045. char tmp[1024];
  29046. int tmpSz;
  29047. const char expected1[] = "123456";
  29048. const char expected2[] = "112345678912345678901234567890";
  29049. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  29050. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  29051. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  29052. ExpectNotNull(ai = ASN1_INTEGER_new());
  29053. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  29054. /* Invalid parameter testing. */
  29055. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  29056. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  29057. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  29058. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  29059. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  29060. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  29061. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  29062. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  29063. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  29064. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  29065. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  29066. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  29067. /* No data to read from BIO. */
  29068. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  29069. /* read first line */
  29070. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29071. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  29072. XMEMSET(tmp, 0, 1024);
  29073. tmpSz = BIO_read(out, tmp, 1024);
  29074. ExpectIntEQ(tmpSz, 6);
  29075. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  29076. /* fail on second line (not % 2) */
  29077. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  29078. /* read 3rd long line */
  29079. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29080. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  29081. XMEMSET(tmp, 0, 1024);
  29082. tmpSz = BIO_read(out, tmp, 1024);
  29083. ExpectIntEQ(tmpSz, 30);
  29084. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  29085. /* fail on empty line */
  29086. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  29087. BIO_free(bio);
  29088. bio = NULL;
  29089. /* Make long integer, requiring dynamic memory, even longer. */
  29090. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  29091. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29092. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  29093. XMEMSET(tmp, 0, 1024);
  29094. tmpSz = BIO_read(out, tmp, 1024);
  29095. ExpectIntEQ(tmpSz, 48);
  29096. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  29097. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  29098. XMEMSET(tmp, 0, 1024);
  29099. tmpSz = BIO_read(out, tmp, 1024);
  29100. ExpectIntEQ(tmpSz, 56);
  29101. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  29102. BIO_free(bio);
  29103. BIO_free(out);
  29104. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29105. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29106. /* Ensure there is 0 bytes available to write into. */
  29107. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  29108. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  29109. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29110. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  29111. BIO_free(fixed);
  29112. ASN1_INTEGER_free(ai);
  29113. #endif
  29114. return EXPECT_RESULT();
  29115. }
  29116. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  29117. {
  29118. EXPECT_DECLS;
  29119. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29120. ASN1_INTEGER *a = NULL;
  29121. unsigned char *pp = NULL,*tpp = NULL;
  29122. int ret = 0;
  29123. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  29124. /* Invalid parameter testing. */
  29125. /* Set pp to an invalid value. */
  29126. pp = NULL;
  29127. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  29128. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  29129. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  29130. /* 40 */
  29131. if (a != NULL) {
  29132. a->intData[0] = ASN_INTEGER;
  29133. a->intData[1] = 1;
  29134. a->intData[2] = 40;
  29135. }
  29136. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29137. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29138. DYNAMIC_TYPE_TMP_BUFFER));
  29139. tpp = pp;
  29140. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29141. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29142. tpp--;
  29143. ExpectIntEQ(*tpp, 40);
  29144. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29145. pp = NULL;
  29146. /* 128 */
  29147. if (a != NULL) {
  29148. a->intData[0] = ASN_INTEGER;
  29149. a->intData[1] = 1;
  29150. a->intData[2] = 128;
  29151. }
  29152. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29153. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29154. DYNAMIC_TYPE_TMP_BUFFER));
  29155. tpp = pp;
  29156. if (tpp != NULL) {
  29157. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29158. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29159. tpp--;
  29160. ExpectIntEQ(*(tpp--), 128);
  29161. ExpectIntEQ(*tpp, 0);
  29162. }
  29163. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29164. pp = NULL;
  29165. /* -40 */
  29166. if (a != NULL) {
  29167. a->intData[0] = ASN_INTEGER;
  29168. a->intData[1] = 1;
  29169. a->intData[2] = 40;
  29170. a->negative = 1;
  29171. }
  29172. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29173. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29174. DYNAMIC_TYPE_TMP_BUFFER));
  29175. tpp = pp;
  29176. if (tpp != NULL) {
  29177. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29178. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29179. tpp--;
  29180. ExpectIntEQ(*tpp, 216);
  29181. }
  29182. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29183. pp = NULL;
  29184. /* -128 */
  29185. if (a != NULL) {
  29186. a->intData[0] = ASN_INTEGER;
  29187. a->intData[1] = 1;
  29188. a->intData[2] = 128;
  29189. a->negative = 1;
  29190. }
  29191. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29192. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29193. DYNAMIC_TYPE_TMP_BUFFER));
  29194. tpp = pp;
  29195. if (tpp != NULL) {
  29196. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29197. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29198. tpp--;
  29199. ExpectIntEQ(*tpp, 128);
  29200. }
  29201. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29202. pp = NULL;
  29203. /* -200 */
  29204. if (a != NULL) {
  29205. a->intData[0] = ASN_INTEGER;
  29206. a->intData[1] = 1;
  29207. a->intData[2] = 200;
  29208. a->negative = 1;
  29209. }
  29210. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29211. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29212. DYNAMIC_TYPE_TMP_BUFFER));
  29213. tpp = pp;
  29214. if (tpp != NULL) {
  29215. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29216. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29217. tpp--;
  29218. ExpectIntEQ(*(tpp--), 56);
  29219. ExpectIntEQ(*tpp, 255);
  29220. }
  29221. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29222. pp = NULL;
  29223. /* Empty */
  29224. if (a != NULL) {
  29225. a->intData[0] = ASN_INTEGER;
  29226. a->intData[1] = 0;
  29227. a->negative = 0;
  29228. }
  29229. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29230. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29231. DYNAMIC_TYPE_TMP_BUFFER));
  29232. tpp = pp;
  29233. if (tpp != NULL) {
  29234. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29235. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29236. tpp--;
  29237. ExpectIntEQ(*tpp, 0);
  29238. }
  29239. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29240. pp = NULL;
  29241. /* 0 */
  29242. if (a != NULL) {
  29243. a->intData[0] = ASN_INTEGER;
  29244. a->intData[1] = 1;
  29245. a->intData[2] = 0;
  29246. a->negative = 1;
  29247. }
  29248. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  29249. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29250. DYNAMIC_TYPE_TMP_BUFFER));
  29251. if (tpp != NULL) {
  29252. tpp = pp;
  29253. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29254. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  29255. tpp--;
  29256. ExpectIntEQ(*tpp, 0);
  29257. }
  29258. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29259. pp = NULL;
  29260. /* 0x100 */
  29261. if (a != NULL) {
  29262. a->intData[0] = ASN_INTEGER;
  29263. a->intData[1] = 2;
  29264. a->intData[2] = 0x01;
  29265. a->intData[3] = 0x00;
  29266. a->negative = 0;
  29267. }
  29268. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29269. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29270. DYNAMIC_TYPE_TMP_BUFFER));
  29271. if (tpp != NULL) {
  29272. tpp = pp;
  29273. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29274. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29275. tpp -= 2;
  29276. ExpectIntEQ(tpp[0], 0x01);
  29277. ExpectIntEQ(tpp[1], 0x00);
  29278. }
  29279. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29280. pp = NULL;
  29281. /* -0x8000 => 0x8000 */
  29282. if (a != NULL) {
  29283. a->intData[0] = ASN_INTEGER;
  29284. a->intData[1] = 2;
  29285. a->intData[2] = 0x80;
  29286. a->intData[3] = 0x00;
  29287. a->negative = 1;
  29288. }
  29289. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  29290. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29291. DYNAMIC_TYPE_TMP_BUFFER));
  29292. tpp = pp;
  29293. if (tpp != NULL) {
  29294. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29295. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  29296. tpp -= 2;
  29297. ExpectIntEQ(tpp[0], 0x80);
  29298. ExpectIntEQ(tpp[1], 0x00);
  29299. }
  29300. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29301. pp = NULL;
  29302. /* -0x8001 => 0xFF7FFF */
  29303. if (a != NULL) {
  29304. a->intData[0] = ASN_INTEGER;
  29305. a->intData[1] = 2;
  29306. a->intData[2] = 0x80;
  29307. a->intData[3] = 0x01;
  29308. a->negative = 1;
  29309. }
  29310. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  29311. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  29312. DYNAMIC_TYPE_TMP_BUFFER));
  29313. tpp = pp;
  29314. if (tpp != NULL) {
  29315. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  29316. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  29317. tpp -= 3;
  29318. ExpectIntEQ(tpp[0], 0xFF);
  29319. ExpectIntEQ(tpp[1], 0x7F);
  29320. ExpectIntEQ(tpp[2], 0xFF);
  29321. }
  29322. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29323. wolfSSL_ASN1_INTEGER_free(a);
  29324. #endif /* OPENSSL_EXTRA && !NO_ASN */
  29325. return EXPECT_RESULT();
  29326. }
  29327. static int test_wolfSSL_ASN1_OBJECT(void)
  29328. {
  29329. EXPECT_DECLS;
  29330. #if defined(OPENSSL_EXTRA)
  29331. ASN1_OBJECT* a = NULL;
  29332. ASN1_OBJECT s;
  29333. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  29334. /* Invalid parameter testing. */
  29335. ASN1_OBJECT_free(NULL);
  29336. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  29337. /* Test that a static ASN1_OBJECT can be freed. */
  29338. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  29339. ASN1_OBJECT_free(&s);
  29340. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  29341. ASN1_OBJECT_free(a);
  29342. a = NULL;
  29343. s.obj = der;
  29344. s.objSz = sizeof(der);
  29345. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  29346. ASN1_OBJECT_free(a);
  29347. ASN1_OBJECT_free(&s);
  29348. #endif /* OPENSSL_EXTRA */
  29349. return EXPECT_RESULT();
  29350. }
  29351. static int test_wolfSSL_ASN1_get_object(void)
  29352. {
  29353. EXPECT_DECLS;
  29354. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29355. const unsigned char* derBuf = cliecc_cert_der_256;
  29356. const unsigned char* nullPtr = NULL;
  29357. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  29358. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  29359. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  29360. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  29361. const unsigned char* p;
  29362. unsigned char objDer[8];
  29363. unsigned char* der;
  29364. unsigned char* derPtr;
  29365. int len = sizeof_cliecc_cert_der_256;
  29366. long asnLen = 0;
  29367. int tag = 0;
  29368. int cls = 0;
  29369. ASN1_OBJECT* a = NULL;
  29370. ASN1_OBJECT s;
  29371. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  29372. /* Invalid encoding at length. */
  29373. p = objDerInvalidLen;
  29374. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  29375. 0x80);
  29376. p = objDerBadLen;
  29377. /* Error = 0x80, Constructed = 0x20 */
  29378. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  29379. 0x80 | 0x20);
  29380. /* Read a couple TLV triplets and make sure they match the expected values
  29381. */
  29382. /* SEQUENCE */
  29383. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  29384. ExpectIntEQ(asnLen, 861);
  29385. ExpectIntEQ(tag, 0x10);
  29386. ExpectIntEQ(cls, 0);
  29387. /* SEQUENCE */
  29388. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29389. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29390. ExpectIntEQ(asnLen, 772);
  29391. ExpectIntEQ(tag, 0x10);
  29392. ExpectIntEQ(cls, 0);
  29393. /* [0] */
  29394. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29395. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29396. ExpectIntEQ(asnLen, 3);
  29397. ExpectIntEQ(tag, 0);
  29398. ExpectIntEQ(cls, 0x80);
  29399. /* INTEGER */
  29400. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29401. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29402. ExpectIntEQ(asnLen, 1);
  29403. ExpectIntEQ(tag, 0x2);
  29404. ExpectIntEQ(cls, 0);
  29405. derBuf += asnLen;
  29406. /* INTEGER */
  29407. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29408. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29409. ExpectIntEQ(asnLen, 20);
  29410. ExpectIntEQ(tag, 0x2);
  29411. ExpectIntEQ(cls, 0);
  29412. derBuf += asnLen;
  29413. /* SEQUENCE */
  29414. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  29415. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  29416. ExpectIntEQ(asnLen, 10);
  29417. ExpectIntEQ(tag, 0x10);
  29418. ExpectIntEQ(cls, 0);
  29419. /* Found OBJECT_ID. */
  29420. /* Invalid parameter testing. */
  29421. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  29422. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  29423. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  29424. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  29425. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  29426. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  29427. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  29428. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  29429. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  29430. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  29431. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  29432. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  29433. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  29434. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  29435. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  29436. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  29437. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  29438. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  29439. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  29440. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  29441. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  29442. /* Invalid encoding at length. */
  29443. p = objDerInvalidLen;
  29444. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  29445. p = objDerBadLen;
  29446. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  29447. p = objDerNotObj;
  29448. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  29449. p = objDerNoData;
  29450. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  29451. /* Create an ASN OBJECT from content */
  29452. p = derBuf + 2;
  29453. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  29454. ASN1_OBJECT_free(a);
  29455. a = NULL;
  29456. /* Create an ASN OBJECT from DER */
  29457. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  29458. /* Invalid parameter testing. */
  29459. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  29460. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  29461. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  29462. der = NULL;
  29463. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  29464. derPtr = objDer;
  29465. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  29466. ExpectPtrNE(derPtr, objDer);
  29467. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  29468. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  29469. ASN1_OBJECT_free(a);
  29470. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29471. return EXPECT_RESULT();
  29472. }
  29473. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  29474. {
  29475. EXPECT_DECLS;
  29476. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  29477. ASN1_OBJECT* obj = NULL;
  29478. ASN1_OBJECT* a = NULL;
  29479. BIO *bio = NULL;
  29480. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  29481. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  29482. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  29483. const unsigned char* p;
  29484. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  29485. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  29486. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  29487. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  29488. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  29489. /* No DER encoding in ASN1_OBJECT. */
  29490. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  29491. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29492. ASN1_OBJECT_free(a);
  29493. a = NULL;
  29494. /* DER encoding - not OBJECT_ID */
  29495. p = notObjDer;
  29496. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29497. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29498. ASN1_OBJECT_free(a);
  29499. a = NULL;
  29500. /* Bad length encoding. */
  29501. p = badLenDer;
  29502. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29503. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29504. ASN1_OBJECT_free(a);
  29505. a = NULL;
  29506. /* Good encoding - but unknown. */
  29507. p = goodDer;
  29508. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  29509. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  29510. ASN1_OBJECT_free(a);
  29511. BIO_free(bio);
  29512. ASN1_OBJECT_free(obj);
  29513. #endif
  29514. return EXPECT_RESULT();
  29515. }
  29516. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  29517. {
  29518. EXPECT_DECLS;
  29519. #if defined(OPENSSL_EXTRA) && \
  29520. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  29521. char buf[50] = {0};
  29522. ASN1_OBJECT* obj;
  29523. const char* oid = "2.5.29.19";
  29524. const char* ln = "X509v3 Basic Constraints";
  29525. obj = NULL;
  29526. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  29527. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  29528. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  29529. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  29530. XMEMSET(buf, 0, sizeof(buf));
  29531. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  29532. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  29533. ASN1_OBJECT_free(obj);
  29534. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  29535. return EXPECT_RESULT();
  29536. }
  29537. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  29538. {
  29539. EXPECT_DECLS;
  29540. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  29541. WOLFSSL_STACK* sk = NULL;
  29542. WOLFSSL_ASN1_OBJECT* obj;
  29543. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29544. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29545. wolfSSL_sk_ASN1_OBJECT_free(sk);
  29546. sk = NULL;
  29547. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29548. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  29549. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  29550. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  29551. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  29552. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  29553. sk = NULL;
  29554. /* obj freed in pop_free call. */
  29555. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29556. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  29557. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  29558. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  29559. wolfSSL_sk_ASN1_OBJECT_free(sk);
  29560. wolfSSL_ASN1_OBJECT_free(obj);
  29561. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  29562. return EXPECT_RESULT();
  29563. }
  29564. static int test_wolfSSL_ASN1_STRING(void)
  29565. {
  29566. EXPECT_DECLS;
  29567. #if defined(OPENSSL_EXTRA)
  29568. ASN1_STRING* str = NULL;
  29569. ASN1_STRING* c = NULL;
  29570. const char data[] = "hello wolfSSL";
  29571. const char data2[] = "Same len data";
  29572. const char longData[] =
  29573. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  29574. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29575. ASN1_STRING_free(str);
  29576. str = NULL;
  29577. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29578. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  29579. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  29580. /* Check setting to NULL works. */
  29581. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  29582. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  29583. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  29584. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  29585. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  29586. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  29587. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  29588. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  29589. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  29590. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  29591. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  29592. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  29593. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  29594. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  29595. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  29596. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  29597. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  29598. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  29599. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  29600. /* Check setting back to smaller size frees dynamic data. */
  29601. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  29602. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  29603. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  29604. ExpectNull(ASN1_STRING_get0_data(NULL));
  29605. ExpectNotNull(ASN1_STRING_get0_data(str));
  29606. ExpectNull(ASN1_STRING_data(NULL));
  29607. ExpectNotNull(ASN1_STRING_data(str));
  29608. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  29609. ExpectIntGT(ASN1_STRING_length(str), 0);
  29610. ASN1_STRING_free(c);
  29611. ASN1_STRING_free(str);
  29612. ASN1_STRING_free(NULL);
  29613. #ifndef NO_WOLFSSL_STUB
  29614. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  29615. #endif
  29616. #endif
  29617. return EXPECT_RESULT();
  29618. }
  29619. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  29620. {
  29621. EXPECT_DECLS;
  29622. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  29623. !defined(NO_FILESYSTEM)
  29624. WOLFSSL_X509* x509 = NULL;
  29625. WOLFSSL_X509_NAME* subject = NULL;
  29626. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  29627. WOLFSSL_ASN1_STRING* a = NULL;
  29628. FILE* file = XBADFILE;
  29629. int idx = 0;
  29630. char targetOutput[16] = "www.wolfssl.com";
  29631. unsigned char* actual_output = NULL;
  29632. int len = 0;
  29633. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  29634. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  29635. if (file != NULL)
  29636. fclose(file);
  29637. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  29638. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  29639. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  29640. NID_commonName, -1)), 5);
  29641. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  29642. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  29643. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  29644. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  29645. a = NULL;
  29646. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  29647. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  29648. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  29649. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  29650. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  29651. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  29652. wolfSSL_X509_free(x509);
  29653. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29654. ExpectNotNull(a = ASN1_STRING_new());
  29655. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  29656. ASN1_STRING_free(a);
  29657. #endif
  29658. return EXPECT_RESULT();
  29659. }
  29660. static int test_wolfSSL_i2s_ASN1_STRING(void)
  29661. {
  29662. EXPECT_DECLS;
  29663. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  29664. WOLFSSL_ASN1_STRING* str = NULL;
  29665. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  29666. char* ret = NULL;
  29667. ExpectNotNull(str = ASN1_STRING_new());
  29668. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  29669. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29670. ret = NULL;
  29671. /* No data. */
  29672. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29673. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29674. ret = NULL;
  29675. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  29676. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29677. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29678. ret = NULL;
  29679. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  29680. /* No type. */
  29681. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  29682. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  29683. ASN1_STRING_free(str);
  29684. #endif
  29685. return EXPECT_RESULT();
  29686. }
  29687. static int test_wolfSSL_ASN1_STRING_canon(void)
  29688. {
  29689. EXPECT_DECLS;
  29690. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  29691. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  29692. defined(OPENSSL_EXTRA_X509_SMALL))
  29693. WOLFSSL_ASN1_STRING* orig = NULL;
  29694. WOLFSSL_ASN1_STRING* canon = NULL;
  29695. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  29696. const char* whitespaceOnly = "\t\r\n";
  29697. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  29698. const char* canonData = "\x01 \x02 \xff tt";
  29699. const char longData[] =
  29700. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  29701. ExpectNotNull(orig = ASN1_STRING_new());
  29702. ExpectNotNull(canon = ASN1_STRING_new());
  29703. /* Invalid parameter testing. */
  29704. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  29705. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  29706. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  29707. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29708. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29709. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  29710. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29711. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29712. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  29713. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29714. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29715. ASN1_STRING_free(orig);
  29716. orig = NULL;
  29717. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29718. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  29719. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29720. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  29721. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29722. ASN1_STRING_free(orig);
  29723. orig = NULL;
  29724. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  29725. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  29726. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  29727. ASN1_STRING_free(orig);
  29728. orig = NULL;
  29729. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  29730. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  29731. ASN1_STRING_free(orig);
  29732. ASN1_STRING_free(canon);
  29733. #endif
  29734. #endif
  29735. return EXPECT_RESULT();
  29736. }
  29737. static int test_wolfSSL_ASN1_STRING_print(void)
  29738. {
  29739. EXPECT_DECLS;
  29740. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  29741. !defined(NO_BIO)
  29742. ASN1_STRING* asnStr = NULL;
  29743. const char HELLO_DATA[]= \
  29744. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  29745. #define MAX_UNPRINTABLE_CHAR 32
  29746. #define MAX_BUF 255
  29747. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  29748. unsigned char expected[sizeof(unprintableData)+1];
  29749. unsigned char rbuf[MAX_BUF];
  29750. BIO *bio = NULL;
  29751. int p_len;
  29752. int i;
  29753. /* setup */
  29754. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  29755. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  29756. expected[i] = (unsigned char)HELLO_DATA[i];
  29757. }
  29758. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  29759. unprintableData[sizeof(HELLO_DATA)+i] = i;
  29760. if (i == (int)'\n' || i == (int)'\r')
  29761. expected[sizeof(HELLO_DATA)+i] = i;
  29762. else
  29763. expected[sizeof(HELLO_DATA)+i] = '.';
  29764. }
  29765. unprintableData[sizeof(unprintableData)-1] = '\0';
  29766. expected[sizeof(expected)-1] = '\0';
  29767. XMEMSET(rbuf, 0, MAX_BUF);
  29768. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29769. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  29770. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29771. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  29772. (int)sizeof(unprintableData)), 1);
  29773. /* test */
  29774. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  29775. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  29776. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  29777. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  29778. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  29779. ExpectStrEQ((char*)rbuf, (const char*)expected);
  29780. BIO_free(bio);
  29781. bio = NULL;
  29782. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29783. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29784. /* Ensure there is 0 bytes available to write into. */
  29785. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  29786. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29787. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  29788. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29789. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  29790. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  29791. BIO_free(bio);
  29792. ASN1_STRING_free(asnStr);
  29793. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  29794. return EXPECT_RESULT();
  29795. }
  29796. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  29797. {
  29798. EXPECT_DECLS;
  29799. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  29800. ASN1_STRING* asn_str = NULL;
  29801. const char data[] = "Hello wolfSSL!";
  29802. ASN1_STRING* esc_str = NULL;
  29803. const char esc_data[] = "a+;<>";
  29804. ASN1_STRING* neg_int = NULL;
  29805. const char neg_int_data[] = "\xff";
  29806. ASN1_STRING* neg_enum = NULL;
  29807. const char neg_enum_data[] = "\xff";
  29808. BIO *bio = NULL;
  29809. BIO *fixed = NULL;
  29810. unsigned long flags;
  29811. int p_len;
  29812. unsigned char rbuf[255];
  29813. /* setup */
  29814. XMEMSET(rbuf, 0, 255);
  29815. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  29816. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  29817. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  29818. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29819. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  29820. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29821. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  29822. sizeof(esc_data)), 1);
  29823. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  29824. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  29825. sizeof(neg_int_data) - 1), 1);
  29826. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  29827. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  29828. sizeof(neg_enum_data) - 1), 1);
  29829. /* Invalid parameter testing. */
  29830. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  29831. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  29832. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  29833. /* no flags */
  29834. XMEMSET(rbuf, 0, 255);
  29835. flags = 0;
  29836. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  29837. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  29838. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  29839. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29840. /* Ensure there is 0 bytes available to write into. */
  29841. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29842. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29843. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29844. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29845. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  29846. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29847. /* RFC2253 Escape */
  29848. XMEMSET(rbuf, 0, 255);
  29849. flags = ASN1_STRFLGS_ESC_2253;
  29850. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  29851. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  29852. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  29853. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29854. /* Ensure there is 0 bytes available to write into. */
  29855. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29856. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29857. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29858. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29859. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  29860. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  29861. /* Show type */
  29862. XMEMSET(rbuf, 0, 255);
  29863. flags = ASN1_STRFLGS_SHOW_TYPE;
  29864. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  29865. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  29866. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  29867. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29868. /* Ensure there is 0 bytes available to write into. */
  29869. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29870. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29871. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29872. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29873. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  29874. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29875. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  29876. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29877. /* Dump All */
  29878. XMEMSET(rbuf, 0, 255);
  29879. flags = ASN1_STRFLGS_DUMP_ALL;
  29880. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  29881. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  29882. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  29883. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29884. /* Ensure there is 0 bytes available to write into. */
  29885. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29886. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29887. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29888. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29889. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29890. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29891. /* Dump Der */
  29892. XMEMSET(rbuf, 0, 255);
  29893. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  29894. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  29895. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  29896. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  29897. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29898. /* Ensure there is 0 bytes available to write into. */
  29899. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  29900. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29901. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  29902. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29903. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  29904. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29905. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  29906. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  29907. /* Dump All + Show type */
  29908. XMEMSET(rbuf, 0, 255);
  29909. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29910. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  29911. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  29912. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  29913. /* Dump All + Show type - Negative Integer. */
  29914. XMEMSET(rbuf, 0, 255);
  29915. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29916. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  29917. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  29918. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  29919. /* Dump All + Show type - Negative Enumerated. */
  29920. XMEMSET(rbuf, 0, 255);
  29921. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  29922. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  29923. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  29924. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  29925. BIO_free(fixed);
  29926. BIO_free(bio);
  29927. ASN1_STRING_free(asn_str);
  29928. ASN1_STRING_free(esc_str);
  29929. ASN1_STRING_free(neg_int);
  29930. ASN1_STRING_free(neg_enum);
  29931. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  29932. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  29933. #endif
  29934. return EXPECT_RESULT();
  29935. }
  29936. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  29937. {
  29938. EXPECT_DECLS;
  29939. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  29940. ASN1_STRING* asn1str_test = NULL;
  29941. ASN1_STRING* asn1str_answer = NULL;
  29942. /* Each character is encoded using 4 bytes */
  29943. char input[] = {
  29944. 0, 0, 0, 'T',
  29945. 0, 0, 0, 'e',
  29946. 0, 0, 0, 's',
  29947. 0, 0, 0, 't',
  29948. };
  29949. char output[] = "Test";
  29950. char badInput[] = {
  29951. 1, 0, 0, 'T',
  29952. 0, 1, 0, 'e',
  29953. 0, 0, 1, 's',
  29954. };
  29955. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  29956. /* Test wrong type. */
  29957. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  29958. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29959. ASN1_STRING_free(asn1str_test);
  29960. asn1str_test = NULL;
  29961. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  29962. /* Test bad length. */
  29963. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  29964. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29965. /* Test bad input. */
  29966. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  29967. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29968. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  29969. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29970. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  29971. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  29972. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  29973. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  29974. ExpectNotNull(
  29975. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  29976. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  29977. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  29978. ASN1_STRING_free(asn1str_test);
  29979. ASN1_STRING_free(asn1str_answer);
  29980. #endif /* OPENSSL_ALL && !NO_ASN */
  29981. return EXPECT_RESULT();
  29982. }
  29983. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  29984. {
  29985. EXPECT_DECLS;
  29986. #if defined(OPENSSL_EXTRA)
  29987. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  29988. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  29989. if (asn1_gtime != NULL)
  29990. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  29991. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  29992. #endif /* OPENSSL_EXTRA */
  29993. return EXPECT_RESULT();
  29994. }
  29995. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  29996. {
  29997. EXPECT_DECLS;
  29998. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  29999. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  30000. BIO* bio = NULL;
  30001. unsigned char buf[24];
  30002. int i;
  30003. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30004. BIO_set_write_buf_size(bio, 24);
  30005. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  30006. /* Type not set. */
  30007. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30008. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  30009. /* Invalid parameters testing. */
  30010. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  30011. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  30012. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  30013. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  30014. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  30015. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  30016. BIO_free(bio);
  30017. bio = NULL;
  30018. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  30019. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  30020. /* Ensure there is 0 bytes available to write into. */
  30021. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  30022. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30023. for (i = 1; i < 20; i++) {
  30024. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  30025. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  30026. }
  30027. BIO_free(bio);
  30028. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  30029. #endif /* OPENSSL_EXTRA */
  30030. return EXPECT_RESULT();
  30031. }
  30032. static int test_wolfSSL_ASN1_TIME(void)
  30033. {
  30034. EXPECT_DECLS;
  30035. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30036. WOLFSSL_ASN1_TIME* asn_time = NULL;
  30037. unsigned char *data;
  30038. ExpectNotNull(asn_time = ASN1_TIME_new());
  30039. #ifndef NO_WOLFSSL_STUB
  30040. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  30041. #endif
  30042. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  30043. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  30044. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  30045. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  30046. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  30047. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  30048. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  30049. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  30050. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  30051. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  30052. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  30053. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  30054. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  30055. ASN1_TIME_free(asn_time);
  30056. ASN1_TIME_free(NULL);
  30057. #endif
  30058. return EXPECT_RESULT();
  30059. }
  30060. static int test_wolfSSL_ASN1_TIME_to_string(void)
  30061. {
  30062. EXPECT_DECLS;
  30063. #ifndef NO_ASN_TIME
  30064. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30065. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  30066. WOLFSSL_ASN1_TIME* t = NULL;
  30067. char buf[ASN_GENERALIZED_TIME_SIZE];
  30068. ExpectNotNull((t = ASN1_TIME_new()));
  30069. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  30070. /* Invalid parameter testing. */
  30071. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  30072. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  30073. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  30074. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  30075. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  30076. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  30077. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  30078. /* Buffer needs to be longer than minimum of 5 characters. */
  30079. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  30080. ASN1_TIME_free(t);
  30081. #endif
  30082. #endif /* NO_ASN_TIME */
  30083. return EXPECT_RESULT();
  30084. }
  30085. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  30086. {
  30087. EXPECT_DECLS;
  30088. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30089. ASN1_TIME* fromTime = NULL;
  30090. ASN1_TIME* closeToTime = NULL;
  30091. ASN1_TIME* toTime = NULL;
  30092. ASN1_TIME* invalidTime = NULL;
  30093. int daysDiff = 0;
  30094. int secsDiff = 0;
  30095. ExpectNotNull((fromTime = ASN1_TIME_new()));
  30096. /* Feb 22, 2003, 21:15:15 */
  30097. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  30098. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  30099. /* Feb 22, 2003, 21:16:15 */
  30100. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  30101. ExpectNotNull((toTime = ASN1_TIME_new()));
  30102. /* Dec 19, 2010, 18:10:11 */
  30103. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  30104. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  30105. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  30106. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  30107. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  30108. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  30109. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30110. /* Error conditions. */
  30111. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  30112. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  30113. /* If both times are NULL, difference is 0. */
  30114. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  30115. ExpectIntEQ(daysDiff, 0);
  30116. ExpectIntEQ(secsDiff, 0);
  30117. /* If one time is NULL, it defaults to the current time. */
  30118. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  30119. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  30120. /* Normal operation. Both times non-NULL. */
  30121. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30122. ExpectIntEQ(daysDiff, 2856);
  30123. ExpectIntEQ(secsDiff, 75296);
  30124. /* Swapping the times should return negative values. */
  30125. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  30126. ExpectIntEQ(daysDiff, -2856);
  30127. ExpectIntEQ(secsDiff, -75296);
  30128. /* Compare with invalid time string. */
  30129. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  30130. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  30131. /* Compare with days difference of 0. */
  30132. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  30133. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  30134. /* Days and seconds differences not 0. */
  30135. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  30136. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  30137. /* Same time. */
  30138. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  30139. /* Compare regression test: No seconds difference, just difference in days.
  30140. */
  30141. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  30142. ASN1_TIME_set_string(toTime, "19800101000000Z");
  30143. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  30144. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  30145. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  30146. /* Edge case with Unix epoch. */
  30147. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  30148. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  30149. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30150. ExpectIntEQ(daysDiff, 3652);
  30151. ExpectIntEQ(secsDiff, 0);
  30152. /* Edge case with year > 2038 (year 2038 problem). */
  30153. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  30154. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  30155. ExpectIntEQ(daysDiff, 2932896);
  30156. ExpectIntEQ(secsDiff, 86399);
  30157. ASN1_TIME_free(fromTime);
  30158. ASN1_TIME_free(closeToTime);
  30159. ASN1_TIME_free(toTime);
  30160. ASN1_TIME_free(invalidTime);
  30161. #endif
  30162. return EXPECT_RESULT();
  30163. }
  30164. static int test_wolfSSL_ASN1_TIME_adj(void)
  30165. {
  30166. EXPECT_DECLS;
  30167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  30168. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  30169. const int year = 365*24*60*60;
  30170. const int day = 24*60*60;
  30171. const int hour = 60*60;
  30172. const int mini = 60;
  30173. const byte asn_utc_time = ASN_UTC_TIME;
  30174. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30175. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  30176. #endif
  30177. WOLFSSL_ASN1_TIME* asn_time = NULL;
  30178. WOLFSSL_ASN1_TIME* s = NULL;
  30179. int offset_day;
  30180. long offset_sec;
  30181. char date_str[CTC_DATE_SIZE + 1];
  30182. time_t t;
  30183. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  30184. /* UTC notation test */
  30185. /* 2000/2/15 20:30:00 */
  30186. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  30187. offset_day = 7;
  30188. offset_sec = 45 * mini;
  30189. /* offset_sec = -45 * min;*/
  30190. ExpectNotNull(asn_time =
  30191. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  30192. ExpectTrue(asn_time->type == asn_utc_time);
  30193. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30194. CTC_DATE_SIZE));
  30195. date_str[CTC_DATE_SIZE] = '\0';
  30196. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  30197. /* negative offset */
  30198. offset_sec = -45 * mini;
  30199. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  30200. ExpectNotNull(asn_time);
  30201. ExpectTrue(asn_time->type == asn_utc_time);
  30202. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30203. CTC_DATE_SIZE));
  30204. date_str[CTC_DATE_SIZE] = '\0';
  30205. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  30206. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30207. s = NULL;
  30208. XMEMSET(date_str, 0, sizeof(date_str));
  30209. /* Generalized time will overflow time_t if not long */
  30210. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  30211. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  30212. DYNAMIC_TYPE_OPENSSL);
  30213. /* GeneralizedTime notation test */
  30214. /* 2055/03/01 09:00:00 */
  30215. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  30216. offset_day = 12;
  30217. offset_sec = 10 * mini;
  30218. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  30219. offset_sec));
  30220. ExpectTrue(asn_time->type == asn_gen_time);
  30221. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30222. CTC_DATE_SIZE));
  30223. date_str[CTC_DATE_SIZE] = '\0';
  30224. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  30225. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  30226. s = NULL;
  30227. XMEMSET(date_str, 0, sizeof(date_str));
  30228. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  30229. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  30230. s = NULL;
  30231. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  30232. offset_day = 7;
  30233. offset_sec = 45 * mini;
  30234. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  30235. offset_sec));
  30236. ExpectTrue(asn_time->type == asn_utc_time);
  30237. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30238. CTC_DATE_SIZE));
  30239. date_str[CTC_DATE_SIZE] = '\0';
  30240. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30241. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30242. asn_time = NULL;
  30243. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  30244. offset_sec));
  30245. ExpectTrue(asn_time->type == asn_utc_time);
  30246. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  30247. CTC_DATE_SIZE));
  30248. date_str[CTC_DATE_SIZE] = '\0';
  30249. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  30250. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  30251. #endif
  30252. return EXPECT_RESULT();
  30253. }
  30254. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  30255. {
  30256. EXPECT_DECLS;
  30257. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30258. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  30259. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  30260. ASN1_TIME asnTime;
  30261. struct tm tm;
  30262. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  30263. XMEMSET(&tm, 0, sizeof(struct tm));
  30264. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30265. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  30266. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  30267. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30268. ExpectIntEQ(tm.tm_sec, 15);
  30269. ExpectIntEQ(tm.tm_min, 15);
  30270. ExpectIntEQ(tm.tm_hour, 21);
  30271. ExpectIntEQ(tm.tm_mday, 22);
  30272. ExpectIntEQ(tm.tm_mon, 1);
  30273. ExpectIntEQ(tm.tm_year, 100);
  30274. ExpectIntEQ(tm.tm_isdst, 0);
  30275. #ifdef XMKTIME
  30276. ExpectIntEQ(tm.tm_wday, 2);
  30277. ExpectIntEQ(tm.tm_yday, 52);
  30278. #endif
  30279. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  30280. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30281. ExpectIntEQ(tm.tm_year, 50);
  30282. /* Get current time. */
  30283. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  30284. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  30285. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  30286. /* 0 length. */
  30287. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30288. /* No type. */
  30289. asnTime.length = 1;
  30290. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30291. /* Not UTCTIME length. */
  30292. asnTime.type = V_ASN1_UTCTIME;
  30293. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30294. /* Not GENERALIZEDTIME length. */
  30295. asnTime.type = V_ASN1_GENERALIZEDTIME;
  30296. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30297. /* Not Zulu timezone. */
  30298. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  30299. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30300. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  30301. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  30302. #ifdef XMKTIME
  30303. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  30304. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  30305. ExpectIntEQ(tm.tm_sec, 47);
  30306. ExpectIntEQ(tm.tm_min, 22);
  30307. ExpectIntEQ(tm.tm_hour, 21);
  30308. ExpectIntEQ(tm.tm_mday, 12);
  30309. ExpectIntEQ(tm.tm_mon, 4);
  30310. ExpectIntEQ(tm.tm_year, 123);
  30311. ExpectIntEQ(tm.tm_wday, 5);
  30312. ExpectIntEQ(tm.tm_yday, 131);
  30313. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  30314. fields are zeroed out as expected. */
  30315. ExpectIntEQ(tm.tm_isdst, 0);
  30316. #endif
  30317. #endif
  30318. return EXPECT_RESULT();
  30319. }
  30320. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  30321. {
  30322. EXPECT_DECLS;
  30323. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  30324. WOLFSSL_ASN1_TIME *t = NULL;
  30325. WOLFSSL_ASN1_TIME *out = NULL;
  30326. WOLFSSL_ASN1_TIME *gtime = NULL;
  30327. int tlen = 0;
  30328. unsigned char *data = NULL;
  30329. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  30330. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  30331. /* type not set. */
  30332. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30333. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30334. t = NULL;
  30335. /* UTC Time test */
  30336. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  30337. if (t != NULL) {
  30338. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  30339. t->type = ASN_UTC_TIME;
  30340. t->length = ASN_UTC_TIME_SIZE;
  30341. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  30342. }
  30343. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  30344. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  30345. out = NULL;
  30346. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30347. wolfSSL_ASN1_TIME_free(gtime);
  30348. gtime = NULL;
  30349. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  30350. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30351. ExpectPtrEq(gtime, out);
  30352. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30353. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30354. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30355. /* Generalized Time test */
  30356. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30357. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  30358. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  30359. if (t != NULL) {
  30360. t->type = ASN_GENERALIZED_TIME;
  30361. t->length = ASN_GENERALIZED_TIME_SIZE;
  30362. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  30363. }
  30364. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  30365. ASN_GENERALIZED_TIME_SIZE);
  30366. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  30367. "20050727123456Z");
  30368. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30369. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30370. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30371. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30372. /* UTC Time to Generalized Time 1900's test */
  30373. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30374. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  30375. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  30376. if (t != NULL) {
  30377. t->type = ASN_UTC_TIME;
  30378. t->length = ASN_UTC_TIME_SIZE;
  30379. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  30380. }
  30381. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  30382. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30383. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30384. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  30385. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30386. /* Null parameter test */
  30387. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  30388. gtime = NULL;
  30389. out = NULL;
  30390. if (t != NULL) {
  30391. t->type = ASN_UTC_TIME;
  30392. t->length = ASN_UTC_TIME_SIZE;
  30393. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  30394. }
  30395. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  30396. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  30397. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  30398. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  30399. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30400. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  30401. #endif
  30402. return EXPECT_RESULT();
  30403. }
  30404. static int test_wolfSSL_ASN1_TIME_print(void)
  30405. {
  30406. EXPECT_DECLS;
  30407. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  30408. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  30409. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  30410. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  30411. !defined(NO_ASN_TIME)
  30412. BIO* bio = NULL;
  30413. BIO* fixed = NULL;
  30414. X509* x509 = NULL;
  30415. const unsigned char* der = client_cert_der_2048;
  30416. ASN1_TIME* notAfter = NULL;
  30417. ASN1_TIME* notBefore = NULL;
  30418. unsigned char buf[25];
  30419. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30420. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  30421. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  30422. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  30423. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  30424. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  30425. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  30426. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  30427. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  30428. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30429. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  30430. /* Test BIO_write fails. */
  30431. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  30432. /* Ensure there is 0 bytes available to write into. */
  30433. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  30434. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30435. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  30436. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30437. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  30438. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  30439. /* create a bad time and test results */
  30440. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  30441. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  30442. if (EXPECT_SUCCESS()) {
  30443. notAfter->data[8] = 0;
  30444. notAfter->data[3] = 0;
  30445. }
  30446. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  30447. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  30448. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  30449. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  30450. BIO_free(bio);
  30451. BIO_free(fixed);
  30452. X509_free(x509);
  30453. #endif
  30454. return EXPECT_RESULT();
  30455. }
  30456. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  30457. {
  30458. EXPECT_DECLS;
  30459. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  30460. BIO* bio = NULL;
  30461. ASN1_UTCTIME* utc = NULL;
  30462. unsigned char buf[25];
  30463. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  30464. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  30465. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  30466. /* Valid date */
  30467. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30468. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  30469. DYNAMIC_TYPE_ASN1));
  30470. if (utc != NULL) {
  30471. utc->type = ASN_UTC_TIME;
  30472. utc->length = ASN_UTC_TIME_SIZE;
  30473. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  30474. }
  30475. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  30476. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  30477. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  30478. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  30479. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  30480. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  30481. XMEMSET(buf, 0, sizeof(buf));
  30482. BIO_free(bio);
  30483. bio = NULL;
  30484. /* Invalid format */
  30485. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  30486. if (utc != NULL) {
  30487. utc->type = ASN_UTC_TIME;
  30488. utc->length = ASN_UTC_TIME_SIZE;
  30489. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  30490. }
  30491. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  30492. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  30493. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  30494. /* Invalid type */
  30495. if (utc != NULL) {
  30496. utc->type = ASN_GENERALIZED_TIME;
  30497. utc->length = ASN_GENERALIZED_TIME_SIZE;
  30498. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  30499. }
  30500. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  30501. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  30502. BIO_free(bio);
  30503. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  30504. return EXPECT_RESULT();
  30505. }
  30506. static int test_wolfSSL_ASN1_TYPE(void)
  30507. {
  30508. EXPECT_DECLS;
  30509. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  30510. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  30511. WOLFSSL_ASN1_TYPE* t = NULL;
  30512. WOLFSSL_ASN1_OBJECT* obj = NULL;
  30513. #ifndef NO_ASN_TIME
  30514. WOLFSSL_ASN1_TIME* time = NULL;
  30515. #endif
  30516. WOLFSSL_ASN1_STRING* str = NULL;
  30517. unsigned char data[] = { 0x00 };
  30518. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  30519. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30520. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  30521. wolfSSL_ASN1_TYPE_free(t);
  30522. t = NULL;
  30523. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30524. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  30525. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  30526. wolfSSL_ASN1_TYPE_free(t);
  30527. t = NULL;
  30528. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30529. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  30530. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  30531. wolfSSL_ASN1_TYPE_free(t);
  30532. t = NULL;
  30533. #ifndef NO_ASN_TIME
  30534. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30535. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  30536. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  30537. wolfSSL_ASN1_TYPE_free(t);
  30538. t = NULL;
  30539. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30540. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  30541. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  30542. wolfSSL_ASN1_TYPE_free(t);
  30543. t = NULL;
  30544. #endif
  30545. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30546. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30547. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  30548. wolfSSL_ASN1_TYPE_free(t);
  30549. t = NULL;
  30550. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30551. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30552. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  30553. wolfSSL_ASN1_TYPE_free(t);
  30554. t = NULL;
  30555. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30556. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30557. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  30558. wolfSSL_ASN1_TYPE_free(t);
  30559. t = NULL;
  30560. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30561. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30562. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  30563. wolfSSL_ASN1_TYPE_free(t);
  30564. t = NULL;
  30565. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30566. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30567. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  30568. wolfSSL_ASN1_TYPE_free(t);
  30569. t = NULL;
  30570. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  30571. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  30572. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  30573. wolfSSL_ASN1_TYPE_free(t);
  30574. t = NULL;
  30575. #endif
  30576. return EXPECT_RESULT();
  30577. }
  30578. /* Testing code used in dpp.c in hostap */
  30579. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30580. typedef struct {
  30581. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  30582. * as an OID identifying the curve */
  30583. X509_ALGOR *alg;
  30584. /* Compressed format public key per ANSI X9.63 */
  30585. ASN1_BIT_STRING *pub_key;
  30586. } DPP_BOOTSTRAPPING_KEY;
  30587. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  30588. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  30589. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  30590. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  30591. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  30592. typedef struct {
  30593. ASN1_INTEGER *integer;
  30594. } TEST_ASN1;
  30595. ASN1_SEQUENCE(TEST_ASN1) = {
  30596. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  30597. } ASN1_SEQUENCE_END(TEST_ASN1)
  30598. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  30599. typedef struct {
  30600. ASN1_OCTET_STRING *octet_string;
  30601. } TEST_FAIL_ASN1;
  30602. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  30603. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  30604. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  30605. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  30606. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  30607. #endif
  30608. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  30609. {
  30610. EXPECT_DECLS;
  30611. /* Testing code used in dpp.c in hostap */
  30612. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  30613. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  30614. EC_KEY *eckey = NULL;
  30615. EVP_PKEY *key = NULL;
  30616. size_t len = 0;
  30617. unsigned char *der = NULL;
  30618. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  30619. const unsigned char *in = ecc_clikey_der_256;
  30620. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  30621. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  30622. const EC_GROUP *group = NULL;
  30623. const EC_POINT *point = NULL;
  30624. int nid;
  30625. TEST_ASN1 *test_asn1 = NULL;
  30626. TEST_FAIL_ASN1 test_fail_asn1;
  30627. const unsigned char badObjDer[] = { 0x06, 0x00 };
  30628. const unsigned char goodObjDer[] = {
  30629. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  30630. };
  30631. WOLFSSL_ASN1_ITEM emptyTemplate;
  30632. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  30633. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30634. der = NULL;
  30635. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  30636. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  30637. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30638. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  30639. (long)sizeof_ecc_clikey_der_256));
  30640. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  30641. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  30642. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  30643. nid = EC_GROUP_get_curve_name(group);
  30644. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  30645. group_obj = OBJ_nid2obj(nid);
  30646. if ((ec_obj != NULL) && (group_obj != NULL)) {
  30647. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  30648. group_obj), 1);
  30649. if (EXPECT_SUCCESS()) {
  30650. ec_obj = NULL;
  30651. group_obj = NULL;
  30652. }
  30653. }
  30654. wolfSSL_ASN1_OBJECT_free(group_obj);
  30655. wolfSSL_ASN1_OBJECT_free(ec_obj);
  30656. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  30657. #ifdef HAVE_COMP_KEY
  30658. ExpectIntGT((len = EC_POINT_point2oct(
  30659. group, point, POINT_CONVERSION_COMPRESSED,
  30660. NULL, 0, NULL)), 0);
  30661. #else
  30662. ExpectIntGT((len = EC_POINT_point2oct(
  30663. group, point, POINT_CONVERSION_UNCOMPRESSED,
  30664. NULL, 0, NULL)), 0);
  30665. #endif
  30666. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  30667. #ifdef HAVE_COMP_KEY
  30668. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30669. der, len-1, NULL), 0);
  30670. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  30671. der, len, NULL), len);
  30672. #else
  30673. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30674. der, len-1, NULL), 0);
  30675. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  30676. der, len, NULL), len);
  30677. #endif
  30678. if (EXPECT_SUCCESS()) {
  30679. bootstrap->pub_key->data = der;
  30680. bootstrap->pub_key->length = (int)len;
  30681. /* Not actually used */
  30682. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30683. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30684. }
  30685. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  30686. der = NULL;
  30687. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30688. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30689. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30690. EVP_PKEY_free(key);
  30691. EC_KEY_free(eckey);
  30692. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30693. bootstrap = NULL;
  30694. DPP_BOOTSTRAPPING_KEY_free(NULL);
  30695. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  30696. * a NULL and an empty BIT_STRING. */
  30697. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  30698. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  30699. if (EXPECT_SUCCESS()) {
  30700. bootstrap->alg->algorithm->obj = badObjDer;
  30701. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  30702. }
  30703. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  30704. if (EXPECT_SUCCESS()) {
  30705. bootstrap->alg->parameter->type = V_ASN1_NULL;
  30706. bootstrap->alg->parameter->value.ptr = NULL;
  30707. bootstrap->pub_key->data = NULL;
  30708. bootstrap->pub_key->length = 0;
  30709. /* Not actually used */
  30710. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  30711. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  30712. }
  30713. /* Encode with bad OBJECT_ID. */
  30714. der = NULL;
  30715. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  30716. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  30717. if (EXPECT_SUCCESS()) {
  30718. bootstrap->alg->algorithm->obj = goodObjDer;
  30719. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  30720. bootstrap->alg->algorithm->grp = 2;
  30721. }
  30722. der = NULL;
  30723. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  30724. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  30725. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30726. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  30727. /* Test integer */
  30728. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  30729. der = NULL;
  30730. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  30731. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  30732. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  30733. TEST_ASN1_free(test_asn1);
  30734. /* Test integer cases. */
  30735. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30736. TEST_ASN1_free(NULL);
  30737. /* Test error cases. */
  30738. ExpectNull(TEST_FAIL_ASN1_new());
  30739. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  30740. TEST_FAIL_ASN1_free(NULL);
  30741. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  30742. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  30743. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  30744. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  30745. return EXPECT_RESULT();
  30746. }
  30747. static int test_wolfSSL_lhash(void)
  30748. {
  30749. EXPECT_DECLS;
  30750. #ifdef OPENSSL_ALL
  30751. const char testStr[] = "Like a true nature's child\n"
  30752. "We were born\n"
  30753. "Born to be wild";
  30754. #ifdef NO_SHA
  30755. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  30756. #else
  30757. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  30758. #endif
  30759. #endif
  30760. return EXPECT_RESULT();
  30761. }
  30762. static int test_wolfSSL_X509_NAME(void)
  30763. {
  30764. EXPECT_DECLS;
  30765. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30766. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30767. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  30768. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  30769. defined(OPENSSL_EXTRA))
  30770. X509* x509 = NULL;
  30771. const unsigned char* c = NULL;
  30772. unsigned char buf[4096];
  30773. int bytes = 0;
  30774. XFILE f = XBADFILE;
  30775. const X509_NAME* a = NULL;
  30776. const X509_NAME* b = NULL;
  30777. X509_NAME* d2i_name = NULL;
  30778. int sz = 0;
  30779. unsigned char* tmp = NULL;
  30780. char file[] = "./certs/ca-cert.der";
  30781. #ifndef OPENSSL_EXTRA_X509_SMALL
  30782. byte empty[] = { /* CN=empty emailAddress= */
  30783. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  30784. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  30785. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  30786. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  30787. 0x01, 0x16, 0x00
  30788. };
  30789. #endif
  30790. #ifndef OPENSSL_EXTRA_X509_SMALL
  30791. /* test compile of deprecated function, returns 0 */
  30792. ExpectIntEQ(CRYPTO_thread_id(), 0);
  30793. #endif
  30794. ExpectNotNull(a = X509_NAME_new());
  30795. X509_NAME_free((X509_NAME*)a);
  30796. a = NULL;
  30797. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  30798. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  30799. if (f != XBADFILE)
  30800. XFCLOSE(f);
  30801. c = buf;
  30802. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  30803. /* test cmp function */
  30804. ExpectNotNull(a = X509_get_issuer_name(x509));
  30805. ExpectNotNull(b = X509_get_subject_name(x509));
  30806. #ifndef OPENSSL_EXTRA_X509_SMALL
  30807. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  30808. #endif
  30809. tmp = buf;
  30810. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  30811. if (sz > 0 && tmp == buf) {
  30812. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  30813. __LINE__);
  30814. fprintf(stderr, " Expected pointer to be incremented\n");
  30815. abort();
  30816. }
  30817. #ifndef OPENSSL_EXTRA_X509_SMALL
  30818. tmp = buf;
  30819. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30820. #endif
  30821. /* if output parameter is NULL, should still return required size. */
  30822. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  30823. /* retry but with the function creating a buffer */
  30824. tmp = NULL;
  30825. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30826. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30827. tmp = NULL;
  30828. #ifdef WOLFSSL_CERT_NAME_ALL
  30829. /* test for givenName and name */
  30830. {
  30831. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  30832. const byte gName[] = "test-given-name";
  30833. const byte name[] = "test-name";
  30834. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30835. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  30836. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30837. 1);
  30838. wolfSSL_X509_NAME_ENTRY_free(entry);
  30839. entry = NULL;
  30840. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  30841. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  30842. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  30843. 1);
  30844. wolfSSL_X509_NAME_ENTRY_free(entry);
  30845. tmp = NULL;
  30846. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  30847. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  30848. }
  30849. #endif
  30850. b = NULL;
  30851. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  30852. #ifndef OPENSSL_EXTRA_X509_SMALL
  30853. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  30854. #endif
  30855. X509_NAME_free((X509_NAME*)b);
  30856. X509_NAME_free(d2i_name);
  30857. d2i_name = NULL;
  30858. X509_free(x509);
  30859. #ifndef OPENSSL_EXTRA_X509_SMALL
  30860. /* test with an empty domain component */
  30861. tmp = empty;
  30862. sz = sizeof(empty);
  30863. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  30864. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  30865. /* size of empty emailAddress will be 0 */
  30866. tmp = buf;
  30867. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  30868. (char*)tmp, sizeof(buf)), 0);
  30869. /* should contain no organization name */
  30870. tmp = buf;
  30871. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  30872. (char*)tmp, sizeof(buf)), -1);
  30873. X509_NAME_free(d2i_name);
  30874. #endif
  30875. #endif
  30876. return EXPECT_RESULT();
  30877. }
  30878. static int test_wolfSSL_X509_NAME_hash(void)
  30879. {
  30880. EXPECT_DECLS;
  30881. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  30882. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  30883. BIO* bio = NULL;
  30884. X509* x509 = NULL;
  30885. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30886. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30887. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30888. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  30889. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  30890. X509_free(x509);
  30891. BIO_free(bio);
  30892. #endif
  30893. return EXPECT_RESULT();
  30894. }
  30895. static int test_wolfSSL_X509_NAME_print_ex(void)
  30896. {
  30897. EXPECT_DECLS;
  30898. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  30899. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  30900. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  30901. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  30902. !defined(NO_BIO) && !defined(NO_RSA)
  30903. int memSz = 0;
  30904. byte* mem = NULL;
  30905. BIO* bio = NULL;
  30906. BIO* membio = NULL;
  30907. X509* x509 = NULL;
  30908. X509_NAME* name = NULL;
  30909. const char* expNormal = "C=US, CN=wolfssl.com";
  30910. const char* expEqSpace = "C = US, CN = wolfssl.com";
  30911. const char* expReverse = "CN=wolfssl.com, C=US";
  30912. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  30913. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  30914. const char* expRFC5523 =
  30915. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  30916. /* Test with real cert (svrCertFile) first */
  30917. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  30918. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  30919. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  30920. ExpectNotNull(name = X509_get_subject_name(x509));
  30921. /* Test without flags */
  30922. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30923. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30924. BIO_free(membio);
  30925. membio = NULL;
  30926. /* Test flag: XN_FLAG_RFC2253 */
  30927. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30928. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30929. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30930. BIO_free(membio);
  30931. membio = NULL;
  30932. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  30933. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30934. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30935. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30936. BIO_free(membio);
  30937. membio = NULL;
  30938. X509_free(x509);
  30939. BIO_free(bio);
  30940. name = NULL;
  30941. /* Test normal case without escaped characters */
  30942. {
  30943. /* Create name: "/C=US/CN=wolfssl.com" */
  30944. ExpectNotNull(name = X509_NAME_new());
  30945. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  30946. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  30947. WOLFSSL_SUCCESS);
  30948. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  30949. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  30950. WOLFSSL_SUCCESS);
  30951. /* Test without flags */
  30952. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30953. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  30954. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30955. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  30956. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  30957. BIO_free(membio);
  30958. membio = NULL;
  30959. /* Test with XN_FLAG_ONELINE which should enable XN_FLAG_SPC_EQ for
  30960. spaces aroun '=' */
  30961. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30962. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, XN_FLAG_ONELINE),
  30963. WOLFSSL_SUCCESS);
  30964. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30965. ExpectIntEQ(memSz, XSTRLEN(expEqSpace));
  30966. ExpectIntEQ(XSTRNCMP((char*)mem, expEqSpace, XSTRLEN(expEqSpace)), 0);
  30967. BIO_free(membio);
  30968. membio = NULL;
  30969. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  30970. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30971. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30972. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  30973. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30974. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  30975. BIO_free(membio);
  30976. membio = NULL;
  30977. /* Test flags: XN_FLAG_DN_REV - reversed */
  30978. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  30979. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  30980. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  30981. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  30982. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  30983. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  30984. BIO_free(membio);
  30985. membio = NULL;
  30986. X509_NAME_free(name);
  30987. name = NULL;
  30988. }
  30989. /* Test RFC2253 characters are escaped with backslashes */
  30990. {
  30991. ExpectNotNull(name = X509_NAME_new());
  30992. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  30993. /* space at beginning and end, and: ,+"\ */
  30994. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  30995. WOLFSSL_SUCCESS);
  30996. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  30997. /* # at beginning, and: <>;*/
  30998. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  30999. WOLFSSL_SUCCESS);
  31000. /* Test without flags */
  31001. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31002. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  31003. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31004. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  31005. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  31006. XSTRLEN(expNotEscaped)), 0);
  31007. BIO_free(membio);
  31008. membio = NULL;
  31009. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  31010. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31011. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31012. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  31013. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31014. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  31015. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  31016. BIO_free(membio);
  31017. membio = NULL;
  31018. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  31019. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  31020. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  31021. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  31022. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  31023. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  31024. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  31025. XSTRLEN(expNotEscapedRev)), 0);
  31026. BIO_free(membio);
  31027. X509_NAME_free(name);
  31028. }
  31029. #endif
  31030. return EXPECT_RESULT();
  31031. }
  31032. #ifndef NO_BIO
  31033. static int test_wolfSSL_X509_INFO_multiple_info(void)
  31034. {
  31035. EXPECT_DECLS;
  31036. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  31037. STACK_OF(X509_INFO) *info_stack = NULL;
  31038. X509_INFO *info = NULL;
  31039. int len;
  31040. int i;
  31041. const char* files[] = {
  31042. cliCertFile,
  31043. cliKeyFile,
  31044. /* This needs to be the order as svrCertFile contains the
  31045. * intermediate cert as well. */
  31046. svrKeyFile,
  31047. svrCertFile,
  31048. NULL,
  31049. };
  31050. const char** curFile;
  31051. BIO *fileBIO = NULL;
  31052. BIO *concatBIO = NULL;
  31053. byte tmp[FOURK_BUF];
  31054. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  31055. * to group objects together. */
  31056. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  31057. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  31058. int fileLen = 0;
  31059. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  31060. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  31061. if (EXPECT_SUCCESS()) {
  31062. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  31063. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  31064. fileLen -= len;
  31065. if (EXPECT_FAIL())
  31066. break;
  31067. }
  31068. /* Make sure we read the entire file */
  31069. ExpectIntEQ(fileLen, 0);
  31070. }
  31071. BIO_free(fileBIO);
  31072. fileBIO = NULL;
  31073. }
  31074. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  31075. NULL));
  31076. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  31077. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  31078. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  31079. ExpectNotNull(info->x509);
  31080. ExpectNull(info->crl);
  31081. if (i != 0) {
  31082. ExpectNotNull(info->x_pkey);
  31083. ExpectIntEQ(X509_check_private_key(info->x509,
  31084. info->x_pkey->dec_pkey), 1);
  31085. }
  31086. else {
  31087. ExpectNull(info->x_pkey);
  31088. }
  31089. }
  31090. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31091. BIO_free(concatBIO);
  31092. #endif
  31093. return EXPECT_RESULT();
  31094. }
  31095. #endif
  31096. #ifndef NO_BIO
  31097. static int test_wolfSSL_X509_INFO(void)
  31098. {
  31099. EXPECT_DECLS;
  31100. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  31101. STACK_OF(X509_INFO) *info_stack = NULL;
  31102. X509_INFO *info = NULL;
  31103. BIO *cert = NULL;
  31104. int i;
  31105. /* PEM in hex format to avoid null terminator */
  31106. byte data[] = {
  31107. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  31108. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  31109. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  31110. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  31111. 0x2d, 0x2d, 0x2d
  31112. };
  31113. /* PEM in hex format to avoid null terminator */
  31114. byte data2[] = {
  31115. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  31116. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  31117. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  31118. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  31119. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  31120. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  31121. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  31122. };
  31123. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  31124. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31125. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  31126. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  31127. ExpectNotNull(info->x509);
  31128. ExpectNull(info->crl);
  31129. ExpectNull(info->x_pkey);
  31130. }
  31131. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31132. info_stack = NULL;
  31133. BIO_free(cert);
  31134. cert = NULL;
  31135. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  31136. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31137. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31138. info_stack = NULL;
  31139. BIO_free(cert);
  31140. cert = NULL;
  31141. /* This case should fail due to invalid input. */
  31142. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  31143. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  31144. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31145. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31146. info_stack = NULL;
  31147. BIO_free(cert);
  31148. cert = NULL;
  31149. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  31150. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  31151. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  31152. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  31153. BIO_free(cert);
  31154. #endif
  31155. return EXPECT_RESULT();
  31156. }
  31157. #endif
  31158. static int test_wolfSSL_X509_subject_name_hash(void)
  31159. {
  31160. EXPECT_DECLS;
  31161. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31162. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  31163. X509* x509 = NULL;
  31164. X509_NAME* subjectName = NULL;
  31165. unsigned long ret1 = 0;
  31166. unsigned long ret2 = 0;
  31167. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31168. SSL_FILETYPE_PEM));
  31169. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  31170. /* These two
  31171. * - X509_subject_name_hash(x509)
  31172. * - X509_NAME_hash(X509_get_subject_name(x509))
  31173. * should give the same hash, if !defined(NO_SHA) is true. */
  31174. ret1 = X509_subject_name_hash(x509);
  31175. ExpectIntNE(ret1, 0);
  31176. #if !defined(NO_SHA)
  31177. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  31178. ExpectIntNE(ret2, 0);
  31179. ExpectIntEQ(ret1, ret2);
  31180. #else
  31181. (void) ret2;
  31182. #endif
  31183. X509_free(x509);
  31184. #endif
  31185. return EXPECT_RESULT();
  31186. }
  31187. static int test_wolfSSL_X509_issuer_name_hash(void)
  31188. {
  31189. EXPECT_DECLS;
  31190. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  31191. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  31192. X509* x509 = NULL;
  31193. X509_NAME* issuertName = NULL;
  31194. unsigned long ret1 = 0;
  31195. unsigned long ret2 = 0;
  31196. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31197. SSL_FILETYPE_PEM));
  31198. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  31199. /* These two
  31200. * - X509_issuer_name_hash(x509)
  31201. * - X509_NAME_hash(X509_get_issuer_name(x509))
  31202. * should give the same hash, if !defined(NO_SHA) is true. */
  31203. ret1 = X509_issuer_name_hash(x509);
  31204. ExpectIntNE(ret1, 0);
  31205. #if !defined(NO_SHA)
  31206. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  31207. ExpectIntNE(ret2, 0);
  31208. ExpectIntEQ(ret1, ret2);
  31209. #else
  31210. (void) ret2;
  31211. #endif
  31212. X509_free(x509);
  31213. #endif
  31214. return EXPECT_RESULT();
  31215. }
  31216. static int test_wolfSSL_X509_check_host(void)
  31217. {
  31218. EXPECT_DECLS;
  31219. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  31220. && !defined(NO_SHA) && !defined(NO_RSA)
  31221. X509* x509 = NULL;
  31222. const char altName[] = "example.com";
  31223. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31224. SSL_FILETYPE_PEM));
  31225. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  31226. WOLFSSL_SUCCESS);
  31227. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  31228. WOLFSSL_FAILURE);
  31229. X509_free(x509);
  31230. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  31231. WOLFSSL_FAILURE);
  31232. #endif
  31233. return EXPECT_RESULT();
  31234. }
  31235. static int test_wolfSSL_X509_check_email(void)
  31236. {
  31237. EXPECT_DECLS;
  31238. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  31239. X509* x509 = NULL;
  31240. const char goodEmail[] = "info@wolfssl.com";
  31241. const char badEmail[] = "disinfo@wolfssl.com";
  31242. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31243. SSL_FILETYPE_PEM));
  31244. /* Should fail on non-matching email address */
  31245. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  31246. WOLFSSL_FAILURE);
  31247. /* Should succeed on matching email address */
  31248. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  31249. WOLFSSL_SUCCESS);
  31250. /* Should compute length internally when not provided */
  31251. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  31252. WOLFSSL_SUCCESS);
  31253. /* Should fail when email address is NULL */
  31254. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  31255. WOLFSSL_FAILURE);
  31256. X509_free(x509);
  31257. /* Should fail when x509 is NULL */
  31258. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  31259. WOLFSSL_FAILURE);
  31260. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  31261. return EXPECT_RESULT();
  31262. }
  31263. static int test_wc_PemToDer(void)
  31264. {
  31265. EXPECT_DECLS;
  31266. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  31267. int ret;
  31268. DerBuffer* pDer = NULL;
  31269. const char* ca_cert = "./certs/server-cert.pem";
  31270. byte* cert_buf = NULL;
  31271. size_t cert_sz = 0;
  31272. int eccKey = 0;
  31273. EncryptedInfo info;
  31274. XMEMSET(&info, 0, sizeof(info));
  31275. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31276. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  31277. &info, &eccKey), 0);
  31278. wc_FreeDer(&pDer);
  31279. pDer = NULL;
  31280. if (cert_buf != NULL) {
  31281. free(cert_buf);
  31282. cert_buf = NULL;
  31283. }
  31284. #ifdef HAVE_ECC
  31285. {
  31286. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  31287. byte key_buf[256] = {0};
  31288. /* Test fail of loading a key with cert type */
  31289. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  31290. key_buf[0] = '\n';
  31291. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  31292. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  31293. &pDer, NULL, &info, &eccKey)), 0);
  31294. #ifdef OPENSSL_EXTRA
  31295. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  31296. &pDer, NULL, &info, &eccKey)), 0);
  31297. #endif
  31298. wc_FreeDer(&pDer);
  31299. if (cert_buf != NULL)
  31300. free(cert_buf);
  31301. }
  31302. #endif
  31303. #endif
  31304. return EXPECT_RESULT();
  31305. }
  31306. static int test_wc_AllocDer(void)
  31307. {
  31308. EXPECT_DECLS;
  31309. #if !defined(NO_CERTS)
  31310. DerBuffer* pDer = NULL;
  31311. word32 testSize = 1024;
  31312. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  31313. BAD_FUNC_ARG);
  31314. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  31315. ExpectNotNull(pDer);
  31316. wc_FreeDer(&pDer);
  31317. #endif
  31318. return EXPECT_RESULT();
  31319. }
  31320. static int test_wc_CertPemToDer(void)
  31321. {
  31322. EXPECT_DECLS;
  31323. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  31324. const char* ca_cert = "./certs/ca-cert.pem";
  31325. byte* cert_buf = NULL;
  31326. size_t cert_sz = 0;
  31327. size_t cert_dersz = 0;
  31328. byte* cert_der = NULL;
  31329. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31330. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31331. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31332. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  31333. (int)cert_dersz, CERT_TYPE), 0);
  31334. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  31335. BAD_FUNC_ARG);
  31336. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  31337. BAD_FUNC_ARG);
  31338. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  31339. BAD_FUNC_ARG);
  31340. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  31341. CERT_TYPE), BAD_FUNC_ARG);
  31342. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  31343. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  31344. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  31345. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  31346. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  31347. CERT_TYPE), BAD_FUNC_ARG);
  31348. if (cert_der != NULL)
  31349. free(cert_der);
  31350. if (cert_buf != NULL)
  31351. free(cert_buf);
  31352. #endif
  31353. return EXPECT_RESULT();
  31354. }
  31355. static int test_wc_KeyPemToDer(void)
  31356. {
  31357. EXPECT_DECLS;
  31358. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31359. int ret = 0;
  31360. const byte cert_buf[] = \
  31361. "-----BEGIN PRIVATE KEY-----\n"
  31362. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  31363. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  31364. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  31365. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  31366. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  31367. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  31368. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  31369. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  31370. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  31371. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  31372. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  31373. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  31374. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  31375. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  31376. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  31377. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  31378. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  31379. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  31380. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  31381. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  31382. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  31383. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  31384. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  31385. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  31386. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  31387. "AQue5o6oVfhgLiJzMdo/77gw\n"
  31388. "-----END PRIVATE KEY-----\n";
  31389. const int cert_sz = sizeof(cert_buf);
  31390. const char cert_pw[] = "password";
  31391. int cert_dersz = 0;
  31392. byte* cert_der = NULL;
  31393. /* Bad arg: Cert buffer is NULL */
  31394. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  31395. BAD_FUNC_ARG);
  31396. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  31397. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  31398. BAD_FUNC_ARG);
  31399. /* Test normal operation */
  31400. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31401. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  31402. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  31403. cert_pw), 0);
  31404. ExpectIntLE(ret, cert_sz);
  31405. if (cert_der != NULL) {
  31406. free(cert_der);
  31407. cert_der = NULL;
  31408. }
  31409. /* Test NULL for DER buffer to return needed DER buffer size */
  31410. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  31411. ExpectIntLE(ret, cert_sz);
  31412. if (EXPECT_SUCCESS())
  31413. cert_dersz = ret;
  31414. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  31415. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  31416. cert_pw), 0);
  31417. ExpectIntLE(ret, cert_sz);
  31418. if (cert_der != NULL)
  31419. free(cert_der);
  31420. #endif
  31421. return EXPECT_RESULT();
  31422. }
  31423. static int test_wc_PubKeyPemToDer(void)
  31424. {
  31425. EXPECT_DECLS;
  31426. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  31427. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  31428. int ret = 0;
  31429. const char* key = "./certs/ecc-client-keyPub.pem";
  31430. byte* cert_buf = NULL;
  31431. size_t cert_sz = 0, cert_dersz = 0;
  31432. byte* cert_der = NULL;
  31433. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  31434. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  31435. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  31436. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  31437. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31438. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  31439. (int)cert_dersz), 0);
  31440. if (cert_der != NULL) {
  31441. free(cert_der);
  31442. cert_der = NULL;
  31443. }
  31444. /* Test NULL for DER buffer to return needed DER buffer size */
  31445. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  31446. ExpectIntLE(ret, cert_sz);
  31447. cert_dersz = (size_t)ret;
  31448. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31449. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  31450. (int)cert_dersz), 0);
  31451. if (cert_der != NULL) {
  31452. free(cert_der);
  31453. }
  31454. if (cert_buf != NULL) {
  31455. free(cert_buf);
  31456. }
  31457. #endif
  31458. return EXPECT_RESULT();
  31459. }
  31460. static int test_wc_PemPubKeyToDer(void)
  31461. {
  31462. EXPECT_DECLS;
  31463. #if !defined(NO_FILESYSTEM) && \
  31464. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  31465. const char* key = "./certs/ecc-client-keyPub.pem";
  31466. size_t cert_dersz = 1024;
  31467. byte* cert_der = NULL;
  31468. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  31469. BAD_FUNC_ARG);
  31470. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31471. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  31472. if (cert_der != NULL) {
  31473. free(cert_der);
  31474. }
  31475. #endif
  31476. return EXPECT_RESULT();
  31477. }
  31478. static int test_wc_GetPubKeyDerFromCert(void)
  31479. {
  31480. EXPECT_DECLS;
  31481. #if !defined(NO_RSA) || defined(HAVE_ECC)
  31482. int ret;
  31483. word32 idx = 0;
  31484. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  31485. word32 keyDerSz = (word32)sizeof(keyDer);
  31486. DecodedCert decoded;
  31487. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  31488. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  31489. word32 certBufSz = sizeof(certBuf);
  31490. #endif
  31491. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  31492. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  31493. XFILE fp = XBADFILE;
  31494. #endif
  31495. #ifndef NO_RSA
  31496. RsaKey rsaKey;
  31497. #if defined(USE_CERT_BUFFERS_2048)
  31498. byte* rsaCertDer = (byte*)client_cert_der_2048;
  31499. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  31500. #elif defined(USE_CERT_BUFFERS_1024)
  31501. byte* rsaCertDer = (byte*)client_cert_der_1024;
  31502. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  31503. #else
  31504. unsigned char rsaCertDer[TWOK_BUF];
  31505. word32 rsaCertDerSz;
  31506. #endif
  31507. #endif
  31508. #ifdef HAVE_ECC
  31509. ecc_key eccKey;
  31510. #if defined(USE_CERT_BUFFERS_256)
  31511. byte* eccCert = (byte*)cliecc_cert_der_256;
  31512. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  31513. #else
  31514. unsigned char eccCert[ONEK_BUF];
  31515. word32 eccCertSz;
  31516. XFILE fp2 = XBADFILE;
  31517. #endif
  31518. #endif
  31519. #ifndef NO_RSA
  31520. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  31521. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  31522. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  31523. fp), 0);
  31524. if (fp != XBADFILE) {
  31525. XFCLOSE(fp);
  31526. fp = XBADFILE;
  31527. }
  31528. #endif
  31529. /* good test case - RSA DER cert */
  31530. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  31531. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31532. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31533. ExpectIntGT(keyDerSz, 0);
  31534. /* sanity check, verify we can import DER public key */
  31535. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31536. ExpectIntEQ(ret, 0);
  31537. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31538. if (ret == 0) {
  31539. wc_FreeRsaKey(&rsaKey);
  31540. }
  31541. /* test LENGTH_ONLY_E case */
  31542. keyDerSz = 0;
  31543. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31544. LENGTH_ONLY_E);
  31545. ExpectIntGT(keyDerSz, 0);
  31546. /* bad args: DecodedCert NULL */
  31547. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  31548. /* bad args: output key buff size */
  31549. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  31550. /* bad args: zero size output key buffer */
  31551. keyDerSz = 0;
  31552. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31553. BAD_FUNC_ARG);
  31554. wc_FreeDecodedCert(&decoded);
  31555. /* Certificate Request Tests */
  31556. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  31557. {
  31558. XMEMSET(certBuf, 0, sizeof(certBuf));
  31559. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  31560. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  31561. if (fp != XBADFILE) {
  31562. XFCLOSE(fp);
  31563. }
  31564. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  31565. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  31566. /* good test case - RSA DER certificate request */
  31567. keyDerSz = sizeof(keyDer);
  31568. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  31569. 0);
  31570. ExpectIntGT(keyDerSz, 0);
  31571. /* sanity check, verify we can import DER public key */
  31572. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  31573. ExpectIntEQ(ret, 0);
  31574. idx = 0;
  31575. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  31576. if (ret == 0) {
  31577. wc_FreeRsaKey(&rsaKey);
  31578. }
  31579. wc_FreeDecodedCert(&decoded);
  31580. }
  31581. #endif /* WOLFSSL_CERT_REQ */
  31582. #endif /* NO_RSA */
  31583. #ifdef HAVE_ECC
  31584. #ifndef USE_CERT_BUFFERS_256
  31585. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  31586. XBADFILE);
  31587. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  31588. if (fp2 != XBADFILE) {
  31589. XFCLOSE(fp2);
  31590. }
  31591. #endif
  31592. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  31593. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31594. /* good test case - ECC */
  31595. XMEMSET(keyDer, 0, sizeof(keyDer));
  31596. keyDerSz = sizeof(keyDer);
  31597. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31598. ExpectIntGT(keyDerSz, 0);
  31599. /* sanity check, verify we can import DER public key */
  31600. ret = wc_ecc_init(&eccKey);
  31601. ExpectIntEQ(ret, 0);
  31602. idx = 0; /* reset idx to 0, used above in RSA case */
  31603. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  31604. if (ret == 0) {
  31605. wc_ecc_free(&eccKey);
  31606. }
  31607. /* test LENGTH_ONLY_E case */
  31608. keyDerSz = 0;
  31609. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  31610. LENGTH_ONLY_E);
  31611. ExpectIntGT(keyDerSz, 0);
  31612. wc_FreeDecodedCert(&decoded);
  31613. #endif
  31614. #endif /* !NO_RSA || HAVE_ECC */
  31615. return EXPECT_RESULT();
  31616. }
  31617. static int test_wc_CheckCertSigPubKey(void)
  31618. {
  31619. EXPECT_DECLS;
  31620. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31621. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  31622. int ret = 0;
  31623. const char* ca_cert = "./certs/ca-cert.pem";
  31624. byte* cert_buf = NULL;
  31625. size_t cert_sz = 0;
  31626. byte* cert_der = NULL;
  31627. word32 cert_dersz = 0;
  31628. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  31629. word32 keyDerSz = (word32)sizeof(keyDer);
  31630. DecodedCert decoded;
  31631. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  31632. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  31633. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  31634. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  31635. (int)cert_dersz, CERT_TYPE), 0);
  31636. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  31637. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  31638. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  31639. ExpectIntGT(keyDerSz, 0);
  31640. /* Good test case. */
  31641. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31642. keyDerSz, RSAk), 0);
  31643. /* No certificate. */
  31644. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  31645. ECDSAk), BAD_FUNC_ARG);
  31646. /* Bad cert size. */
  31647. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  31648. RSAk), 0);
  31649. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  31650. /* No public key. */
  31651. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  31652. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  31653. /* Bad public key size. */
  31654. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  31655. RSAk), BAD_FUNC_ARG);
  31656. /* Wrong aglo. */
  31657. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  31658. keyDerSz, ECDSAk), ASN_PARSE_E);
  31659. wc_FreeDecodedCert(&decoded);
  31660. if (cert_der != NULL)
  31661. free(cert_der);
  31662. if (cert_buf != NULL)
  31663. free(cert_buf);
  31664. #endif
  31665. return EXPECT_RESULT();
  31666. }
  31667. static int test_wolfSSL_certs(void)
  31668. {
  31669. EXPECT_DECLS;
  31670. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31671. !defined(NO_RSA)
  31672. X509* x509ext = NULL;
  31673. #ifdef OPENSSL_ALL
  31674. X509* x509 = NULL;
  31675. WOLFSSL_X509_EXTENSION* ext = NULL;
  31676. ASN1_OBJECT* obj = NULL;
  31677. #endif
  31678. WOLFSSL* ssl = NULL;
  31679. WOLFSSL_CTX* ctx = NULL;
  31680. STACK_OF(ASN1_OBJECT)* sk = NULL;
  31681. ASN1_STRING* asn1_str = NULL;
  31682. AUTHORITY_KEYID* akey = NULL;
  31683. BASIC_CONSTRAINTS* bc = NULL;
  31684. int crit = 0;
  31685. #ifndef NO_WOLFSSL_SERVER
  31686. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31687. #else
  31688. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31689. #endif
  31690. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31691. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31692. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  31693. #if !defined(NO_CHECK_PRIVATE_KEY)
  31694. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  31695. #endif
  31696. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31697. #if !defined(NO_CHECK_PRIVATE_KEY)
  31698. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  31699. #endif
  31700. ExpectNotNull(ssl = SSL_new(ctx));
  31701. #if !defined(NO_CHECK_PRIVATE_KEY)
  31702. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31703. #endif
  31704. #ifdef HAVE_PK_CALLBACKS
  31705. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  31706. #endif /* HAVE_PK_CALLBACKS */
  31707. /* create and use x509 */
  31708. #ifdef OPENSSL_ALL
  31709. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  31710. WOLFSSL_FILETYPE_PEM));
  31711. #endif
  31712. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  31713. WOLFSSL_FILETYPE_PEM));
  31714. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  31715. #if !defined(NO_CHECK_PRIVATE_KEY)
  31716. /* with loading in a new cert the check on private key should now fail */
  31717. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31718. #endif
  31719. #if defined(USE_CERT_BUFFERS_2048)
  31720. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  31721. (unsigned char*)server_cert_der_2048,
  31722. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  31723. #endif
  31724. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31725. /************* Get Digest of Certificate ******************/
  31726. {
  31727. byte digest[64]; /* max digest size */
  31728. word32 digestSz;
  31729. XMEMSET(digest, 0, sizeof(digest));
  31730. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  31731. WOLFSSL_SUCCESS);
  31732. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  31733. WOLFSSL_SUCCESS);
  31734. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  31735. WOLFSSL_FAILURE);
  31736. }
  31737. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  31738. /* test and checkout X509 extensions */
  31739. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  31740. NID_basic_constraints, &crit, NULL));
  31741. ExpectIntEQ(crit, 0);
  31742. #ifdef OPENSSL_ALL
  31743. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  31744. X509_EXTENSION_free(ext);
  31745. ext = NULL;
  31746. ExpectNotNull(ext = X509_EXTENSION_new());
  31747. X509_EXTENSION_set_critical(ext, 1);
  31748. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  31749. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  31750. ASN1_OBJECT_free(obj);
  31751. obj = NULL;
  31752. X509_EXTENSION_free(ext);
  31753. ext = NULL;
  31754. ExpectNotNull(ext = X509_EXTENSION_new());
  31755. X509_EXTENSION_set_critical(ext, 0);
  31756. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  31757. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  31758. NULL);
  31759. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  31760. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  31761. * and X509_get_ext_d2i has created new */
  31762. asn1_str = NULL;
  31763. X509_EXTENSION_free(ext);
  31764. ext = NULL;
  31765. #endif
  31766. BASIC_CONSTRAINTS_free(bc);
  31767. bc = NULL;
  31768. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  31769. NID_key_usage, &crit, NULL));
  31770. ExpectIntEQ(crit, 1);
  31771. ExpectIntEQ(asn1_str->type, NID_key_usage);
  31772. #ifdef OPENSSL_ALL
  31773. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  31774. X509_EXTENSION_free(ext);
  31775. ext = NULL;
  31776. #endif
  31777. ASN1_STRING_free(asn1_str);
  31778. asn1_str = NULL;
  31779. #ifdef OPENSSL_ALL
  31780. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  31781. NID_ext_key_usage, &crit, NULL));
  31782. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  31783. X509_EXTENSION_free(ext);
  31784. ext = NULL;
  31785. EXTENDED_KEY_USAGE_free(sk);
  31786. sk = NULL;
  31787. #else
  31788. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  31789. &crit, NULL);
  31790. ExpectNull(sk);
  31791. #endif
  31792. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  31793. NID_authority_key_identifier, &crit, NULL));
  31794. #ifdef OPENSSL_ALL
  31795. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  31796. akey));
  31797. X509_EXTENSION_free(ext);
  31798. ext = NULL;
  31799. #endif
  31800. wolfSSL_AUTHORITY_KEYID_free(akey);
  31801. akey = NULL;
  31802. /* NID not yet supported */
  31803. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31804. NID_private_key_usage_period, &crit, NULL));
  31805. ExpectIntEQ(crit, -1);
  31806. sk_ASN1_OBJECT_free(sk);
  31807. sk = NULL;
  31808. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  31809. NID_subject_alt_name, &crit, NULL));
  31810. {
  31811. int i;
  31812. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  31813. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  31814. ExpectIntEQ(gen->type, GEN_DNS);
  31815. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  31816. }
  31817. }
  31818. sk_GENERAL_NAME_free(sk);
  31819. sk = NULL;
  31820. /* NID not yet supported */
  31821. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31822. NID_issuer_alt_name, &crit, NULL));
  31823. ExpectIntEQ(crit, -1);
  31824. sk_ASN1_OBJECT_free(sk);
  31825. sk = NULL;
  31826. /* NID not yet supported */
  31827. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31828. NID_info_access, &crit, NULL));
  31829. sk_ASN1_OBJECT_free(sk);
  31830. sk = NULL;
  31831. /* NID not yet supported */
  31832. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31833. NID_sinfo_access, &crit, NULL));
  31834. ExpectIntEQ(crit, -1);
  31835. sk_ASN1_OBJECT_free(sk);
  31836. sk = NULL;
  31837. /* NID not yet supported */
  31838. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31839. NID_name_constraints, &crit, NULL));
  31840. ExpectIntEQ(crit, -1);
  31841. sk_ASN1_OBJECT_free(sk);
  31842. sk = NULL;
  31843. /* no cert policy set */
  31844. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31845. NID_certificate_policies, &crit, NULL));
  31846. sk_ASN1_OBJECT_free(sk);
  31847. sk = NULL;
  31848. /* NID not yet supported */
  31849. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31850. NID_policy_mappings, &crit, NULL));
  31851. ExpectIntEQ(crit, -1);
  31852. sk_ASN1_OBJECT_free(sk);
  31853. sk = NULL;
  31854. /* NID not yet supported */
  31855. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31856. NID_policy_constraints, &crit, NULL));
  31857. ExpectIntEQ(crit, -1);
  31858. sk_ASN1_OBJECT_free(sk);
  31859. sk = NULL;
  31860. /* NID not yet supported */
  31861. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31862. NID_inhibit_any_policy, &crit, NULL));
  31863. ExpectIntEQ(crit, -1);
  31864. sk_ASN1_OBJECT_free(sk);
  31865. sk = NULL;
  31866. /* NID not yet supported */
  31867. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  31868. NID_tlsfeature, &crit, NULL));
  31869. ExpectIntEQ(crit, -1);
  31870. sk_ASN1_OBJECT_free(sk);
  31871. sk = NULL;
  31872. /* test invalid cases */
  31873. crit = 0;
  31874. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  31875. NULL));
  31876. ExpectIntEQ(crit, -1);
  31877. /* NULL passed for criticality. */
  31878. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  31879. NID_tlsfeature, NULL, NULL));
  31880. ExpectIntEQ(SSL_get_hit(ssl), 0);
  31881. #ifdef OPENSSL_ALL
  31882. X509_free(x509);
  31883. #endif
  31884. X509_free(x509ext);
  31885. SSL_free(ssl);
  31886. SSL_CTX_free(ctx);
  31887. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  31888. return EXPECT_RESULT();
  31889. }
  31890. static int test_wolfSSL_X509_check_private_key(void)
  31891. {
  31892. EXPECT_DECLS;
  31893. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  31894. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  31895. X509* x509 = NULL;
  31896. EVP_PKEY* pkey = NULL;
  31897. const byte* key;
  31898. /* Check with correct key */
  31899. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  31900. SSL_FILETYPE_PEM)));
  31901. key = client_key_der_2048;
  31902. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31903. (long)sizeof_client_key_der_2048));
  31904. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  31905. EVP_PKEY_free(pkey);
  31906. pkey = NULL;
  31907. /* Check with wrong key */
  31908. key = server_key_der_2048;
  31909. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  31910. (long)sizeof_server_key_der_2048));
  31911. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  31912. /* test for incorrect parameter */
  31913. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  31914. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  31915. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  31916. EVP_PKEY_free(pkey);
  31917. X509_free(x509);
  31918. #endif
  31919. return EXPECT_RESULT();
  31920. }
  31921. static int test_wolfSSL_private_keys(void)
  31922. {
  31923. EXPECT_DECLS;
  31924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31925. !defined(NO_FILESYSTEM)
  31926. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31927. WOLFSSL* ssl = NULL;
  31928. WOLFSSL_CTX* ctx = NULL;
  31929. EVP_PKEY* pkey = NULL;
  31930. OpenSSL_add_all_digests();
  31931. OpenSSL_add_all_algorithms();
  31932. #ifndef NO_RSA
  31933. #ifndef NO_WOLFSSL_SERVER
  31934. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31935. #else
  31936. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31937. #endif
  31938. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  31939. /* Have to load a cert before you can check the private key against that
  31940. * certificates public key! */
  31941. #if !defined(NO_CHECK_PRIVATE_KEY)
  31942. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  31943. #endif
  31944. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  31945. #if !defined(NO_CHECK_PRIVATE_KEY)
  31946. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31947. #endif
  31948. ExpectNotNull(ssl = SSL_new(ctx));
  31949. #if !defined(NO_CHECK_PRIVATE_KEY)
  31950. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31951. #endif
  31952. #ifdef USE_CERT_BUFFERS_2048
  31953. {
  31954. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31955. unsigned char buf[FOURK_BUF];
  31956. word32 bufSz;
  31957. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  31958. (unsigned char*)client_key_der_2048,
  31959. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  31960. #if !defined(NO_CHECK_PRIVATE_KEY)
  31961. /* Should mismatch now that a different private key loaded */
  31962. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31963. #endif
  31964. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  31965. (unsigned char*)server_key,
  31966. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  31967. #if !defined(NO_CHECK_PRIVATE_KEY)
  31968. /* After loading back in DER format of original key, should match */
  31969. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  31970. #endif
  31971. /* test loading private key to the WOLFSSL_CTX */
  31972. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  31973. (unsigned char*)client_key_der_2048,
  31974. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  31975. #if !defined(NO_CHECK_PRIVATE_KEY)
  31976. /* Should mismatch now that a different private key loaded */
  31977. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31978. #endif
  31979. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  31980. (unsigned char*)server_key,
  31981. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  31982. #if !defined(NO_CHECK_PRIVATE_KEY)
  31983. /* After loading back in DER format of original key, should match */
  31984. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  31985. #endif
  31986. /* pkey not set yet, expecting to fail */
  31987. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  31988. /* set PKEY and test again */
  31989. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31990. &server_key, (long)sizeof_server_key_der_2048));
  31991. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  31992. /* reuse PKEY structure and test
  31993. * this should be checked with a memory management sanity checker */
  31994. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  31995. server_key = (const unsigned char*)server_key_der_2048;
  31996. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  31997. &server_key, (long)sizeof_server_key_der_2048));
  31998. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  31999. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  32000. bufSz = FOURK_BUF;
  32001. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  32002. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  32003. RSAk, NULL, 0)), 0);
  32004. server_key = (const unsigned char*)buf;
  32005. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  32006. (long)bufSz));
  32007. }
  32008. #endif
  32009. EVP_PKEY_free(pkey);
  32010. pkey = NULL;
  32011. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  32012. ssl = NULL;
  32013. SSL_CTX_free(ctx);
  32014. ctx = NULL;
  32015. #endif /* end of RSA private key match tests */
  32016. #ifdef HAVE_ECC
  32017. #ifndef NO_WOLFSSL_SERVER
  32018. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32019. #else
  32020. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32021. #endif
  32022. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  32023. WOLFSSL_FILETYPE_PEM));
  32024. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  32025. WOLFSSL_FILETYPE_PEM));
  32026. ExpectNotNull(ssl = SSL_new(ctx));
  32027. #if !defined(NO_CHECK_PRIVATE_KEY)
  32028. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32029. #endif
  32030. SSL_free(ssl);
  32031. ssl = NULL;
  32032. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  32033. WOLFSSL_FILETYPE_PEM));
  32034. ExpectNotNull(ssl = SSL_new(ctx));
  32035. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  32036. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32037. #endif
  32038. SSL_free(ssl);
  32039. ssl = NULL;
  32040. SSL_CTX_free(ctx);
  32041. ctx = NULL;
  32042. #endif /* end of ECC private key match tests */
  32043. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  32044. #ifndef NO_WOLFSSL_SERVER
  32045. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32046. #else
  32047. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32048. #endif
  32049. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  32050. WOLFSSL_FILETYPE_PEM));
  32051. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  32052. WOLFSSL_FILETYPE_PEM));
  32053. ExpectNotNull(ssl = SSL_new(ctx));
  32054. #if !defined(NO_CHECK_PRIVATE_KEY)
  32055. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32056. #endif
  32057. SSL_free(ssl);
  32058. ssl = NULL;
  32059. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  32060. WOLFSSL_FILETYPE_PEM));
  32061. ExpectNotNull(ssl = SSL_new(ctx));
  32062. #if !defined(NO_CHECK_PRIVATE_KEY)
  32063. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32064. #endif
  32065. SSL_free(ssl);
  32066. ssl = NULL;
  32067. SSL_CTX_free(ctx);
  32068. ctx = NULL;
  32069. #endif /* end of Ed25519 private key match tests */
  32070. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  32071. #ifndef NO_WOLFSSL_SERVER
  32072. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32073. #else
  32074. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32075. #endif
  32076. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  32077. WOLFSSL_FILETYPE_PEM));
  32078. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  32079. WOLFSSL_FILETYPE_PEM));
  32080. ExpectNotNull(ssl = SSL_new(ctx));
  32081. #if !defined(NO_CHECK_PRIVATE_KEY)
  32082. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32083. #endif
  32084. SSL_free(ssl);
  32085. ssl = NULL;
  32086. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  32087. WOLFSSL_FILETYPE_PEM));
  32088. ExpectNotNull(ssl = SSL_new(ctx));
  32089. #if !defined(NO_CHECK_PRIVATE_KEY)
  32090. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  32091. #endif
  32092. SSL_free(ssl);
  32093. ssl = NULL;
  32094. SSL_CTX_free(ctx);
  32095. ctx = NULL;
  32096. #endif /* end of Ed448 private key match tests */
  32097. EVP_cleanup();
  32098. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  32099. CONF_modules_free();
  32100. ENGINE_cleanup();
  32101. CONF_modules_unload();
  32102. (void)ssl;
  32103. (void)ctx;
  32104. (void)pkey;
  32105. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32106. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32107. return EXPECT_RESULT();
  32108. }
  32109. static int test_wolfSSL_PEM_def_callback(void)
  32110. {
  32111. EXPECT_DECLS;
  32112. #ifdef OPENSSL_EXTRA
  32113. char buf[10];
  32114. const char* defpwd = "DEF PWD";
  32115. int defpwdLen = (int)XSTRLEN(defpwd);
  32116. int smallLen = 1;
  32117. /* Bad parameters. */
  32118. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  32119. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  32120. 0);
  32121. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  32122. XMEMSET(buf, 0, sizeof(buf));
  32123. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  32124. defpwdLen);
  32125. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  32126. ExpectIntEQ(buf[defpwdLen], 0);
  32127. /* Size of buffer is smaller than default password. */
  32128. XMEMSET(buf, 0, sizeof(buf));
  32129. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  32130. smallLen);
  32131. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  32132. ExpectIntEQ(buf[smallLen], 0);
  32133. #endif /* OPENSSL_EXTRA */
  32134. return EXPECT_RESULT();
  32135. }
  32136. static int test_wolfSSL_PEM_read_PrivateKey(void)
  32137. {
  32138. EXPECT_DECLS;
  32139. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  32140. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  32141. XFILE file = XBADFILE;
  32142. #if !defined(NO_RSA)
  32143. const char* fname_rsa = "./certs/server-key.pem";
  32144. RSA* rsa = NULL;
  32145. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  32146. unsigned char* sig = NULL;
  32147. size_t sigLen = 0;
  32148. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  32149. size_t tbsLen = sizeof(tbs);
  32150. #endif
  32151. #if !defined(NO_DSA)
  32152. const char* fname_dsa = "./certs/dsa2048.pem";
  32153. #endif
  32154. #if defined(HAVE_ECC)
  32155. const char* fname_ec = "./certs/ecc-key.pem";
  32156. #endif
  32157. #if !defined(NO_DH)
  32158. const char* fname_dh = "./certs/dh-priv-2048.pem";
  32159. #endif
  32160. EVP_PKEY* pkey = NULL;
  32161. /* Check error case. */
  32162. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  32163. /* not a PEM key. */
  32164. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  32165. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32166. if (file != XBADFILE)
  32167. XFCLOSE(file);
  32168. file = XBADFILE;
  32169. #ifndef NO_RSA
  32170. /* Read in an RSA key. */
  32171. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  32172. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32173. if (file != XBADFILE)
  32174. XFCLOSE(file);
  32175. file = XBADFILE;
  32176. /* Make sure the key is usable by signing some data with it. */
  32177. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  32178. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  32179. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  32180. DYNAMIC_TYPE_TMP_BUFFER));
  32181. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  32182. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  32183. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  32184. WOLFSSL_SUCCESS);
  32185. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  32186. EVP_PKEY_CTX_free(ctx);
  32187. EVP_PKEY_free(pkey);
  32188. pkey = NULL;
  32189. #endif
  32190. #ifndef NO_DSA
  32191. /* Read in a DSA key. */
  32192. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  32193. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  32194. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32195. EVP_PKEY_free(pkey);
  32196. pkey = NULL;
  32197. #else
  32198. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32199. #endif
  32200. if (file != XBADFILE)
  32201. XFCLOSE(file);
  32202. file = XBADFILE;
  32203. #endif
  32204. #ifdef HAVE_ECC
  32205. /* Read in an EC key. */
  32206. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  32207. ExpectNotNull(pkey = EVP_PKEY_new());
  32208. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  32209. if (file != XBADFILE)
  32210. XFCLOSE(file);
  32211. file = XBADFILE;
  32212. EVP_PKEY_free(pkey);
  32213. pkey = NULL;
  32214. #endif
  32215. #ifndef NO_DH
  32216. /* Read in a DH key. */
  32217. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  32218. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32219. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  32220. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32221. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32222. EVP_PKEY_free(pkey);
  32223. pkey = NULL;
  32224. #else
  32225. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  32226. #endif
  32227. if (file != XBADFILE)
  32228. XFCLOSE(file);
  32229. file = XBADFILE;
  32230. #endif
  32231. #endif
  32232. return EXPECT_RESULT();
  32233. }
  32234. static int test_wolfSSL_PEM_read_PUBKEY(void)
  32235. {
  32236. EXPECT_DECLS;
  32237. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  32238. && !defined(NO_FILESYSTEM)
  32239. XFILE file = XBADFILE;
  32240. const char* fname = "./certs/client-keyPub.pem";
  32241. EVP_PKEY* pkey = NULL;
  32242. /* Check error case. */
  32243. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  32244. /* Read in an RSA key. */
  32245. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32246. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  32247. EVP_PKEY_free(pkey);
  32248. pkey = NULL;
  32249. if (file != XBADFILE)
  32250. XFCLOSE(file);
  32251. file = XBADFILE;
  32252. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32253. ExpectNotNull(pkey = EVP_PKEY_new());
  32254. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  32255. EVP_PKEY_free(pkey);
  32256. if (file != XBADFILE)
  32257. XFCLOSE(file);
  32258. #endif
  32259. return EXPECT_RESULT();
  32260. }
  32261. /* test loading RSA key using BIO */
  32262. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  32263. {
  32264. EXPECT_DECLS;
  32265. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  32266. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  32267. !defined(NO_BIO)
  32268. BIO* bio = NULL;
  32269. XFILE file = XBADFILE;
  32270. const char* fname = "./certs/server-key.pem";
  32271. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  32272. EVP_PKEY* pkey = NULL;
  32273. size_t sz = 0;
  32274. byte* buf = NULL;
  32275. EVP_PKEY* pkey2 = NULL;
  32276. EVP_PKEY* pkey3 = NULL;
  32277. RSA* rsa_key = NULL;
  32278. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  32279. unsigned char extra[10];
  32280. int i;
  32281. BIO* pub_bio = NULL;
  32282. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  32283. #endif
  32284. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32285. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32286. ExpectIntGT(sz = XFTELL(file), 0);
  32287. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32288. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32289. if (buf != NULL) {
  32290. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32291. }
  32292. if (file != XBADFILE) {
  32293. XFCLOSE(file);
  32294. file = XBADFILE;
  32295. }
  32296. /* Test using BIO new mem and loading PEM private key */
  32297. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32298. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32299. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32300. buf = NULL;
  32301. BIO_free(bio);
  32302. bio = NULL;
  32303. /* New empty EVP_PKEY */
  32304. ExpectNotNull(pkey2 = EVP_PKEY_new());
  32305. if (pkey2 != NULL) {
  32306. pkey2->type = EVP_PKEY_RSA;
  32307. }
  32308. /* Test parameter copy */
  32309. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  32310. EVP_PKEY_free(pkey2);
  32311. EVP_PKEY_free(pkey);
  32312. pkey = NULL;
  32313. /* Qt unit test case : rsa pkcs8 key */
  32314. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  32315. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32316. ExpectIntGT(sz = XFTELL(file), 0);
  32317. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32318. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32319. if (buf) {
  32320. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32321. }
  32322. if (file != XBADFILE) {
  32323. XFCLOSE(file);
  32324. file = XBADFILE;
  32325. }
  32326. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32327. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32328. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32329. buf = NULL;
  32330. BIO_free(bio);
  32331. bio = NULL;
  32332. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32333. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  32334. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  32335. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32336. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32337. #else
  32338. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32339. #endif
  32340. RSA_free(rsa_key);
  32341. EVP_PKEY_free(pkey3);
  32342. EVP_PKEY_free(pkey);
  32343. pkey = NULL;
  32344. pkey2 = NULL;
  32345. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  32346. #define BIO_PEM_TEST_CHAR 'a'
  32347. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  32348. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32349. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  32350. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32351. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  32352. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  32353. (long)sizeof_server_key_der_2048));
  32354. ExpectNull(pkey);
  32355. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  32356. (long)sizeof_server_key_der_2048));
  32357. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  32358. WOLFSSL_FAILURE);
  32359. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  32360. WOLFSSL_FAILURE);
  32361. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32362. WOLFSSL_SUCCESS);
  32363. ExpectIntGT(BIO_pending(bio), 0);
  32364. ExpectIntEQ(BIO_pending(bio), 1679);
  32365. /* Check if the pubkey API writes only the public key */
  32366. #ifdef WOLFSSL_KEY_GEN
  32367. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  32368. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  32369. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  32370. ExpectIntGT(BIO_pending(pub_bio), 0);
  32371. /* Previously both the private key and the pubkey calls would write
  32372. * out the private key and the PEM header was the only difference.
  32373. * The public PEM should be significantly shorter than the
  32374. * private key versison. */
  32375. ExpectIntEQ(BIO_pending(pub_bio), 451);
  32376. #else
  32377. /* Not supported. */
  32378. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  32379. #endif
  32380. /* test creating new EVP_PKEY with good args */
  32381. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32382. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  32383. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  32384. pkey->pkey_sz), 0);
  32385. }
  32386. /* test of reuse of EVP_PKEY */
  32387. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  32388. ExpectIntEQ(BIO_pending(bio), 0);
  32389. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32390. SSL_SUCCESS);
  32391. /* add 10 extra bytes after PEM */
  32392. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  32393. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  32394. ExpectNotNull(pkey);
  32395. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  32396. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  32397. pkey->pkey_sz), 0);
  32398. }
  32399. /* check 10 extra bytes still there */
  32400. ExpectIntEQ(BIO_pending(bio), 10);
  32401. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  32402. for (i = 0; i < 10; i++) {
  32403. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  32404. }
  32405. BIO_free(pub_bio);
  32406. BIO_free(bio);
  32407. bio = NULL;
  32408. EVP_PKEY_free(pkey);
  32409. pkey = NULL;
  32410. EVP_PKEY_free(pkey2);
  32411. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  32412. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  32413. * !NO_FILESYSTEM && !NO_BIO */
  32414. return EXPECT_RESULT();
  32415. }
  32416. /* test loading ECC key using BIO */
  32417. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  32418. {
  32419. EXPECT_DECLS;
  32420. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  32421. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32422. BIO* bio = NULL;
  32423. EVP_PKEY* pkey = NULL;
  32424. XFILE file = XBADFILE;
  32425. const char* fname = "./certs/ecc-key.pem";
  32426. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  32427. size_t sz = 0;
  32428. byte* buf = NULL;
  32429. EVP_PKEY* pkey2 = NULL;
  32430. EVP_PKEY* pkey3 = NULL;
  32431. EC_KEY* ec_key = NULL;
  32432. int nid = 0;
  32433. BIO* pub_bio = NULL;
  32434. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32435. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32436. ExpectIntGT(sz = XFTELL(file), 0);
  32437. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32438. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32439. if (buf) {
  32440. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32441. }
  32442. if (file != XBADFILE) {
  32443. XFCLOSE(file);
  32444. file = XBADFILE;
  32445. }
  32446. /* Test using BIO new mem and loading PEM private key */
  32447. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32448. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32449. BIO_free(bio);
  32450. bio = NULL;
  32451. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32452. buf = NULL;
  32453. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32454. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32455. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32456. WOLFSSL_SUCCESS);
  32457. ExpectIntGT(BIO_pending(bio), 0);
  32458. /* No parmeters. */
  32459. ExpectIntEQ(BIO_pending(bio), 227);
  32460. /* Check if the pubkey API writes only the public key */
  32461. #ifdef WOLFSSL_KEY_GEN
  32462. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  32463. ExpectIntGT(BIO_pending(pub_bio), 0);
  32464. /* Previously both the private key and the pubkey calls would write
  32465. * out the private key and the PEM header was the only difference.
  32466. * The public PEM should be significantly shorter than the
  32467. * private key versison. */
  32468. ExpectIntEQ(BIO_pending(pub_bio), 178);
  32469. #endif
  32470. BIO_free(pub_bio);
  32471. BIO_free(bio);
  32472. bio = NULL;
  32473. ExpectNotNull(pkey2 = EVP_PKEY_new());
  32474. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32475. if (pkey2 != NULL) {
  32476. pkey2->type = EVP_PKEY_EC;
  32477. }
  32478. /* Test parameter copy */
  32479. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  32480. /* Qt unit test case 1*/
  32481. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  32482. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  32483. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32484. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32485. #else
  32486. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32487. #endif
  32488. /* Test default digest */
  32489. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  32490. ExpectIntEQ(nid, NID_sha256);
  32491. EC_KEY_free(ec_key);
  32492. ec_key = NULL;
  32493. EVP_PKEY_free(pkey3);
  32494. pkey3 = NULL;
  32495. EVP_PKEY_free(pkey2);
  32496. pkey2 = NULL;
  32497. EVP_PKEY_free(pkey);
  32498. pkey = NULL;
  32499. /* Qt unit test case ec pkcs8 key */
  32500. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  32501. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  32502. ExpectIntGT(sz = XFTELL(file), 0);
  32503. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  32504. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  32505. if (buf) {
  32506. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  32507. }
  32508. if (file != XBADFILE) {
  32509. XFCLOSE(file);
  32510. file = XBADFILE;
  32511. }
  32512. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  32513. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  32514. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  32515. buf = NULL;
  32516. BIO_free(bio);
  32517. bio = NULL;
  32518. ExpectNotNull(pkey3 = EVP_PKEY_new());
  32519. /* Qt unit test case */
  32520. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  32521. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  32522. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  32523. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  32524. #else
  32525. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  32526. #endif
  32527. EC_KEY_free(ec_key);
  32528. EVP_PKEY_free(pkey3);
  32529. EVP_PKEY_free(pkey);
  32530. pkey = NULL;
  32531. #endif
  32532. return EXPECT_RESULT();
  32533. }
  32534. /* test loading DSA key using BIO */
  32535. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  32536. {
  32537. EXPECT_DECLS;
  32538. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  32539. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32540. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  32541. BIO* bio = NULL;
  32542. EVP_PKEY* pkey = NULL;
  32543. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  32544. /* Private DSA EVP_PKEY */
  32545. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32546. NULL));
  32547. BIO_free(bio);
  32548. bio = NULL;
  32549. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32550. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32551. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32552. NULL), 0);
  32553. #endif
  32554. #ifdef WOLFSSL_KEY_GEN
  32555. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  32556. ExpectIntEQ(BIO_pending(bio), 1178);
  32557. BIO_reset(bio);
  32558. #endif
  32559. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32560. 1);
  32561. ExpectIntEQ(BIO_pending(bio), 1196);
  32562. BIO_free(bio);
  32563. bio = NULL;
  32564. EVP_PKEY_free(pkey);
  32565. pkey = NULL;
  32566. #endif
  32567. #endif
  32568. return EXPECT_RESULT();
  32569. }
  32570. /* test loading DH key using BIO */
  32571. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  32572. {
  32573. EXPECT_DECLS;
  32574. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  32575. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  32576. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  32577. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  32578. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  32579. BIO* bio = NULL;
  32580. EVP_PKEY* pkey = NULL;
  32581. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  32582. /* Private DH EVP_PKEY */
  32583. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32584. NULL));
  32585. BIO_free(bio);
  32586. bio = NULL;
  32587. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32588. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32589. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32590. NULL), 0);
  32591. #endif
  32592. #ifdef WOLFSSL_KEY_GEN
  32593. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  32594. #endif
  32595. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32596. 1);
  32597. ExpectIntEQ(BIO_pending(bio), 806);
  32598. BIO_free(bio);
  32599. bio = NULL;
  32600. EVP_PKEY_free(pkey);
  32601. pkey = NULL;
  32602. #endif
  32603. #endif
  32604. return EXPECT_RESULT();
  32605. }
  32606. static int test_wolfSSL_PEM_PrivateKey(void)
  32607. {
  32608. EXPECT_DECLS;
  32609. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32610. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  32611. #ifndef NO_BIO
  32612. BIO* bio = NULL;
  32613. #endif
  32614. EVP_PKEY* pkey = NULL;
  32615. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  32616. #ifndef NO_BIO
  32617. /* test creating new EVP_PKEY with bad arg */
  32618. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  32619. /* Test bad EVP_PKEY type. */
  32620. /* New HMAC EVP_PKEY */
  32621. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  32622. ExpectNotNull(pkey = EVP_PKEY_new());
  32623. if (pkey != NULL) {
  32624. pkey->type = EVP_PKEY_HMAC;
  32625. }
  32626. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  32627. 0);
  32628. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32629. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32630. NULL), 0);
  32631. #endif
  32632. #ifdef WOLFSSL_KEY_GEN
  32633. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  32634. #endif
  32635. EVP_PKEY_free(pkey);
  32636. pkey = NULL;
  32637. BIO_free(bio);
  32638. bio = NULL;
  32639. /* key is DES encrypted */
  32640. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  32641. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  32642. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  32643. {
  32644. XFILE f = XBADFILE;
  32645. wc_pem_password_cb* passwd_cb = NULL;
  32646. void* passwd_cb_userdata;
  32647. SSL_CTX* ctx = NULL;
  32648. char passwd[] = "bad password";
  32649. #ifndef WOLFSSL_NO_TLS12
  32650. #ifndef NO_WOLFSSL_SERVER
  32651. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  32652. #else
  32653. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  32654. #endif
  32655. #else
  32656. #ifndef NO_WOLFSSL_SERVER
  32657. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  32658. #else
  32659. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  32660. #endif
  32661. #endif
  32662. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  32663. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  32664. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  32665. ExpectNull(passwd_cb_userdata =
  32666. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32667. /* fail case with password call back */
  32668. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  32669. (void*)passwd));
  32670. BIO_free(bio);
  32671. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  32672. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  32673. (void*)passwd));
  32674. BIO_free(bio);
  32675. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  32676. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  32677. if ((bio == NULL) && (f != XBADFILE)) {
  32678. XFCLOSE(f);
  32679. }
  32680. /* use callback that works */
  32681. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  32682. (void*)"yassl123"));
  32683. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  32684. EVP_PKEY_free(pkey);
  32685. pkey = NULL;
  32686. BIO_free(bio);
  32687. bio = NULL;
  32688. SSL_CTX_free(ctx);
  32689. }
  32690. #endif /* !defined(NO_DES3) */
  32691. #endif /* !NO_BIO */
  32692. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  32693. {
  32694. unsigned char buf[2048];
  32695. size_t bytes = 0;
  32696. XFILE f = XBADFILE;
  32697. SSL_CTX* ctx = NULL;
  32698. #ifndef WOLFSSL_NO_TLS12
  32699. #ifndef NO_WOLFSSL_SERVER
  32700. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  32701. #else
  32702. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  32703. #endif
  32704. #else
  32705. #ifndef NO_WOLFSSL_SERVER
  32706. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  32707. #else
  32708. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  32709. #endif
  32710. #endif
  32711. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  32712. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  32713. if (f != XBADFILE)
  32714. XFCLOSE(f);
  32715. server_key = buf;
  32716. pkey = NULL;
  32717. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  32718. ExpectNull(pkey);
  32719. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  32720. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  32721. EVP_PKEY_free(pkey);
  32722. pkey = NULL;
  32723. SSL_CTX_free(ctx);
  32724. server_key = NULL;
  32725. }
  32726. #endif
  32727. #ifndef NO_BIO
  32728. (void)bio;
  32729. #endif
  32730. (void)pkey;
  32731. (void)server_key;
  32732. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  32733. return EXPECT_RESULT();
  32734. }
  32735. static int test_wolfSSL_PEM_file_RSAKey(void)
  32736. {
  32737. EXPECT_DECLS;
  32738. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32739. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32740. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32741. RSA* rsa = NULL;
  32742. XFILE fp = XBADFILE;
  32743. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  32744. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  32745. if (fp != XBADFILE)
  32746. XFCLOSE(fp);
  32747. ExpectIntEQ(RSA_size(rsa), 256);
  32748. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  32749. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  32750. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  32751. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  32752. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  32753. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  32754. RSA_free(rsa);
  32755. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32756. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32757. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32758. return EXPECT_RESULT();
  32759. }
  32760. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  32761. {
  32762. EXPECT_DECLS;
  32763. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  32764. !defined(NO_FILESYSTEM) && \
  32765. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  32766. RSA* rsa = NULL;
  32767. XFILE f = NULL;
  32768. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  32769. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32770. ExpectIntEQ(RSA_size(rsa), 256);
  32771. if (f != XBADFILE) {
  32772. XFCLOSE(f);
  32773. f = XBADFILE;
  32774. }
  32775. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  32776. NULL), WOLFSSL_FAILURE);
  32777. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  32778. NULL), WOLFSSL_FAILURE);
  32779. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  32780. WOLFSSL_SUCCESS);
  32781. RSA_free(rsa);
  32782. #ifdef HAVE_ECC
  32783. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  32784. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  32785. if (f != XBADFILE)
  32786. XFCLOSE(f);
  32787. #endif /* HAVE_ECC */
  32788. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32789. return EXPECT_RESULT();
  32790. }
  32791. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  32792. {
  32793. EXPECT_DECLS;
  32794. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32795. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32796. XFILE file = XBADFILE;
  32797. const char* fname = "./certs/client-keyPub.pem";
  32798. RSA *rsa = NULL;
  32799. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  32800. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  32801. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  32802. ExpectIntEQ(RSA_size(rsa), 256);
  32803. RSA_free(rsa);
  32804. if (file != XBADFILE)
  32805. XFCLOSE(file);
  32806. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32807. return EXPECT_RESULT();
  32808. }
  32809. #ifndef NO_BIO
  32810. static int test_wolfSSL_PEM_bio_RSAKey(void)
  32811. {
  32812. EXPECT_DECLS;
  32813. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32814. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  32815. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  32816. RSA* rsa = NULL;
  32817. BIO* bio = NULL;
  32818. /* PrivateKey */
  32819. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32820. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  32821. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  32822. ExpectNotNull(rsa);
  32823. ExpectIntEQ(RSA_size(rsa), 256);
  32824. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  32825. NULL), WOLFSSL_FAILURE);
  32826. BIO_free(bio);
  32827. bio = NULL;
  32828. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32829. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  32830. NULL), WOLFSSL_SUCCESS);
  32831. BIO_free(bio);
  32832. bio = NULL;
  32833. RSA_free(rsa);
  32834. rsa = NULL;
  32835. /* PUBKEY */
  32836. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  32837. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32838. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32839. ExpectIntEQ(RSA_size(rsa), 256);
  32840. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32841. BIO_free(bio);
  32842. bio = NULL;
  32843. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32844. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  32845. BIO_free(bio);
  32846. bio = NULL;
  32847. RSA_free(rsa);
  32848. rsa = NULL;
  32849. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  32850. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  32851. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32852. BIO_free(bio);
  32853. bio = NULL;
  32854. RSA_free(rsa);
  32855. rsa = NULL;
  32856. #ifdef HAVE_ECC
  32857. /* ensure that non-rsa keys do not work */
  32858. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32859. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32860. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  32861. BIO_free(bio);
  32862. bio = NULL;
  32863. RSA_free(rsa);
  32864. rsa = NULL;
  32865. #endif /* HAVE_ECC */
  32866. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32867. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  32868. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  32869. return EXPECT_RESULT();
  32870. }
  32871. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  32872. {
  32873. EXPECT_DECLS;
  32874. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32875. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32876. RSA* rsa = NULL;
  32877. RSA* rsa_dup = NULL;
  32878. BIO* bio = NULL;
  32879. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  32880. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32881. ExpectIntEQ(RSA_size(rsa), 256);
  32882. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  32883. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  32884. /* Test duplicating empty key. */
  32885. ExpectNotNull(rsa_dup = RSA_new());
  32886. ExpectNull(RSAPublicKey_dup(rsa_dup));
  32887. RSA_free(rsa_dup);
  32888. rsa_dup = NULL;
  32889. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  32890. ExpectPtrNE(rsa_dup, rsa);
  32891. #endif
  32892. /* test if valgrind complains about unreleased memory */
  32893. RSA_up_ref(rsa);
  32894. RSA_free(rsa);
  32895. BIO_free(bio);
  32896. bio = NULL;
  32897. RSA_free(rsa);
  32898. rsa = NULL;
  32899. RSA_free(rsa_dup);
  32900. rsa_dup = NULL;
  32901. #ifdef HAVE_ECC
  32902. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  32903. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  32904. BIO_free(bio);
  32905. #endif /* HAVE_ECC */
  32906. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  32907. return EXPECT_RESULT();
  32908. }
  32909. static int test_wolfSSL_PEM_bio_DSAKey(void)
  32910. {
  32911. EXPECT_DECLS;
  32912. #ifndef HAVE_SELFTEST
  32913. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  32914. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  32915. DSA* dsa = NULL;
  32916. BIO* bio = NULL;
  32917. /* PrivateKey */
  32918. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  32919. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  32920. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32921. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32922. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  32923. NULL), WOLFSSL_FAILURE);
  32924. BIO_free(bio);
  32925. bio = NULL;
  32926. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32927. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  32928. NULL), WOLFSSL_SUCCESS);
  32929. BIO_free(bio);
  32930. bio = NULL;
  32931. DSA_free(dsa);
  32932. dsa = NULL;
  32933. /* PUBKEY */
  32934. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  32935. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  32936. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32937. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  32938. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  32939. BIO_free(bio);
  32940. bio = NULL;
  32941. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  32942. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  32943. BIO_free(bio);
  32944. bio = NULL;
  32945. DSA_free(dsa);
  32946. dsa = NULL;
  32947. #ifdef HAVE_ECC
  32948. /* ensure that non-dsa keys do not work */
  32949. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  32950. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  32951. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  32952. BIO_free(bio);
  32953. bio = NULL;
  32954. DSA_free(dsa);
  32955. dsa = NULL;
  32956. #endif /* HAVE_ECC */
  32957. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  32958. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  32959. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  32960. #endif /* HAVE_SELFTEST */
  32961. return EXPECT_RESULT();
  32962. }
  32963. static int test_wolfSSL_PEM_bio_ECKey(void)
  32964. {
  32965. EXPECT_DECLS;
  32966. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  32967. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  32968. EC_KEY* ec = NULL;
  32969. EC_KEY* ec2;
  32970. BIO* bio = NULL;
  32971. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  32972. unsigned char* pem = NULL;
  32973. int pLen;
  32974. #endif
  32975. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  32976. "MAA=\n"
  32977. "-----END PUBLIC KEY-----";
  32978. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  32979. "MAA=\n"
  32980. "-----END EC PRIVATE KEY-----";
  32981. /* PrivateKey */
  32982. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  32983. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  32984. ec2 = NULL;
  32985. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  32986. ExpectIntEQ(ec == ec2, 1);
  32987. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  32988. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  32989. NULL), WOLFSSL_FAILURE);
  32990. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  32991. NULL), WOLFSSL_FAILURE);
  32992. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  32993. WOLFSSL_FAILURE);
  32994. BIO_free(bio);
  32995. bio = NULL;
  32996. /* Public key data - fail. */
  32997. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  32998. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  32999. BIO_free(bio);
  33000. bio = NULL;
  33001. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  33002. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  33003. NULL), WOLFSSL_SUCCESS);
  33004. BIO_free(bio);
  33005. bio = NULL;
  33006. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  33007. NULL),WOLFSSL_FAILURE);
  33008. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  33009. WOLFSSL_FAILURE);
  33010. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  33011. WOLFSSL_FAILURE);
  33012. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  33013. WOLFSSL_SUCCESS);
  33014. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  33015. NULL), 0);
  33016. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  33017. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  33018. NULL), 0);
  33019. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  33020. NULL), 0);
  33021. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  33022. &pLen), 0);
  33023. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  33024. &pLen), 0);
  33025. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  33026. &pLen), 0);
  33027. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  33028. NULL), 0);
  33029. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  33030. &pLen), 1);
  33031. ExpectIntGT(pLen, 0);
  33032. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33033. #endif
  33034. EC_KEY_free(ec);
  33035. ec = NULL;
  33036. /* PUBKEY */
  33037. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  33038. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  33039. ec2 = NULL;
  33040. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  33041. ExpectIntEQ(ec == ec2, 1);
  33042. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  33043. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  33044. BIO_free(bio);
  33045. bio = NULL;
  33046. /* Test 0x30, 0x00 fails. */
  33047. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  33048. sizeof(ec_key_bad_1)));
  33049. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  33050. BIO_free(bio);
  33051. bio = NULL;
  33052. /* Private key data - fail. */
  33053. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  33054. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  33055. BIO_free(bio);
  33056. bio = NULL;
  33057. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  33058. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  33059. BIO_free(bio);
  33060. bio = NULL;
  33061. /* Same test as above, but with a file pointer rather than a BIO. */
  33062. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  33063. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  33064. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  33065. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  33066. EC_KEY_free(ec);
  33067. ec = NULL;
  33068. #ifndef NO_RSA
  33069. /* ensure that non-ec keys do not work */
  33070. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  33071. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  33072. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  33073. BIO_free(bio);
  33074. bio = NULL;
  33075. EC_KEY_free(ec);
  33076. ec = NULL;
  33077. #endif /* !NO_RSA */
  33078. /* Test 0x30, 0x00 fails. */
  33079. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  33080. sizeof(ec_priv_key_bad_1)));
  33081. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  33082. BIO_free(bio);
  33083. bio = NULL;
  33084. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  33085. return EXPECT_RESULT();
  33086. }
  33087. static int test_wolfSSL_PEM_PUBKEY(void)
  33088. {
  33089. EXPECT_DECLS;
  33090. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  33091. BIO* bio = NULL;
  33092. EVP_PKEY* pkey = NULL;
  33093. /* test creating new EVP_PKEY with bad arg */
  33094. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  33095. /* test loading ECC key using BIO */
  33096. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  33097. {
  33098. XFILE file = XBADFILE;
  33099. const char* fname = "./certs/ecc-client-keyPub.pem";
  33100. size_t sz = 0;
  33101. byte* buf = NULL;
  33102. EVP_PKEY* pkey2 = NULL;
  33103. EC_KEY* ec_key = NULL;
  33104. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  33105. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  33106. ExpectIntGT(sz = XFTELL(file), 0);
  33107. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  33108. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  33109. if (buf != NULL) {
  33110. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  33111. }
  33112. if (file != XBADFILE) {
  33113. XFCLOSE(file);
  33114. }
  33115. /* Test using BIO new mem and loading PEM private key */
  33116. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33117. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  33118. BIO_free(bio);
  33119. bio = NULL;
  33120. EVP_PKEY_free(pkey);
  33121. pkey = NULL;
  33122. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  33123. ExpectNotNull(pkey = EVP_PKEY_new());
  33124. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  33125. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  33126. BIO_free(bio);
  33127. bio = NULL;
  33128. /* Qt unit test case*/
  33129. ExpectNotNull(pkey2 = EVP_PKEY_new());
  33130. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  33131. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  33132. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  33133. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  33134. #else
  33135. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  33136. #endif
  33137. EC_KEY_free(ec_key);
  33138. EVP_PKEY_free(pkey2);
  33139. EVP_PKEY_free(pkey);
  33140. pkey = NULL;
  33141. }
  33142. #endif
  33143. (void)bio;
  33144. (void)pkey;
  33145. #endif
  33146. return EXPECT_RESULT();
  33147. }
  33148. #endif /* !NO_BIO */
  33149. static int test_DSA_do_sign_verify(void)
  33150. {
  33151. EXPECT_DECLS;
  33152. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  33153. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  33154. !defined(NO_DSA)
  33155. unsigned char digest[WC_SHA_DIGEST_SIZE];
  33156. DSA_SIG* sig = NULL;
  33157. DSA* dsa = NULL;
  33158. word32 bytes;
  33159. byte sigBin[DSA_SIG_SIZE];
  33160. int dsacheck;
  33161. #ifdef USE_CERT_BUFFERS_1024
  33162. byte tmp[ONEK_BUF];
  33163. XMEMSET(tmp, 0, sizeof(tmp));
  33164. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33165. bytes = sizeof_dsa_key_der_1024;
  33166. #elif defined(USE_CERT_BUFFERS_2048)
  33167. byte tmp[TWOK_BUF];
  33168. XMEMSET(tmp, 0, sizeof(tmp));
  33169. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33170. bytes = sizeof_dsa_key_der_2048;
  33171. #else
  33172. byte tmp[TWOK_BUF];
  33173. XFILE fp = XBADFILE;
  33174. XMEMSET(tmp, 0, sizeof(tmp));
  33175. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  33176. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  33177. if (fp != XBADFILE)
  33178. XFCLOSE(fp);
  33179. #endif /* END USE_CERT_BUFFERS_1024 */
  33180. XMEMSET(digest, 202, sizeof(digest));
  33181. ExpectNotNull(dsa = DSA_new());
  33182. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  33183. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  33184. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  33185. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  33186. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  33187. DSA_SIG_free(sig);
  33188. DSA_free(dsa);
  33189. #endif
  33190. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  33191. return EXPECT_RESULT();
  33192. }
  33193. static int test_wolfSSL_tmp_dh(void)
  33194. {
  33195. EXPECT_DECLS;
  33196. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33197. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  33198. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33199. byte buff[6000];
  33200. char file[] = "./certs/dsaparams.pem";
  33201. XFILE f = XBADFILE;
  33202. int bytes = 0;
  33203. DSA* dsa = NULL;
  33204. DH* dh = NULL;
  33205. #if defined(WOLFSSL_DH_EXTRA) && \
  33206. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33207. DH* dh2 = NULL;
  33208. #endif
  33209. BIO* bio = NULL;
  33210. SSL* ssl = NULL;
  33211. SSL_CTX* ctx = NULL;
  33212. #ifndef NO_WOLFSSL_SERVER
  33213. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33214. #else
  33215. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33216. #endif
  33217. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33218. WOLFSSL_FILETYPE_PEM));
  33219. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33220. WOLFSSL_FILETYPE_PEM));
  33221. ExpectNotNull(ssl = SSL_new(ctx));
  33222. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  33223. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  33224. if (f != XBADFILE)
  33225. XFCLOSE(f);
  33226. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33227. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  33228. ExpectNotNull(dsa);
  33229. dh = wolfSSL_DSA_dup_DH(dsa);
  33230. ExpectNotNull(dh);
  33231. #if defined(WOLFSSL_DH_EXTRA) && \
  33232. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33233. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  33234. #endif
  33235. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  33236. #ifndef NO_WOLFSSL_SERVER
  33237. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  33238. #else
  33239. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  33240. #endif
  33241. BIO_free(bio);
  33242. DSA_free(dsa);
  33243. DH_free(dh);
  33244. dh = NULL;
  33245. #if defined(WOLFSSL_DH_EXTRA) && \
  33246. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  33247. DH_free(dh2);
  33248. dh2 = NULL;
  33249. #endif
  33250. SSL_free(ssl);
  33251. SSL_CTX_free(ctx);
  33252. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33253. #endif
  33254. return EXPECT_RESULT();
  33255. }
  33256. static int test_wolfSSL_ctrl(void)
  33257. {
  33258. EXPECT_DECLS;
  33259. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  33260. byte buff[6000];
  33261. BIO* bio = NULL;
  33262. int bytes;
  33263. BUF_MEM* ptr = NULL;
  33264. XMEMSET(buff, 0, sizeof(buff));
  33265. bytes = sizeof(buff);
  33266. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  33267. ExpectNotNull(BIO_s_socket());
  33268. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  33269. /* needs tested after stubs filled out @TODO
  33270. SSL_ctrl
  33271. SSL_CTX_ctrl
  33272. */
  33273. BIO_free(bio);
  33274. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  33275. return EXPECT_RESULT();
  33276. }
  33277. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  33278. {
  33279. EXPECT_DECLS;
  33280. #ifdef OPENSSL_EXTRA
  33281. static const unsigned char pw[] = "password";
  33282. static const int pwSz = sizeof(pw) - 1;
  33283. size_t checkPwSz = 0;
  33284. const unsigned char* checkPw = NULL;
  33285. WOLFSSL_EVP_PKEY* key = NULL;
  33286. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  33287. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  33288. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  33289. pwSz));
  33290. if (key != NULL) {
  33291. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  33292. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  33293. ExpectIntEQ(key->pkey_sz, pwSz);
  33294. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  33295. }
  33296. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  33297. ExpectIntEQ((int)checkPwSz, pwSz);
  33298. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  33299. wolfSSL_EVP_PKEY_free(key);
  33300. key = NULL;
  33301. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  33302. 0));
  33303. ExpectIntEQ(key->pkey_sz, 0);
  33304. if (EXPECT_SUCCESS()) {
  33305. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  33306. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  33307. }
  33308. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  33309. ExpectIntEQ((int)checkPwSz, 0);
  33310. wolfSSL_EVP_PKEY_free(key);
  33311. key = NULL;
  33312. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  33313. 0));
  33314. ExpectIntEQ(key->pkey_sz, 0);
  33315. if (EXPECT_SUCCESS()) {
  33316. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  33317. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  33318. }
  33319. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  33320. ExpectIntEQ((int)checkPwSz, 0);
  33321. wolfSSL_EVP_PKEY_free(key);
  33322. key = NULL;
  33323. #endif /* OPENSSL_EXTRA */
  33324. return EXPECT_RESULT();
  33325. }
  33326. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  33327. {
  33328. EXPECT_DECLS;
  33329. #ifdef OPENSSL_EXTRA
  33330. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  33331. const char *priv = "ABCDEFGHIJKLMNOP";
  33332. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  33333. WOLFSSL_EVP_PKEY* key = NULL;
  33334. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33335. NULL, NULL, AES_128_KEY_SIZE, cipher));
  33336. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33337. NULL, (const unsigned char *)priv, 0, cipher));
  33338. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33339. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  33340. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  33341. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  33342. wolfSSL_EVP_PKEY_free(key);
  33343. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  33344. #endif /* OPENSSL_EXTRA */
  33345. return EXPECT_RESULT();
  33346. }
  33347. static int test_wolfSSL_EVP_Digest(void)
  33348. {
  33349. EXPECT_DECLS;
  33350. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  33351. const char* in = "abc";
  33352. int inLen = (int)XSTRLEN(in);
  33353. byte out[WC_SHA256_DIGEST_SIZE];
  33354. unsigned int outLen;
  33355. const char* expOut =
  33356. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  33357. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  33358. "\x15\xAD";
  33359. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  33360. "SHA256", NULL), 1);
  33361. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  33362. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  33363. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  33364. return EXPECT_RESULT();
  33365. }
  33366. static int test_wolfSSL_EVP_Digest_all(void)
  33367. {
  33368. EXPECT_DECLS;
  33369. #ifdef OPENSSL_EXTRA
  33370. const char* digests[] = {
  33371. #ifndef NO_MD5
  33372. "MD5",
  33373. #endif
  33374. #ifndef NO_SHA
  33375. "SHA",
  33376. #endif
  33377. #ifdef WOLFSSL_SHA224
  33378. "SHA224",
  33379. #endif
  33380. #ifndef NO_SHA256
  33381. "SHA256",
  33382. #endif
  33383. #ifdef WOLFSSL_SHA384
  33384. "SHA384",
  33385. #endif
  33386. #ifdef WOLFSSL_SHA512
  33387. "SHA512",
  33388. #endif
  33389. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  33390. "SHA512_224",
  33391. #endif
  33392. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  33393. "SHA512_256",
  33394. #endif
  33395. #ifdef WOLFSSL_SHA3
  33396. #ifndef WOLFSSL_NOSHA3_224
  33397. "SHA3_224",
  33398. #endif
  33399. #ifndef WOLFSSL_NOSHA3_256
  33400. "SHA3_256",
  33401. #endif
  33402. "SHA3_384",
  33403. #ifndef WOLFSSL_NOSHA3_512
  33404. "SHA3_512",
  33405. #endif
  33406. #endif /* WOLFSSL_SHA3 */
  33407. NULL
  33408. };
  33409. const char** d;
  33410. const unsigned char in[] = "abc";
  33411. int inLen = XSTR_SIZEOF(in);
  33412. byte out[WC_MAX_DIGEST_SIZE];
  33413. unsigned int outLen;
  33414. for (d = digests; *d != NULL; d++) {
  33415. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  33416. ExpectIntGT(outLen, 0);
  33417. ExpectIntEQ(EVP_MD_size(*d), outLen);
  33418. }
  33419. #endif
  33420. return EXPECT_RESULT();
  33421. }
  33422. static int test_wolfSSL_EVP_MD_size(void)
  33423. {
  33424. EXPECT_DECLS;
  33425. #ifdef OPENSSL_EXTRA
  33426. WOLFSSL_EVP_MD_CTX mdCtx;
  33427. #ifdef WOLFSSL_SHA3
  33428. #ifndef WOLFSSL_NOSHA3_224
  33429. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33430. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  33431. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  33432. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  33433. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33434. #endif
  33435. #ifndef WOLFSSL_NOSHA3_256
  33436. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33437. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  33438. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  33439. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  33440. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33441. #endif
  33442. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33443. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  33444. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  33445. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  33446. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33447. #ifndef WOLFSSL_NOSHA3_512
  33448. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33449. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  33450. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  33451. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  33452. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33453. #endif
  33454. #endif /* WOLFSSL_SHA3 */
  33455. #ifndef NO_SHA256
  33456. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33457. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  33458. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33459. WC_SHA256_DIGEST_SIZE);
  33460. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33461. WC_SHA256_BLOCK_SIZE);
  33462. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  33463. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  33464. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33465. #endif
  33466. #ifndef NO_MD5
  33467. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33468. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  33469. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33470. WC_MD5_DIGEST_SIZE);
  33471. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33472. WC_MD5_BLOCK_SIZE);
  33473. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  33474. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  33475. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33476. #endif
  33477. #ifdef WOLFSSL_SHA224
  33478. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33479. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  33480. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33481. WC_SHA224_DIGEST_SIZE);
  33482. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33483. WC_SHA224_BLOCK_SIZE);
  33484. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  33485. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  33486. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33487. #endif
  33488. #ifdef WOLFSSL_SHA384
  33489. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33490. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  33491. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33492. WC_SHA384_DIGEST_SIZE);
  33493. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33494. WC_SHA384_BLOCK_SIZE);
  33495. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  33496. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  33497. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33498. #endif
  33499. #ifdef WOLFSSL_SHA512
  33500. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33501. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  33502. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33503. WC_SHA512_DIGEST_SIZE);
  33504. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33505. WC_SHA512_BLOCK_SIZE);
  33506. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  33507. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  33508. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33509. #endif
  33510. #ifndef NO_SHA
  33511. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33512. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  33513. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33514. WC_SHA_DIGEST_SIZE);
  33515. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33516. WC_SHA_BLOCK_SIZE);
  33517. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  33518. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  33519. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33520. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33521. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  33522. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33523. WC_SHA_DIGEST_SIZE);
  33524. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33525. WC_SHA_BLOCK_SIZE);
  33526. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  33527. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  33528. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33529. #endif
  33530. /* error case */
  33531. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33532. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  33533. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  33534. BAD_FUNC_ARG);
  33535. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  33536. /* Cleanup is valid on uninit'ed struct */
  33537. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33538. #endif /* OPENSSL_EXTRA */
  33539. return EXPECT_RESULT();
  33540. }
  33541. static int test_wolfSSL_EVP_MD_pkey_type(void)
  33542. {
  33543. EXPECT_DECLS;
  33544. #ifdef OPENSSL_EXTRA
  33545. const WOLFSSL_EVP_MD* md;
  33546. #ifndef NO_MD5
  33547. ExpectNotNull(md = EVP_md5());
  33548. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  33549. #endif
  33550. #ifndef NO_SHA
  33551. ExpectNotNull(md = EVP_sha1());
  33552. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  33553. #endif
  33554. #ifdef WOLFSSL_SHA224
  33555. ExpectNotNull(md = EVP_sha224());
  33556. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  33557. #endif
  33558. ExpectNotNull(md = EVP_sha256());
  33559. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  33560. #ifdef WOLFSSL_SHA384
  33561. ExpectNotNull(md = EVP_sha384());
  33562. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  33563. #endif
  33564. #ifdef WOLFSSL_SHA512
  33565. ExpectNotNull(md = EVP_sha512());
  33566. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  33567. #endif
  33568. #endif
  33569. return EXPECT_RESULT();
  33570. }
  33571. #ifdef OPENSSL_EXTRA
  33572. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  33573. size_t testKeySz, const char* testData, size_t testDataSz,
  33574. const byte* testResult, size_t testResultSz)
  33575. {
  33576. EXPECT_DECLS;
  33577. unsigned char check[WC_MAX_DIGEST_SIZE];
  33578. size_t checkSz = -1;
  33579. WOLFSSL_EVP_PKEY* key = NULL;
  33580. WOLFSSL_EVP_MD_CTX mdCtx;
  33581. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  33582. testKey, (int)testKeySz));
  33583. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33584. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  33585. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33586. (unsigned int)testDataSz), 1);
  33587. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33588. ExpectIntEQ((int)checkSz, (int)testResultSz);
  33589. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33590. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33591. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  33592. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33593. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  33594. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33595. (unsigned int)testDataSz), 1);
  33596. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  33597. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33598. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33599. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  33600. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33601. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33602. ExpectIntEQ((int)checkSz, (int)testResultSz);
  33603. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33604. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33605. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33606. (unsigned int)testDataSz - 4), 1);
  33607. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33608. ExpectIntEQ((int)checkSz,(int)testResultSz);
  33609. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  33610. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33611. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  33612. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33613. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33614. (unsigned int)testDataSz - 4), 1);
  33615. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  33616. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  33617. wolfSSL_EVP_PKEY_free(key);
  33618. return EXPECT_RESULT();
  33619. }
  33620. #endif
  33621. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  33622. {
  33623. EXPECT_DECLS;
  33624. #ifdef OPENSSL_EXTRA
  33625. static const unsigned char testKey[] =
  33626. {
  33627. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33628. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  33629. 0x0b, 0x0b, 0x0b, 0x0b
  33630. };
  33631. static const char testData[] = "Hi There";
  33632. #ifdef WOLFSSL_SHA224
  33633. static const unsigned char testResultSha224[] =
  33634. {
  33635. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  33636. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  33637. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  33638. 0x53, 0x68, 0x4b, 0x22
  33639. };
  33640. #endif
  33641. #ifndef NO_SHA256
  33642. static const unsigned char testResultSha256[] =
  33643. {
  33644. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  33645. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  33646. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  33647. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  33648. };
  33649. #endif
  33650. #ifdef WOLFSSL_SHA384
  33651. static const unsigned char testResultSha384[] =
  33652. {
  33653. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  33654. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  33655. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  33656. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  33657. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  33658. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  33659. };
  33660. #endif
  33661. #ifdef WOLFSSL_SHA512
  33662. static const unsigned char testResultSha512[] =
  33663. {
  33664. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  33665. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  33666. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  33667. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  33668. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  33669. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  33670. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  33671. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  33672. };
  33673. #endif
  33674. #ifdef WOLFSSL_SHA3
  33675. #ifndef WOLFSSL_NOSHA3_224
  33676. static const unsigned char testResultSha3_224[] =
  33677. {
  33678. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  33679. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  33680. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  33681. 0xf3, 0xc8, 0x60, 0xf7
  33682. };
  33683. #endif
  33684. #ifndef WOLFSSL_NOSHA3_256
  33685. static const unsigned char testResultSha3_256[] =
  33686. {
  33687. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  33688. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  33689. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  33690. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  33691. };
  33692. #endif
  33693. #ifndef WOLFSSL_NOSHA3_384
  33694. static const unsigned char testResultSha3_384[] =
  33695. {
  33696. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  33697. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  33698. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  33699. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  33700. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  33701. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  33702. };
  33703. #endif
  33704. #ifndef WOLFSSL_NOSHA3_512
  33705. static const unsigned char testResultSha3_512[] =
  33706. {
  33707. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  33708. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  33709. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  33710. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  33711. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  33712. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  33713. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  33714. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  33715. };
  33716. #endif
  33717. #endif
  33718. #ifndef NO_SHA256
  33719. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  33720. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  33721. sizeof(testResultSha256)), TEST_SUCCESS);
  33722. #endif
  33723. #ifdef WOLFSSL_SHA224
  33724. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  33725. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  33726. sizeof(testResultSha224)), TEST_SUCCESS);
  33727. #endif
  33728. #ifdef WOLFSSL_SHA384
  33729. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  33730. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  33731. sizeof(testResultSha384)), TEST_SUCCESS);
  33732. #endif
  33733. #ifdef WOLFSSL_SHA512
  33734. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  33735. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  33736. sizeof(testResultSha512)), TEST_SUCCESS);
  33737. #endif
  33738. #ifdef WOLFSSL_SHA3
  33739. #ifndef WOLFSSL_NOSHA3_224
  33740. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  33741. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  33742. sizeof(testResultSha3_224)), TEST_SUCCESS);
  33743. #endif
  33744. #ifndef WOLFSSL_NOSHA3_256
  33745. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  33746. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  33747. sizeof(testResultSha3_256)), TEST_SUCCESS);
  33748. #endif
  33749. #ifndef WOLFSSL_NOSHA3_384
  33750. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  33751. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  33752. sizeof(testResultSha3_384)), TEST_SUCCESS);
  33753. #endif
  33754. #ifndef WOLFSSL_NOSHA3_512
  33755. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  33756. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  33757. sizeof(testResultSha3_512)), TEST_SUCCESS);
  33758. #endif
  33759. #endif
  33760. #endif /* OPENSSL_EXTRA */
  33761. return EXPECT_RESULT();
  33762. }
  33763. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  33764. {
  33765. EXPECT_DECLS;
  33766. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  33767. WOLFSSL_EVP_PKEY* privKey = NULL;
  33768. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33769. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  33770. const char testData[] = "Hi There";
  33771. WOLFSSL_EVP_MD_CTX mdCtx;
  33772. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  33773. int ret;
  33774. size_t checkSz = -1;
  33775. int sz = 2048 / 8;
  33776. const unsigned char* cp;
  33777. const unsigned char* p;
  33778. unsigned char check[2048/8];
  33779. size_t i;
  33780. int paddings[] = {
  33781. RSA_PKCS1_PADDING,
  33782. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  33783. RSA_PKCS1_PSS_PADDING,
  33784. #endif
  33785. };
  33786. cp = client_key_der_2048;
  33787. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  33788. sizeof_client_key_der_2048)));
  33789. p = client_keypub_der_2048;
  33790. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33791. sizeof_client_keypub_der_2048)));
  33792. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33793. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  33794. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33795. NULL, privKey), 1);
  33796. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33797. (unsigned int)XSTRLEN(testData)), 1);
  33798. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33799. ExpectIntEQ((int)checkSz, sz);
  33800. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33801. ExpectIntEQ((int)checkSz,sz);
  33802. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33803. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  33804. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  33805. ExpectIntEQ(ret, 1);
  33806. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33807. ExpectIntEQ(ret, 1);
  33808. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33809. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33810. NULL, pubKey), 1);
  33811. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33812. (unsigned int)XSTRLEN(testData)),
  33813. 1);
  33814. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33815. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33816. ExpectIntEQ(ret, 1);
  33817. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33818. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33819. NULL, privKey), 1);
  33820. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33821. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33822. ExpectIntEQ((int)checkSz, sz);
  33823. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33824. ExpectIntEQ((int)checkSz, sz);
  33825. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33826. (unsigned int)XSTRLEN(testData) - 4), 1);
  33827. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33828. ExpectIntEQ((int)checkSz, sz);
  33829. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33830. ExpectIntEQ(ret, 1);
  33831. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33832. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33833. NULL, pubKey), 1);
  33834. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33835. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33836. (unsigned int)XSTRLEN(testData) - 4),
  33837. 1);
  33838. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33839. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33840. ExpectIntEQ(ret, 1);
  33841. /* Check all signing padding types */
  33842. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  33843. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33844. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  33845. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  33846. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33847. paddings[i]), 1);
  33848. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33849. (unsigned int)XSTRLEN(testData)), 1);
  33850. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33851. ExpectIntEQ((int)checkSz, sz);
  33852. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33853. ExpectIntEQ((int)checkSz,sz);
  33854. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33855. ExpectIntEQ(ret, 1);
  33856. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33857. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  33858. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  33859. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  33860. paddings[i]), 1);
  33861. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33862. (unsigned int)XSTRLEN(testData)), 1);
  33863. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33864. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33865. ExpectIntEQ(ret, 1);
  33866. }
  33867. wolfSSL_EVP_PKEY_free(pubKey);
  33868. wolfSSL_EVP_PKEY_free(privKey);
  33869. #endif
  33870. return EXPECT_RESULT();
  33871. }
  33872. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  33873. {
  33874. EXPECT_DECLS;
  33875. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  33876. WOLFSSL_EVP_PKEY* privKey = NULL;
  33877. WOLFSSL_EVP_PKEY* pubKey = NULL;
  33878. const char testData[] = "Hi There";
  33879. WOLFSSL_EVP_MD_CTX mdCtx;
  33880. int ret;
  33881. size_t checkSz = -1;
  33882. const unsigned char* cp;
  33883. const unsigned char* p;
  33884. unsigned char check[2048/8];
  33885. cp = ecc_clikey_der_256;
  33886. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  33887. sizeof_ecc_clikey_der_256));
  33888. p = ecc_clikeypub_der_256;
  33889. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  33890. sizeof_ecc_clikeypub_der_256)));
  33891. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33892. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33893. NULL, privKey), 1);
  33894. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  33895. (unsigned int)XSTRLEN(testData)), 1);
  33896. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33897. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33898. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33899. ExpectIntEQ(ret, 1);
  33900. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33901. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33902. NULL, pubKey), 1);
  33903. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  33904. (unsigned int)XSTRLEN(testData)),
  33905. 1);
  33906. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33907. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33908. ExpectIntEQ(ret, 1);
  33909. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33910. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33911. NULL, privKey), 1);
  33912. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  33913. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  33914. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33915. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  33916. (unsigned int)XSTRLEN(testData) - 4), 1);
  33917. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  33918. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33919. ExpectIntEQ(ret, 1);
  33920. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  33921. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  33922. NULL, pubKey), 1);
  33923. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  33924. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  33925. (unsigned int)XSTRLEN(testData) - 4),
  33926. 1);
  33927. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  33928. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  33929. ExpectIntEQ(ret, 1);
  33930. wolfSSL_EVP_PKEY_free(pubKey);
  33931. wolfSSL_EVP_PKEY_free(privKey);
  33932. #endif
  33933. return EXPECT_RESULT();
  33934. }
  33935. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  33936. {
  33937. EXPECT_DECLS;
  33938. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33939. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  33940. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33941. char caFile[] = "./certs/client-ca.pem";
  33942. char clientFile[] = "./certs/client-cert.pem";
  33943. SSL_CTX* ctx = NULL;
  33944. X509* x509 = NULL;
  33945. BIO *bio = NULL;
  33946. X509 *cert = NULL;
  33947. X509 *ca = NULL;
  33948. STACK_OF(X509) *chain = NULL;
  33949. STACK_OF(X509) *chain2 = NULL;
  33950. #ifndef NO_WOLFSSL_SERVER
  33951. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33952. #else
  33953. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33954. #endif
  33955. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  33956. WOLFSSL_FILETYPE_PEM));
  33957. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  33958. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  33959. WOLFSSL_FILETYPE_PEM));
  33960. /* additional test of getting EVP_PKEY key size from X509
  33961. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  33962. * allowed with user RSA */
  33963. {
  33964. EVP_PKEY* pkey = NULL;
  33965. #if defined(HAVE_ECC)
  33966. X509* ecX509 = NULL;
  33967. #endif /* HAVE_ECC */
  33968. ExpectNotNull(pkey = X509_get_pubkey(x509));
  33969. /* current RSA key is 2048 bit (256 bytes) */
  33970. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  33971. EVP_PKEY_free(pkey);
  33972. pkey = NULL;
  33973. #if defined(HAVE_ECC)
  33974. #if defined(USE_CERT_BUFFERS_256)
  33975. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  33976. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  33977. SSL_FILETYPE_ASN1));
  33978. #else
  33979. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  33980. cliEccCertFile, SSL_FILETYPE_PEM));
  33981. #endif
  33982. pkey = X509_get_pubkey(ecX509);
  33983. ExpectNotNull(pkey);
  33984. /* current ECC key is 256 bit (32 bytes) */
  33985. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  33986. X509_free(ecX509);
  33987. ecX509 = NULL;
  33988. EVP_PKEY_free(pkey);
  33989. pkey = NULL;
  33990. #endif /* HAVE_ECC */
  33991. }
  33992. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  33993. if (EXPECT_SUCCESS()) {
  33994. x509 = NULL;
  33995. }
  33996. #ifdef WOLFSSL_ENCRYPTED_KEYS
  33997. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  33998. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  33999. #endif
  34000. SSL_CTX_free(ctx);
  34001. ctx = NULL;
  34002. #ifndef NO_WOLFSSL_SERVER
  34003. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34004. #else
  34005. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34006. #endif
  34007. /* Test haproxy use case */
  34008. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  34009. /* Read Certificate */
  34010. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  34011. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  34012. ExpectNotNull(chain = sk_X509_new_null());
  34013. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  34014. if (EXPECT_SUCCESS()) {
  34015. ca = NULL;
  34016. }
  34017. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  34018. ExpectNotNull(ca = sk_X509_shift(chain2));
  34019. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  34020. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  34021. if (EXPECT_SUCCESS()) {
  34022. ca = NULL;
  34023. }
  34024. BIO_free(bio);
  34025. X509_free(cert);
  34026. X509_free(ca);
  34027. X509_free(x509);
  34028. sk_X509_pop_free(chain, X509_free);
  34029. sk_X509_pop_free(chain2, X509_free);
  34030. SSL_CTX_free(ctx);
  34031. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34032. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34033. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  34034. return EXPECT_RESULT();
  34035. }
  34036. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  34037. static int test_wolfSSL_ERR_peek_last_error_line(void)
  34038. {
  34039. EXPECT_DECLS;
  34040. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34041. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  34042. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  34043. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  34044. callback_functions client_cb;
  34045. callback_functions server_cb;
  34046. int line = 0;
  34047. int flag = ERR_TXT_STRING;
  34048. const char* file = NULL;
  34049. const char* data = NULL;
  34050. /* create a failed connection and inspect the error */
  34051. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  34052. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  34053. client_cb.method = wolfTLSv1_1_client_method;
  34054. server_cb.method = wolfTLSv1_2_server_method;
  34055. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  34056. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  34057. ExpectNotNull(data);
  34058. /* check clearing error state */
  34059. ERR_remove_state(0);
  34060. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  34061. ERR_peek_last_error_line(NULL, &line);
  34062. ExpectIntEQ(line, 0);
  34063. ERR_peek_last_error_line(&file, NULL);
  34064. ExpectNull(file);
  34065. /* retry connection to fill error queue */
  34066. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  34067. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  34068. client_cb.method = wolfTLSv1_1_client_method;
  34069. server_cb.method = wolfTLSv1_2_server_method;
  34070. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  34071. /* check that error code was stored */
  34072. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  34073. ERR_peek_last_error_line(NULL, &line);
  34074. ExpectIntNE(line, 0);
  34075. ERR_peek_last_error_line(&file, NULL);
  34076. ExpectNotNull(file);
  34077. fprintf(stderr, "\nTesting error print out\n");
  34078. ERR_print_errors_fp(stderr);
  34079. fprintf(stderr, "Done testing print out\n\n");
  34080. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34081. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  34082. return EXPECT_RESULT();
  34083. }
  34084. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  34085. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34086. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34087. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  34088. {
  34089. (void) ok;
  34090. (void) ctx;
  34091. fprintf(stderr, "ENTER verify_cb\n");
  34092. return SSL_SUCCESS;
  34093. }
  34094. #endif
  34095. static int test_wolfSSL_X509_Name_canon(void)
  34096. {
  34097. EXPECT_DECLS;
  34098. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34099. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  34100. defined(WOLFSSL_CERT_GEN) && \
  34101. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  34102. const long ex_hash1 = 0x0fdb2da4;
  34103. const long ex_hash2 = 0x9f3e8c9e;
  34104. X509_NAME *name = NULL;
  34105. X509 *x509 = NULL;
  34106. XFILE file = XBADFILE;
  34107. unsigned long hash = 0;
  34108. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  34109. byte *pbuf = NULL;
  34110. word32 len = 0;
  34111. (void) ex_hash2;
  34112. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  34113. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  34114. ExpectNotNull(name = X509_get_issuer_name(x509));
  34115. /* When output buffer is NULL, should return necessary output buffer
  34116. * length.*/
  34117. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  34118. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  34119. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  34120. hash = (((unsigned long)digest[3] << 24) |
  34121. ((unsigned long)digest[2] << 16) |
  34122. ((unsigned long)digest[1] << 8) |
  34123. ((unsigned long)digest[0]));
  34124. ExpectIntEQ(hash, ex_hash1);
  34125. if (file != XBADFILE) {
  34126. XFCLOSE(file);
  34127. file = XBADFILE;
  34128. }
  34129. X509_free(x509);
  34130. x509 = NULL;
  34131. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  34132. pbuf = NULL;
  34133. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  34134. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  34135. ExpectNotNull(name = X509_get_issuer_name(x509));
  34136. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  34137. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  34138. hash = (((unsigned long)digest[3] << 24) |
  34139. ((unsigned long)digest[2] << 16) |
  34140. ((unsigned long)digest[1] << 8) |
  34141. ((unsigned long)digest[0]));
  34142. ExpectIntEQ(hash, ex_hash2);
  34143. if (file != XBADFILE)
  34144. XFCLOSE(file);
  34145. X509_free(x509);
  34146. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  34147. #endif
  34148. return EXPECT_RESULT();
  34149. }
  34150. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  34151. {
  34152. EXPECT_DECLS;
  34153. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  34154. const int MAX_DIR = 4;
  34155. const char paths[][32] = {
  34156. "./certs/ed25519",
  34157. "./certs/ecc",
  34158. "./certs/crl",
  34159. "./certs/",
  34160. };
  34161. char CertCrl_path[MAX_FILENAME_SZ];
  34162. char *p;
  34163. X509_STORE* str = NULL;
  34164. X509_LOOKUP* lookup = NULL;
  34165. WOLFSSL_STACK* sk = NULL;
  34166. int len, total_len, i;
  34167. (void)sk;
  34168. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  34169. /* illegal string */
  34170. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34171. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34172. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  34173. SSL_FILETYPE_PEM,NULL), 0);
  34174. /* free store */
  34175. X509_STORE_free(str);
  34176. str = NULL;
  34177. /* short folder string */
  34178. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34179. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34180. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  34181. SSL_FILETYPE_PEM,NULL), 1);
  34182. #if defined(WOLFSSL_INT_H)
  34183. /* only available when including internal.h */
  34184. ExpectNotNull(sk = lookup->dirs->dir_entry);
  34185. #endif
  34186. /* free store */
  34187. X509_STORE_free(str);
  34188. str = NULL;
  34189. /* typical function check */
  34190. p = &CertCrl_path[0];
  34191. total_len = 0;
  34192. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  34193. len = (int)XSTRLEN((const char*)&paths[i]);
  34194. total_len += len;
  34195. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  34196. p += len;
  34197. if (i != 0) *(p++) = SEPARATOR_CHAR;
  34198. }
  34199. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34200. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34201. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  34202. SSL_FILETYPE_PEM,NULL), 1);
  34203. #if defined(WOLFSSL_INT_H)
  34204. /* only available when including internal.h */
  34205. ExpectNotNull(sk = lookup->dirs->dir_entry);
  34206. #endif
  34207. X509_STORE_free(str);
  34208. #endif
  34209. return EXPECT_RESULT();
  34210. }
  34211. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  34212. {
  34213. EXPECT_DECLS;
  34214. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34215. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  34216. defined(WOLFSSL_SIGNER_DER_CERT)
  34217. X509_STORE_CTX* ctx = NULL;
  34218. X509_STORE* str = NULL;
  34219. X509_LOOKUP* lookup = NULL;
  34220. X509* cert1 = NULL;
  34221. X509* x509Ca = NULL;
  34222. X509* x509Svr = NULL;
  34223. X509* issuer = NULL;
  34224. WOLFSSL_STACK* sk = NULL;
  34225. X509_NAME* caName = NULL;
  34226. X509_NAME* issuerName = NULL;
  34227. XFILE file1 = XBADFILE;
  34228. int i;
  34229. int cert_count = 0;
  34230. int cmp;
  34231. char der[] = "certs/ca-cert.der";
  34232. #ifdef HAVE_CRL
  34233. char pem[][100] = {
  34234. "./certs/crl/crl.pem",
  34235. "./certs/crl/crl2.pem",
  34236. "./certs/crl/caEccCrl.pem",
  34237. "./certs/crl/eccCliCRL.pem",
  34238. "./certs/crl/eccSrvCRL.pem",
  34239. ""
  34240. };
  34241. #endif
  34242. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  34243. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  34244. if (file1 != XBADFILE)
  34245. XFCLOSE(file1);
  34246. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34247. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34248. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34249. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  34250. SSL_FILETYPE_PEM,NULL), 1);
  34251. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  34252. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  34253. /* check if CA cert is loaded into the store */
  34254. for (i = 0; i < cert_count; i++) {
  34255. x509Ca = sk_X509_value(sk, i);
  34256. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  34257. }
  34258. ExpectNotNull((x509Svr =
  34259. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34260. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  34261. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  34262. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  34263. ExpectNotNull(issuer);
  34264. caName = X509_get_subject_name(x509Ca);
  34265. ExpectNotNull(caName);
  34266. issuerName = X509_get_subject_name(issuer);
  34267. ExpectNotNull(issuerName);
  34268. cmp = X509_NAME_cmp(caName, issuerName);
  34269. ExpectIntEQ(cmp, 0);
  34270. /* load der format */
  34271. X509_free(issuer);
  34272. issuer = NULL;
  34273. X509_STORE_CTX_free(ctx);
  34274. ctx = NULL;
  34275. X509_STORE_free(str);
  34276. str = NULL;
  34277. sk_X509_pop_free(sk, NULL);
  34278. sk = NULL;
  34279. X509_free(x509Svr);
  34280. x509Svr = NULL;
  34281. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34282. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34283. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  34284. SSL_FILETYPE_ASN1,NULL), 1);
  34285. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  34286. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  34287. /* check if CA cert is loaded into the store */
  34288. for (i = 0; i < cert_count; i++) {
  34289. x509Ca = sk_X509_value(sk, i);
  34290. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  34291. }
  34292. X509_STORE_free(str);
  34293. str = NULL;
  34294. sk_X509_pop_free(sk, NULL);
  34295. sk = NULL;
  34296. X509_free(cert1);
  34297. cert1 = NULL;
  34298. #ifdef HAVE_CRL
  34299. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  34300. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  34301. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  34302. SSL_FILETYPE_PEM,NULL), 1);
  34303. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  34304. "certs/server-revoked-cert.pem",
  34305. SSL_FILETYPE_PEM,NULL), 1);
  34306. if (str) {
  34307. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  34308. WOLFSSL_FILETYPE_PEM), 1);
  34309. /* since store hasn't yet known the revoked cert*/
  34310. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  34311. "certs/server-revoked-cert.pem",
  34312. WOLFSSL_FILETYPE_PEM), 1);
  34313. }
  34314. for (i = 0; pem[i][0] != '\0'; i++)
  34315. {
  34316. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  34317. SSL_FILETYPE_PEM, NULL), 1);
  34318. }
  34319. if (str) {
  34320. /* since store knows crl list */
  34321. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  34322. "certs/server-revoked-cert.pem",
  34323. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  34324. }
  34325. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  34326. X509_STORE_free(str);
  34327. #endif
  34328. #endif
  34329. return EXPECT_RESULT();
  34330. }
  34331. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  34332. {
  34333. int res = TEST_SKIPPED;
  34334. #if defined(OPENSSL_EXTRA)
  34335. X509_STORE_CTX_cleanup(NULL);
  34336. X509_STORE_CTX_trusted_stack(NULL, NULL);
  34337. res = TEST_SUCCESS;
  34338. #endif
  34339. return res;
  34340. }
  34341. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  34342. {
  34343. EXPECT_DECLS;
  34344. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34345. X509_STORE_CTX* ctx = NULL;
  34346. X509_STORE* str = NULL;
  34347. X509* x509Ca = NULL;
  34348. X509* x509Svr = NULL;
  34349. X509* issuer = NULL;
  34350. X509_NAME* caName = NULL;
  34351. X509_NAME* issuerName = NULL;
  34352. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34353. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34354. ExpectNotNull((x509Ca =
  34355. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  34356. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  34357. ExpectNotNull((x509Svr =
  34358. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34359. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  34360. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  34361. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  34362. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  34363. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  34364. #ifdef WOLFSSL_SIGNER_DER_CERT
  34365. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  34366. #endif
  34367. X509_free(issuer);
  34368. X509_STORE_CTX_free(ctx);
  34369. X509_free(x509Svr);
  34370. X509_STORE_free(str);
  34371. X509_free(x509Ca);
  34372. #endif
  34373. return EXPECT_RESULT();
  34374. }
  34375. static int test_wolfSSL_PKCS7_certs(void)
  34376. {
  34377. EXPECT_DECLS;
  34378. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  34379. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  34380. STACK_OF(X509)* sk = NULL;
  34381. STACK_OF(X509_INFO)* info_sk = NULL;
  34382. PKCS7 *p7 = NULL;
  34383. BIO* bio = NULL;
  34384. const byte* p = NULL;
  34385. int buflen = 0;
  34386. int i;
  34387. /* Test twice. Once with d2i and once without to test
  34388. * that everything is free'd correctly. */
  34389. for (i = 0; i < 2; i++) {
  34390. ExpectNotNull(p7 = PKCS7_new());
  34391. if (p7 != NULL) {
  34392. p7->version = 1;
  34393. #ifdef NO_SHA
  34394. p7->hashOID = SHA256h;
  34395. #else
  34396. p7->hashOID = SHAh;
  34397. #endif
  34398. }
  34399. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  34400. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  34401. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  34402. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  34403. ExpectNotNull(sk = sk_X509_new_null());
  34404. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  34405. X509_INFO* info = NULL;
  34406. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  34407. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  34408. if (EXPECT_SUCCESS() && (info != NULL)) {
  34409. info->x509 = NULL;
  34410. }
  34411. X509_INFO_free(info);
  34412. }
  34413. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  34414. info_sk = NULL;
  34415. BIO_free(bio);
  34416. bio = NULL;
  34417. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  34418. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  34419. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  34420. sk_X509_pop_free(sk, X509_free);
  34421. }
  34422. sk = NULL;
  34423. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  34424. if (i == 0) {
  34425. PKCS7_free(p7);
  34426. p7 = NULL;
  34427. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  34428. if (p7 != NULL) {
  34429. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  34430. * them */
  34431. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  34432. }
  34433. /* PKCS7_free free's the certs */
  34434. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  34435. }
  34436. BIO_free(bio);
  34437. bio = NULL;
  34438. PKCS7_free(p7);
  34439. p7 = NULL;
  34440. }
  34441. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  34442. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  34443. return EXPECT_RESULT();
  34444. }
  34445. static int test_wolfSSL_X509_STORE_CTX(void)
  34446. {
  34447. EXPECT_DECLS;
  34448. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34449. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34450. X509_STORE_CTX* ctx = NULL;
  34451. X509_STORE* str = NULL;
  34452. X509* x509 = NULL;
  34453. #ifdef OPENSSL_ALL
  34454. X509* x5092 = NULL;
  34455. STACK_OF(X509) *sk = NULL;
  34456. STACK_OF(X509) *sk2 = NULL;
  34457. STACK_OF(X509) *sk3 = NULL;
  34458. #endif
  34459. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34460. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  34461. ExpectNotNull((x509 =
  34462. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  34463. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  34464. #ifdef OPENSSL_ALL
  34465. /* sk_X509_new only in OPENSSL_ALL */
  34466. sk = sk_X509_new_null();
  34467. ExpectNotNull(sk);
  34468. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  34469. #else
  34470. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  34471. #endif
  34472. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  34473. X509_STORE_CTX_set_error(ctx, -5);
  34474. X509_STORE_CTX_set_error(NULL, -5);
  34475. X509_STORE_CTX_free(ctx);
  34476. ctx = NULL;
  34477. #ifdef OPENSSL_ALL
  34478. sk_X509_pop_free(sk, NULL);
  34479. sk = NULL;
  34480. #endif
  34481. X509_STORE_free(str);
  34482. str = NULL;
  34483. X509_free(x509);
  34484. x509 = NULL;
  34485. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34486. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  34487. X509_STORE_CTX_free(ctx);
  34488. ctx = NULL;
  34489. #ifdef OPENSSL_ALL
  34490. /* test X509_STORE_CTX_get(1)_chain */
  34491. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  34492. SSL_FILETYPE_PEM)));
  34493. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  34494. SSL_FILETYPE_PEM)));
  34495. ExpectNotNull((sk = sk_X509_new_null()));
  34496. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  34497. if (EXPECT_FAIL()) {
  34498. X509_free(x509);
  34499. x509 = NULL;
  34500. }
  34501. ExpectNotNull((str = X509_STORE_new()));
  34502. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  34503. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  34504. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  34505. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  34506. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  34507. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  34508. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  34509. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  34510. X509_STORE_CTX_free(ctx);
  34511. ctx = NULL;
  34512. X509_STORE_free(str);
  34513. str = NULL;
  34514. /* CTX certs not freed yet */
  34515. X509_free(x5092);
  34516. x5092 = NULL;
  34517. sk_X509_pop_free(sk, NULL);
  34518. sk = NULL;
  34519. /* sk3 is dup so free here */
  34520. sk_X509_pop_free(sk3, NULL);
  34521. sk3 = NULL;
  34522. #endif
  34523. /* test X509_STORE_CTX_get/set_ex_data */
  34524. {
  34525. int i = 0, tmpData = 5;
  34526. void* tmpDataRet;
  34527. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34528. #ifdef HAVE_EX_DATA
  34529. for (i = 0; i < MAX_EX_DATA; i++) {
  34530. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  34531. WOLFSSL_SUCCESS);
  34532. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  34533. ExpectNotNull(tmpDataRet);
  34534. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  34535. }
  34536. #else
  34537. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  34538. WOLFSSL_FAILURE);
  34539. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  34540. ExpectNull(tmpDataRet);
  34541. #endif
  34542. X509_STORE_CTX_free(ctx);
  34543. ctx = NULL;
  34544. }
  34545. /* test X509_STORE_get/set_ex_data */
  34546. {
  34547. int i = 0, tmpData = 99;
  34548. void* tmpDataRet;
  34549. ExpectNotNull(str = X509_STORE_new());
  34550. #ifdef HAVE_EX_DATA
  34551. for (i = 0; i < MAX_EX_DATA; i++) {
  34552. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  34553. WOLFSSL_SUCCESS);
  34554. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  34555. ExpectNotNull(tmpDataRet);
  34556. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  34557. }
  34558. #else
  34559. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  34560. WOLFSSL_FAILURE);
  34561. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  34562. ExpectNull(tmpDataRet);
  34563. #endif
  34564. X509_STORE_free(str);
  34565. str = NULL;
  34566. }
  34567. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34568. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34569. return EXPECT_RESULT();
  34570. }
  34571. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34572. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  34573. STACK_OF(X509)* chain)
  34574. {
  34575. EXPECT_DECLS;
  34576. XFILE fp = XBADFILE;
  34577. X509* cert = NULL;
  34578. ExpectTrue((fp = XFOPEN(filename, "rb"))
  34579. != XBADFILE);
  34580. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  34581. if (fp != XBADFILE) {
  34582. XFCLOSE(fp);
  34583. fp = XBADFILE;
  34584. }
  34585. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  34586. if (EXPECT_FAIL())
  34587. X509_free(cert);
  34588. return EXPECT_RESULT();
  34589. }
  34590. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  34591. int err, int loadCA)
  34592. {
  34593. EXPECT_DECLS;
  34594. X509_STORE_CTX* ctx = NULL;
  34595. X509_STORE* str = NULL;
  34596. XFILE fp = XBADFILE;
  34597. X509* cert = NULL;
  34598. STACK_OF(X509)* untrusted = NULL;
  34599. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  34600. != XBADFILE);
  34601. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  34602. if (fp != XBADFILE) {
  34603. XFCLOSE(fp);
  34604. fp = XBADFILE;
  34605. }
  34606. ExpectNotNull(str = X509_STORE_new());
  34607. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34608. ExpectNotNull(untrusted = sk_X509_new_null());
  34609. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  34610. if (loadCA) {
  34611. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  34612. 1);
  34613. }
  34614. for (; *filenames; filenames++) {
  34615. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  34616. untrusted), TEST_SUCCESS);
  34617. }
  34618. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  34619. ExpectIntEQ(X509_verify_cert(ctx), ret);
  34620. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  34621. X509_free(cert);
  34622. X509_STORE_free(str);
  34623. X509_STORE_CTX_free(ctx);
  34624. sk_X509_pop_free(untrusted, NULL);
  34625. return EXPECT_RESULT();
  34626. }
  34627. #endif
  34628. static int test_X509_STORE_untrusted(void)
  34629. {
  34630. EXPECT_DECLS;
  34631. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34632. const char* untrusted1[] = {
  34633. "./certs/intermediate/ca-int2-cert.pem",
  34634. NULL
  34635. };
  34636. const char* untrusted2[] = {
  34637. "./certs/intermediate/ca-int-cert.pem",
  34638. "./certs/intermediate/ca-int2-cert.pem",
  34639. NULL
  34640. };
  34641. const char* untrusted3[] = {
  34642. "./certs/intermediate/ca-int-cert.pem",
  34643. "./certs/intermediate/ca-int2-cert.pem",
  34644. "./certs/ca-cert.pem",
  34645. NULL
  34646. };
  34647. /* Adding unrelated certs that should be ignored */
  34648. const char* untrusted4[] = {
  34649. "./certs/client-ca.pem",
  34650. "./certs/intermediate/ca-int-cert.pem",
  34651. "./certs/server-cert.pem",
  34652. "./certs/intermediate/ca-int2-cert.pem",
  34653. NULL
  34654. };
  34655. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  34656. * to loaded CA. */
  34657. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  34658. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  34659. /* Succeeds because path to loaded CA is available. */
  34660. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  34661. TEST_SUCCESS);
  34662. /* Fails because root CA is in the untrusted stack */
  34663. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  34664. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  34665. /* Succeeds because path to loaded CA is available. */
  34666. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  34667. TEST_SUCCESS);
  34668. #endif
  34669. return EXPECT_RESULT();
  34670. }
  34671. static int test_wolfSSL_X509_STORE_set_flags(void)
  34672. {
  34673. EXPECT_DECLS;
  34674. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34675. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  34676. X509_STORE* store = NULL;
  34677. X509* x509 = NULL;
  34678. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  34679. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  34680. WOLFSSL_FILETYPE_PEM)));
  34681. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  34682. #ifdef HAVE_CRL
  34683. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  34684. WOLFSSL_SUCCESS);
  34685. #else
  34686. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  34687. NOT_COMPILED_IN);
  34688. #endif
  34689. wolfSSL_X509_free(x509);
  34690. wolfSSL_X509_STORE_free(store);
  34691. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  34692. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34693. return EXPECT_RESULT();
  34694. }
  34695. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  34696. {
  34697. EXPECT_DECLS;
  34698. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  34699. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  34700. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  34701. WOLFSSL_X509_STORE* store = NULL;
  34702. WOLFSSL_X509_LOOKUP* lookup = NULL;
  34703. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  34704. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  34705. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  34706. X509_FILETYPE_PEM), 1);
  34707. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  34708. X509_FILETYPE_PEM), 1);
  34709. if (store != NULL) {
  34710. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  34711. WOLFSSL_FILETYPE_PEM), 1);
  34712. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  34713. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  34714. }
  34715. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  34716. X509_FILETYPE_PEM), 1);
  34717. if (store != NULL) {
  34718. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  34719. WOLFSSL_FILETYPE_PEM), 1);
  34720. }
  34721. wolfSSL_X509_STORE_free(store);
  34722. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  34723. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  34724. return EXPECT_RESULT();
  34725. }
  34726. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  34727. {
  34728. EXPECT_DECLS;
  34729. #if defined(OPENSSL_EXTRA)
  34730. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  34731. time_t c_time;
  34732. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  34733. c_time = 365*24*60*60;
  34734. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  34735. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  34736. WOLFSSL_USE_CHECK_TIME);
  34737. ExpectTrue(ctx->param->check_time == c_time);
  34738. wolfSSL_X509_STORE_CTX_free(ctx);
  34739. #endif /* OPENSSL_EXTRA */
  34740. return EXPECT_RESULT();
  34741. }
  34742. static int test_wolfSSL_CTX_get0_set1_param(void)
  34743. {
  34744. EXPECT_DECLS;
  34745. #if defined(OPENSSL_EXTRA)
  34746. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34747. SSL_CTX* ctx = NULL;
  34748. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34749. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  34750. char testIPv4[] = "127.0.0.1";
  34751. char testhostName[] = "foo.hoge.com";
  34752. #ifndef NO_WOLFSSL_SERVER
  34753. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34754. #else
  34755. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34756. #endif
  34757. ExpectNull(SSL_CTX_get0_param(NULL));
  34758. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  34759. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  34760. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  34761. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  34762. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  34763. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  34764. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  34765. WOLFSSL_SUCCESS);
  34766. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  34767. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  34768. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  34769. (int)XSTRLEN(testhostName)));
  34770. ExpectIntEQ(0x01, pParam->hostFlags);
  34771. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  34772. /* test for incorrect parameter */
  34773. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  34774. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  34775. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  34776. SSL_CTX_free(ctx);
  34777. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  34778. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34779. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34780. return EXPECT_RESULT();
  34781. }
  34782. static int test_wolfSSL_get0_param(void)
  34783. {
  34784. EXPECT_DECLS;
  34785. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34786. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34787. SSL_CTX* ctx = NULL;
  34788. SSL* ssl = NULL;
  34789. #ifndef NO_WOLFSSL_SERVER
  34790. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34791. #else
  34792. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34793. #endif
  34794. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34795. SSL_FILETYPE_PEM));
  34796. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34797. ExpectNotNull(ssl = SSL_new(ctx));
  34798. ExpectNotNull(SSL_get0_param(ssl));
  34799. SSL_free(ssl);
  34800. SSL_CTX_free(ctx);
  34801. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34802. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  34803. return EXPECT_RESULT();
  34804. }
  34805. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  34806. {
  34807. EXPECT_DECLS;
  34808. #if defined(OPENSSL_EXTRA)
  34809. const char host[] = "www.example.com";
  34810. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34811. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  34812. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  34813. if (pParam != NULL) {
  34814. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34815. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  34816. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34817. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  34818. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34819. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34820. }
  34821. #endif /* OPENSSL_EXTRA */
  34822. return EXPECT_RESULT();
  34823. }
  34824. static int test_wolfSSL_set1_host(void)
  34825. {
  34826. EXPECT_DECLS;
  34827. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34828. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34829. const char host[] = "www.test_wolfSSL_set1_host.com";
  34830. const char emptyStr[] = "";
  34831. SSL_CTX* ctx = NULL;
  34832. SSL* ssl = NULL;
  34833. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  34834. #ifndef NO_WOLFSSL_SERVER
  34835. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34836. #else
  34837. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34838. #endif
  34839. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34840. SSL_FILETYPE_PEM));
  34841. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34842. ExpectNotNull(ssl = SSL_new(ctx));
  34843. pParam = SSL_get0_param(ssl);
  34844. /* we should get back host string */
  34845. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34846. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34847. /* we should get back empty string */
  34848. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  34849. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34850. /* we should get back host string */
  34851. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  34852. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  34853. /* we should get back empty string */
  34854. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  34855. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  34856. SSL_free(ssl);
  34857. SSL_CTX_free(ctx);
  34858. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34859. #endif /* OPENSSL_EXTRA */
  34860. return EXPECT_RESULT();
  34861. }
  34862. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  34863. {
  34864. EXPECT_DECLS;
  34865. #if defined(OPENSSL_EXTRA)
  34866. unsigned char buf[16] = {0};
  34867. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  34868. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  34869. /* test 127.0.0.1 */
  34870. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  34871. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  34872. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  34873. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  34874. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34875. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  34876. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  34877. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  34878. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34879. ExpectIntEQ(XSTRNCMP(param->ipasc,
  34880. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  34881. /* test 2001:db8:: */
  34882. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34883. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34884. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34885. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  34886. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34887. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  34888. /* test ::1234:5678 */
  34889. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  34890. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34891. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34892. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34893. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34894. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  34895. /* test 2001:db8::1234:5678 */
  34896. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34897. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  34898. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  34899. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  34900. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34901. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  34902. sizeof(param->ipasc)), 0);
  34903. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  34904. /* 2001:db8:1::ab9:c0a8:102 */
  34905. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  34906. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  34907. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  34908. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  34909. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  34910. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  34911. sizeof(param->ipasc)), 0);
  34912. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  34913. #endif /* OPENSSL_EXTRA */
  34914. return EXPECT_RESULT();
  34915. }
  34916. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  34917. {
  34918. EXPECT_DECLS;
  34919. #if defined(OPENSSL_EXTRA)
  34920. X509_STORE* store = NULL;
  34921. X509_STORE_CTX* ctx = NULL;
  34922. X509_STORE_CTX* ctx_no_init = NULL;
  34923. ExpectNotNull((store = X509_STORE_new()));
  34924. ExpectNotNull(ctx = X509_STORE_CTX_new());
  34925. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  34926. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  34927. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  34928. /* should return NULL if ctx has not bee initialized */
  34929. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  34930. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  34931. wolfSSL_X509_STORE_CTX_free(ctx);
  34932. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  34933. X509_STORE_free(store);
  34934. #endif /* OPENSSL_EXTRA */
  34935. return EXPECT_RESULT();
  34936. }
  34937. static int test_wolfSSL_CTX_set_client_CA_list(void)
  34938. {
  34939. EXPECT_DECLS;
  34940. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  34941. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  34942. WOLFSSL_CTX* ctx = NULL;
  34943. WOLFSSL* ssl = NULL;
  34944. X509_NAME* name = NULL;
  34945. STACK_OF(X509_NAME)* names = NULL;
  34946. STACK_OF(X509_NAME)* ca_list = NULL;
  34947. int names_len = 0;
  34948. int i;
  34949. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  34950. /* Send two X501 names in cert request */
  34951. names = SSL_load_client_CA_file(cliCertFile);
  34952. ExpectNotNull(names);
  34953. ca_list = SSL_load_client_CA_file(caCertFile);
  34954. ExpectNotNull(ca_list);
  34955. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  34956. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  34957. if (EXPECT_FAIL()) {
  34958. wolfSSL_X509_NAME_free(name);
  34959. name = NULL;
  34960. }
  34961. SSL_CTX_set_client_CA_list(ctx, names);
  34962. /* This should only free the stack structure */
  34963. sk_X509_NAME_free(ca_list);
  34964. ca_list = NULL;
  34965. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  34966. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  34967. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  34968. for (i = 0; i < names_len; i++) {
  34969. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  34970. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  34971. }
  34972. /* Needed to be able to create ssl object */
  34973. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34974. SSL_FILETYPE_PEM));
  34975. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  34976. ExpectNotNull(ssl = wolfSSL_new(ctx));
  34977. /* load again as old names are responsibility of ctx to free*/
  34978. names = SSL_load_client_CA_file(cliCertFile);
  34979. ExpectNotNull(names);
  34980. SSL_set_client_CA_list(ssl, names);
  34981. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  34982. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  34983. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  34984. for (i = 0; i < names_len; i++) {
  34985. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  34986. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  34987. }
  34988. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  34989. {
  34990. tcp_ready ready;
  34991. func_args server_args;
  34992. callback_functions server_cb;
  34993. THREAD_TYPE serverThread;
  34994. WOLFSSL* ssl_client = NULL;
  34995. WOLFSSL_CTX* ctx_client = NULL;
  34996. SOCKET_T sockfd = 0;
  34997. /* wolfSSL_get_client_CA_list() with handshake */
  34998. StartTCP();
  34999. InitTcpReady(&ready);
  35000. XMEMSET(&server_args, 0, sizeof(func_args));
  35001. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  35002. server_args.signal = &ready;
  35003. server_args.callbacks = &server_cb;
  35004. /* we are responsible for free'ing WOLFSSL_CTX */
  35005. server_cb.ctx = ctx;
  35006. server_cb.isSharedCtx = 1;
  35007. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  35008. cliCertFile, 0));
  35009. start_thread(test_server_nofail, &server_args, &serverThread);
  35010. wait_tcp_ready(&server_args);
  35011. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35012. ExpectNotNull(ctx_client =
  35013. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35014. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  35015. ctx_client, caCertFile, 0));
  35016. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  35017. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  35018. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  35019. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  35020. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  35021. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  35022. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  35023. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  35024. /* We are expecting two cert names to be sent */
  35025. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  35026. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  35027. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  35028. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  35029. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  35030. }
  35031. wolfSSL_shutdown(ssl_client);
  35032. wolfSSL_free(ssl_client);
  35033. wolfSSL_CTX_free(ctx_client);
  35034. CloseSocket(sockfd);
  35035. join_thread(serverThread);
  35036. FreeTcpReady(&ready);
  35037. }
  35038. #endif
  35039. wolfSSL_free(ssl);
  35040. wolfSSL_CTX_free(ctx);
  35041. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  35042. * !NO_BIO */
  35043. return EXPECT_RESULT();
  35044. }
  35045. static int test_wolfSSL_CTX_add_client_CA(void)
  35046. {
  35047. EXPECT_DECLS;
  35048. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  35049. !defined(NO_WOLFSSL_CLIENT)
  35050. WOLFSSL_CTX* ctx = NULL;
  35051. WOLFSSL_X509* x509 = NULL;
  35052. WOLFSSL_X509* x509_a = NULL;
  35053. STACK_OF(X509_NAME)* ca_list = NULL;
  35054. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35055. /* Add client cert */
  35056. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  35057. SSL_FILETYPE_PEM));
  35058. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  35059. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  35060. /* Add another client cert */
  35061. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  35062. SSL_FILETYPE_PEM));
  35063. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  35064. /* test for incorrect parameter */
  35065. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  35066. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  35067. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  35068. X509_free(x509);
  35069. X509_free(x509_a);
  35070. SSL_CTX_free(ctx);
  35071. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  35072. return EXPECT_RESULT();
  35073. }
  35074. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  35075. defined(HAVE_IO_TESTS_DEPENDENCIES)
  35076. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  35077. {
  35078. callback_functions* callbacks = ((func_args*)args)->callbacks;
  35079. WOLFSSL_CTX* ctx = callbacks->ctx;
  35080. WOLFSSL* ssl = NULL;
  35081. SOCKET_T sfd = 0;
  35082. SOCKET_T cfd = 0;
  35083. word16 port;
  35084. char input[1024];
  35085. int idx;
  35086. int ret, err = 0;
  35087. const char* privateName = "ech-private-name.com";
  35088. int privateNameLen = (int)XSTRLEN(privateName);
  35089. ((func_args*)args)->return_code = TEST_FAIL;
  35090. port = ((func_args*)args)->signal->port;
  35091. AssertIntEQ(WOLFSSL_SUCCESS,
  35092. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  35093. AssertIntEQ(WOLFSSL_SUCCESS,
  35094. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35095. WOLFSSL_FILETYPE_PEM));
  35096. AssertIntEQ(WOLFSSL_SUCCESS,
  35097. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35098. WOLFSSL_FILETYPE_PEM));
  35099. if (callbacks->ctx_ready)
  35100. callbacks->ctx_ready(ctx);
  35101. ssl = wolfSSL_new(ctx);
  35102. /* set the sni for the server */
  35103. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  35104. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  35105. CloseSocket(sfd);
  35106. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  35107. if (callbacks->ssl_ready)
  35108. callbacks->ssl_ready(ssl);
  35109. do {
  35110. err = 0; /* Reset error */
  35111. ret = wolfSSL_accept(ssl);
  35112. if (ret != WOLFSSL_SUCCESS) {
  35113. err = wolfSSL_get_error(ssl, 0);
  35114. }
  35115. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  35116. if (ret != WOLFSSL_SUCCESS) {
  35117. char buff[WOLFSSL_MAX_ERROR_SZ];
  35118. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  35119. }
  35120. else {
  35121. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  35122. input[idx] = 0;
  35123. fprintf(stderr, "Client message: %s\n", input);
  35124. }
  35125. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  35126. privateNameLen));
  35127. ((func_args*)args)->return_code = TEST_SUCCESS;
  35128. }
  35129. if (callbacks->on_result)
  35130. callbacks->on_result(ssl);
  35131. wolfSSL_shutdown(ssl);
  35132. wolfSSL_free(ssl);
  35133. wolfSSL_CTX_free(ctx);
  35134. CloseSocket(cfd);
  35135. #ifdef FP_ECC
  35136. wc_ecc_fp_free();
  35137. #endif
  35138. WOLFSSL_RETURN_FROM_THREAD(0);
  35139. }
  35140. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  35141. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35142. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  35143. {
  35144. AssertNotNull(ssl);
  35145. AssertNotNull(line);
  35146. XFILE fp;
  35147. const byte lf = '\n';
  35148. fp = XFOPEN("./MyKeyLog.txt", "a");
  35149. XFWRITE( line, 1, strlen(line),fp);
  35150. XFWRITE( (void*)&lf,1,1,fp);
  35151. XFFLUSH(fp);
  35152. XFCLOSE(fp);
  35153. }
  35154. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  35155. static int test_wolfSSL_CTX_set_keylog_callback(void)
  35156. {
  35157. EXPECT_DECLS;
  35158. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  35159. !defined(NO_WOLFSSL_CLIENT)
  35160. SSL_CTX* ctx = NULL;
  35161. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35162. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  35163. SSL_CTX_free(ctx);
  35164. SSL_CTX_set_keylog_callback(NULL, NULL);
  35165. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  35166. return EXPECT_RESULT();
  35167. }
  35168. static int test_wolfSSL_CTX_get_keylog_callback(void)
  35169. {
  35170. EXPECT_DECLS;
  35171. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  35172. !defined(NO_WOLFSSL_CLIENT)
  35173. SSL_CTX* ctx = NULL;
  35174. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35175. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  35176. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  35177. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  35178. SSL_CTX_set_keylog_callback(ctx, NULL );
  35179. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  35180. SSL_CTX_free(ctx);
  35181. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  35182. return EXPECT_RESULT();
  35183. }
  35184. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35185. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  35186. {
  35187. /* set keylog callback */
  35188. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  35189. return TEST_SUCCESS;
  35190. }
  35191. #endif
  35192. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  35193. {
  35194. EXPECT_DECLS;
  35195. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  35196. /* This test is intended for checking whether keylog callback is called
  35197. * in client during TLS handshake between the client and a server.
  35198. */
  35199. test_ssl_cbf server_cbf;
  35200. test_ssl_cbf client_cbf;
  35201. XFILE fp = XBADFILE;
  35202. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  35203. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  35204. server_cbf.method = wolfTLSv1_2_server_method;
  35205. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  35206. /* clean up keylog file */
  35207. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  35208. if (fp != XBADFILE) {
  35209. XFFLUSH(fp);
  35210. XFCLOSE(fp);
  35211. fp = XBADFILE;
  35212. }
  35213. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35214. &server_cbf, NULL), TEST_SUCCESS);
  35215. XSLEEP_MS(100);
  35216. /* check if the keylog file exists */
  35217. char buff[300] = {0};
  35218. int found = 0;
  35219. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  35220. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  35221. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  35222. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  35223. found = 1;
  35224. break;
  35225. }
  35226. }
  35227. if (fp != XBADFILE) {
  35228. XFCLOSE(fp);
  35229. }
  35230. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  35231. ExpectIntEQ(found, 1);
  35232. /* clean up */
  35233. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  35234. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  35235. return EXPECT_RESULT();
  35236. }
  35237. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  35238. defined(HAVE_SECRET_CALLBACK)
  35239. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  35240. {
  35241. /* set keylog callback */
  35242. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  35243. return TEST_SUCCESS;
  35244. }
  35245. #endif
  35246. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  35247. {
  35248. EXPECT_DECLS;
  35249. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  35250. defined(HAVE_SECRET_CALLBACK)
  35251. /* This test is intended for checking whether keylog callback is called
  35252. * in client during TLS handshake between the client and a server.
  35253. */
  35254. test_ssl_cbf server_cbf;
  35255. test_ssl_cbf client_cbf;
  35256. XFILE fp = XBADFILE;
  35257. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  35258. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  35259. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  35260. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  35261. /* clean up keylog file */
  35262. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  35263. if (fp != XBADFILE) {
  35264. XFCLOSE(fp);
  35265. fp = XBADFILE;
  35266. }
  35267. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35268. &server_cbf, NULL), TEST_SUCCESS);
  35269. /* check if the keylog file exists */
  35270. {
  35271. char buff[300] = {0};
  35272. int found[4] = {0};
  35273. int numfnd = 0;
  35274. int i;
  35275. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  35276. while (EXPECT_SUCCESS() &&
  35277. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  35278. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  35279. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  35280. found[0] = 1;
  35281. continue;
  35282. }
  35283. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  35284. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  35285. found[1] = 1;
  35286. continue;
  35287. }
  35288. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  35289. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  35290. found[2] = 1;
  35291. continue;
  35292. }
  35293. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  35294. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  35295. found[3] = 1;
  35296. continue;
  35297. }
  35298. }
  35299. if (fp != XBADFILE)
  35300. XFCLOSE(fp);
  35301. for (i = 0; i < 4; i++) {
  35302. if (found[i] != 0)
  35303. numfnd++;
  35304. }
  35305. ExpectIntEQ(numfnd, 4);
  35306. }
  35307. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  35308. return EXPECT_RESULT();
  35309. }
  35310. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  35311. defined(HAVE_IO_TESTS_DEPENDENCIES)
  35312. static int test_wolfSSL_Tls13_ECH_params(void)
  35313. {
  35314. EXPECT_DECLS;
  35315. #if !defined(NO_WOLFSSL_CLIENT)
  35316. word32 outputLen = 0;
  35317. byte testBuf[72];
  35318. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  35319. WOLFSSL *ssl = wolfSSL_new(ctx);
  35320. ExpectNotNull(ctx);
  35321. ExpectNotNull(ssl);
  35322. /* invalid ctx */
  35323. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  35324. "ech-public-name.com", 0, 0, 0));
  35325. /* invalid public name */
  35326. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  35327. 0, 0));
  35328. /* invalid algorithms */
  35329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  35330. "ech-public-name.com", 1000, 1000, 1000));
  35331. /* invalid ctx */
  35332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  35333. &outputLen));
  35334. /* invalid output len */
  35335. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  35336. /* invalid ssl */
  35337. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  35338. (char*)testBuf, sizeof(testBuf)));
  35339. /* invalid configs64 */
  35340. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  35341. sizeof(testBuf)));
  35342. /* invalid size */
  35343. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  35344. (char*)testBuf, 0));
  35345. /* invalid ssl */
  35346. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  35347. sizeof(testBuf)));
  35348. /* invalid configs */
  35349. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  35350. sizeof(testBuf)));
  35351. /* invalid size */
  35352. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  35353. /* invalid ssl */
  35354. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  35355. /* invalid size */
  35356. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  35357. wolfSSL_free(ssl);
  35358. wolfSSL_CTX_free(ctx);
  35359. #endif /* !NO_WOLFSSL_CLIENT */
  35360. return EXPECT_RESULT();
  35361. }
  35362. static int test_wolfSSL_Tls13_ECH(void)
  35363. {
  35364. EXPECT_DECLS;
  35365. tcp_ready ready;
  35366. func_args client_args;
  35367. func_args server_args;
  35368. THREAD_TYPE serverThread;
  35369. callback_functions server_cbf;
  35370. callback_functions client_cbf;
  35371. SOCKET_T sockfd = 0;
  35372. WOLFSSL_CTX* ctx = NULL;
  35373. WOLFSSL* ssl = NULL;
  35374. const char* publicName = "ech-public-name.com";
  35375. const char* privateName = "ech-private-name.com";
  35376. int privateNameLen = 20;
  35377. char reply[1024];
  35378. int replyLen = 0;
  35379. byte rawEchConfig[128];
  35380. word32 rawEchConfigLen = sizeof(rawEchConfig);
  35381. InitTcpReady(&ready);
  35382. ready.port = 22222;
  35383. XMEMSET(&client_args, 0, sizeof(func_args));
  35384. XMEMSET(&server_args, 0, sizeof(func_args));
  35385. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  35386. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  35387. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  35388. /* create the server context here so we can get the ech config */
  35389. ExpectNotNull(server_cbf.ctx =
  35390. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  35391. /* generate ech config */
  35392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  35393. publicName, 0, 0, 0));
  35394. /* get the config for the client to use */
  35395. ExpectIntEQ(WOLFSSL_SUCCESS,
  35396. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  35397. &rawEchConfigLen));
  35398. server_args.callbacks = &server_cbf;
  35399. server_args.signal = &ready;
  35400. /* start server task */
  35401. start_thread(server_task_ech, &server_args, &serverThread);
  35402. wait_tcp_ready(&server_args);
  35403. /* run as a TLS1.3 client */
  35404. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  35405. ExpectIntEQ(WOLFSSL_SUCCESS,
  35406. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35407. ExpectIntEQ(WOLFSSL_SUCCESS,
  35408. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35409. ExpectIntEQ(WOLFSSL_SUCCESS,
  35410. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35411. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35412. /* get connected the server task */
  35413. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35414. /* set the ech configs for the client */
  35415. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  35416. rawEchConfigLen));
  35417. /* set the sni for the client */
  35418. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  35419. privateName, privateNameLen));
  35420. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35421. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  35422. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  35423. privateNameLen);
  35424. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  35425. /* add th null terminator for string compare */
  35426. reply[replyLen] = 0;
  35427. /* check that the server replied with the private name */
  35428. ExpectStrEQ(privateName, reply);
  35429. wolfSSL_free(ssl);
  35430. wolfSSL_CTX_free(ctx);
  35431. CloseSocket(sockfd);
  35432. join_thread(serverThread);
  35433. FreeTcpReady(&ready);
  35434. return EXPECT_RESULT();
  35435. }
  35436. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  35437. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35438. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35439. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35440. static int post_auth_version_cb(WOLFSSL* ssl)
  35441. {
  35442. EXPECT_DECLS;
  35443. /* do handshake and then test version error */
  35444. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  35445. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  35446. return EXPECT_RESULT();
  35447. }
  35448. static int post_auth_version_client_cb(WOLFSSL* ssl)
  35449. {
  35450. EXPECT_DECLS;
  35451. /* do handshake and then test version error */
  35452. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  35453. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  35454. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  35455. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  35456. /* check was added to error queue */
  35457. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  35458. /* check the string matches expected string */
  35459. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  35460. "WRONG_SSL_VERSION");
  35461. #endif
  35462. return EXPECT_RESULT();
  35463. }
  35464. static int post_auth_cb(WOLFSSL* ssl)
  35465. {
  35466. EXPECT_DECLS;
  35467. WOLFSSL_X509* x509 = NULL;
  35468. /* do handshake and then test version error */
  35469. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  35470. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  35471. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  35472. wolfSSL_X509_free(x509);
  35473. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  35474. return EXPECT_RESULT();
  35475. }
  35476. static int set_post_auth_cb(WOLFSSL* ssl)
  35477. {
  35478. if (!wolfSSL_is_server(ssl)) {
  35479. EXPECT_DECLS;
  35480. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  35481. return EXPECT_RESULT();
  35482. }
  35483. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  35484. return TEST_SUCCESS;
  35485. }
  35486. #endif
  35487. static int test_wolfSSL_Tls13_postauth(void)
  35488. {
  35489. EXPECT_DECLS;
  35490. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35491. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35492. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35493. test_ssl_cbf server_cbf;
  35494. test_ssl_cbf client_cbf;
  35495. /* test version failure doing post auth with TLS 1.2 connection */
  35496. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  35497. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  35498. server_cbf.method = wolfTLSv1_2_server_method;
  35499. server_cbf.ssl_ready = set_post_auth_cb;
  35500. server_cbf.on_result = post_auth_version_cb;
  35501. client_cbf.ssl_ready = set_post_auth_cb;
  35502. client_cbf.on_result = post_auth_version_client_cb;
  35503. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35504. &server_cbf, NULL), TEST_SUCCESS);
  35505. /* tests on post auth with TLS 1.3 */
  35506. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  35507. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  35508. server_cbf.method = wolfTLSv1_3_server_method;
  35509. server_cbf.ssl_ready = set_post_auth_cb;
  35510. client_cbf.ssl_ready = set_post_auth_cb;
  35511. server_cbf.on_result = post_auth_cb;
  35512. client_cbf.on_result = NULL;
  35513. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  35514. &server_cbf, NULL), TEST_SUCCESS);
  35515. #endif
  35516. return EXPECT_RESULT();
  35517. }
  35518. static int test_wolfSSL_X509_NID(void)
  35519. {
  35520. EXPECT_DECLS;
  35521. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  35522. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  35523. int sigType;
  35524. int nameSz;
  35525. X509* cert = NULL;
  35526. EVP_PKEY* pubKeyTmp = NULL;
  35527. X509_NAME* name = NULL;
  35528. char commonName[80];
  35529. char countryName[80];
  35530. char localityName[80];
  35531. char stateName[80];
  35532. char orgName[80];
  35533. char orgUnit[80];
  35534. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  35535. /* convert cert from DER to internal WOLFSSL_X509 struct */
  35536. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  35537. sizeof_client_cert_der_2048, HEAP_HINT));
  35538. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  35539. /* extract PUBLIC KEY from cert */
  35540. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  35541. /* extract signatureType */
  35542. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  35543. /* extract subjectName info */
  35544. ExpectNotNull(name = X509_get_subject_name(cert));
  35545. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  35546. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35547. NULL, 0)), 0);
  35548. ExpectIntEQ(nameSz, 15);
  35549. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35550. commonName, sizeof(commonName))), 0);
  35551. ExpectIntEQ(nameSz, 15);
  35552. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  35553. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  35554. commonName, 9)), 0);
  35555. ExpectIntEQ(nameSz, 8);
  35556. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  35557. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  35558. countryName, sizeof(countryName))), 0);
  35559. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  35560. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  35561. localityName, sizeof(localityName))), 0);
  35562. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  35563. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  35564. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  35565. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  35566. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  35567. orgName, sizeof(orgName))), 0);
  35568. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  35569. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  35570. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  35571. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  35572. EVP_PKEY_free(pubKeyTmp);
  35573. X509_free(cert);
  35574. #endif
  35575. return EXPECT_RESULT();
  35576. }
  35577. static int test_wolfSSL_CTX_set_srp_username(void)
  35578. {
  35579. EXPECT_DECLS;
  35580. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  35581. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  35582. WOLFSSL_CTX* ctx = NULL;
  35583. WOLFSSL* ssl = NULL;
  35584. const char *username = "TESTUSER";
  35585. const char *password = "TESTPASSWORD";
  35586. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35587. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35588. SSL_SUCCESS);
  35589. wolfSSL_CTX_free(ctx);
  35590. ctx = NULL;
  35591. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35592. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35593. SSL_SUCCESS);
  35594. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35595. SSL_SUCCESS);
  35596. ExpectNotNull(ssl = SSL_new(ctx));
  35597. ExpectNotNull(SSL_get_srp_username(ssl));
  35598. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  35599. wolfSSL_free(ssl);
  35600. wolfSSL_CTX_free(ctx);
  35601. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  35602. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  35603. return EXPECT_RESULT();
  35604. }
  35605. static int test_wolfSSL_CTX_set_srp_password(void)
  35606. {
  35607. EXPECT_DECLS;
  35608. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  35609. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  35610. WOLFSSL_CTX* ctx = NULL;
  35611. const char *username = "TESTUSER";
  35612. const char *password = "TESTPASSWORD";
  35613. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35614. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35615. SSL_SUCCESS);
  35616. wolfSSL_CTX_free(ctx);
  35617. ctx = NULL;
  35618. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35619. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  35620. SSL_SUCCESS);
  35621. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  35622. SSL_SUCCESS);
  35623. wolfSSL_CTX_free(ctx);
  35624. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  35625. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  35626. return EXPECT_RESULT();
  35627. }
  35628. static int test_wolfSSL_X509_STORE(void)
  35629. {
  35630. EXPECT_DECLS;
  35631. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  35632. X509_STORE *store = NULL;
  35633. #ifdef HAVE_CRL
  35634. X509_STORE_CTX *storeCtx = NULL;
  35635. X509_CRL *crl = NULL;
  35636. X509 *ca = NULL;
  35637. X509 *cert = NULL;
  35638. const char crlPem[] = "./certs/crl/crl.revoked";
  35639. const char srvCert[] = "./certs/server-revoked-cert.pem";
  35640. const char caCert[] = "./certs/ca-cert.pem";
  35641. XFILE fp = XBADFILE;
  35642. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35643. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  35644. SSL_FILETYPE_PEM)));
  35645. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  35646. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  35647. SSL_FILETYPE_PEM)));
  35648. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  35649. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  35650. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  35651. X509_STORE_free(store);
  35652. store = NULL;
  35653. X509_STORE_CTX_free(storeCtx);
  35654. storeCtx = NULL;
  35655. X509_free(cert);
  35656. cert = NULL;
  35657. X509_free(ca);
  35658. ca = NULL;
  35659. /* should fail to verify now after adding in CRL */
  35660. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35661. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  35662. SSL_FILETYPE_PEM)));
  35663. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  35664. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  35665. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  35666. NULL, NULL));
  35667. if (fp != XBADFILE)
  35668. XFCLOSE(fp);
  35669. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  35670. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  35671. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  35672. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  35673. SSL_FILETYPE_PEM)));
  35674. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  35675. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  35676. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  35677. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  35678. X509_CRL_free(crl);
  35679. crl = NULL;
  35680. X509_STORE_free(store);
  35681. store = NULL;
  35682. X509_STORE_CTX_free(storeCtx);
  35683. storeCtx = NULL;
  35684. X509_free(cert);
  35685. cert = NULL;
  35686. X509_free(ca);
  35687. ca = NULL;
  35688. #endif /* HAVE_CRL */
  35689. #ifndef WOLFCRYPT_ONLY
  35690. {
  35691. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35692. SSL_CTX* ctx = NULL;
  35693. SSL* ssl = NULL;
  35694. int i;
  35695. for (i = 0; i < 2; i++) {
  35696. #ifndef NO_WOLFSSL_SERVER
  35697. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35698. #else
  35699. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35700. #endif
  35701. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35702. SSL_CTX_set_cert_store(ctx, store);
  35703. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35704. SSL_CTX_set_cert_store(ctx, store);
  35705. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  35706. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35707. SSL_FILETYPE_PEM), SSL_SUCCESS);
  35708. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35709. SSL_FILETYPE_PEM), SSL_SUCCESS);
  35710. ExpectNotNull(ssl = SSL_new(ctx));
  35711. if (i == 0) {
  35712. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  35713. SSL_SUCCESS);
  35714. }
  35715. else {
  35716. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  35717. #ifdef OPENSSL_ALL
  35718. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  35719. #endif
  35720. }
  35721. if (EXPECT_FAIL() || (i == 1)) {
  35722. X509_STORE_free(store);
  35723. store = NULL;
  35724. }
  35725. SSL_free(ssl);
  35726. ssl = NULL;
  35727. SSL_CTX_free(ctx);
  35728. ctx = NULL;
  35729. }
  35730. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35731. }
  35732. #endif
  35733. #endif
  35734. return EXPECT_RESULT();
  35735. }
  35736. static int test_wolfSSL_X509_STORE_load_locations(void)
  35737. {
  35738. EXPECT_DECLS;
  35739. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  35740. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35741. SSL_CTX *ctx = NULL;
  35742. X509_STORE *store = NULL;
  35743. const char ca_file[] = "./certs/ca-cert.pem";
  35744. const char client_pem_file[] = "./certs/client-cert.pem";
  35745. const char client_der_file[] = "./certs/client-cert.der";
  35746. const char ecc_file[] = "./certs/ecc-key.pem";
  35747. const char certs_path[] = "./certs/";
  35748. const char bad_path[] = "./bad-path/";
  35749. #ifdef HAVE_CRL
  35750. const char crl_path[] = "./certs/crl/";
  35751. const char crl_file[] = "./certs/crl/crl.pem";
  35752. #endif
  35753. #ifndef NO_WOLFSSL_SERVER
  35754. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35755. #else
  35756. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35757. #endif
  35758. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35759. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  35760. WOLFSSL_SUCCESS);
  35761. /* Test bad arguments */
  35762. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  35763. WOLFSSL_FAILURE);
  35764. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  35765. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  35766. WOLFSSL_FAILURE);
  35767. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  35768. WOLFSSL_FAILURE);
  35769. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  35770. WOLFSSL_FAILURE);
  35771. #ifdef HAVE_CRL
  35772. /* Test with CRL */
  35773. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  35774. WOLFSSL_SUCCESS);
  35775. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  35776. WOLFSSL_SUCCESS);
  35777. #endif
  35778. /* Test with CA */
  35779. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  35780. WOLFSSL_SUCCESS);
  35781. /* Test with client_cert and certs path */
  35782. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  35783. WOLFSSL_SUCCESS);
  35784. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  35785. WOLFSSL_SUCCESS);
  35786. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  35787. /* Clear nodes */
  35788. ERR_clear_error();
  35789. #endif
  35790. SSL_CTX_free(ctx);
  35791. #endif
  35792. return EXPECT_RESULT();
  35793. }
  35794. static int test_X509_STORE_get0_objects(void)
  35795. {
  35796. EXPECT_DECLS;
  35797. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  35798. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  35799. X509_STORE *store = NULL;
  35800. X509_STORE *store_cpy = NULL;
  35801. SSL_CTX *ctx = NULL;
  35802. X509_OBJECT *obj = NULL;
  35803. STACK_OF(X509_OBJECT) *objs = NULL;
  35804. int i;
  35805. /* Setup store */
  35806. #ifndef NO_WOLFSSL_SERVER
  35807. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  35808. #else
  35809. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  35810. #endif
  35811. ExpectNotNull(store_cpy = X509_STORE_new());
  35812. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  35813. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  35814. WOLFSSL_SUCCESS);
  35815. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  35816. WOLFSSL_SUCCESS);
  35817. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  35818. WOLFSSL_SUCCESS);
  35819. #ifdef HAVE_CRL
  35820. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  35821. WOLFSSL_SUCCESS);
  35822. #endif
  35823. /* Store ready */
  35824. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  35825. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  35826. #ifdef HAVE_CRL
  35827. #ifdef WOLFSSL_SIGNER_DER_CERT
  35828. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  35829. #else
  35830. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  35831. #endif
  35832. #else
  35833. #ifdef WOLFSSL_SIGNER_DER_CERT
  35834. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  35835. #else
  35836. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  35837. #endif
  35838. #endif
  35839. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  35840. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  35841. switch (X509_OBJECT_get_type(obj)) {
  35842. case X509_LU_X509:
  35843. {
  35844. WOLFSSL_X509* x509;
  35845. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  35846. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  35847. break;
  35848. }
  35849. case X509_LU_CRL:
  35850. #ifdef HAVE_CRL
  35851. {
  35852. WOLFSSL_CRL* crl = NULL;
  35853. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  35854. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  35855. break;
  35856. }
  35857. #endif
  35858. case X509_LU_NONE:
  35859. default:
  35860. Fail(("X509_OBJECT_get_type should return x509 or crl "
  35861. "(when built with crl support)"),
  35862. ("Unrecognized X509_OBJECT type or none"));
  35863. }
  35864. }
  35865. X509_STORE_free(store_cpy);
  35866. SSL_CTX_free(ctx);
  35867. #endif
  35868. return EXPECT_RESULT();
  35869. }
  35870. static int test_wolfSSL_BN_CTX(void)
  35871. {
  35872. EXPECT_DECLS;
  35873. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35874. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35875. WOLFSSL_BN_CTX* bn_ctx = NULL;
  35876. WOLFSSL_BIGNUM* t = NULL;
  35877. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  35878. /* No implementation. */
  35879. BN_CTX_init(NULL);
  35880. ExpectNotNull(t = BN_CTX_get(NULL));
  35881. BN_free(t);
  35882. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  35883. BN_free(t);
  35884. #ifndef NO_WOLFSSL_STUB
  35885. /* No implementation. */
  35886. BN_CTX_start(NULL);
  35887. BN_CTX_start(bn_ctx);
  35888. #endif
  35889. BN_CTX_free(NULL);
  35890. BN_CTX_free(bn_ctx);
  35891. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35892. return EXPECT_RESULT();
  35893. }
  35894. static int test_wolfSSL_BN(void)
  35895. {
  35896. EXPECT_DECLS;
  35897. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35898. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35899. BIGNUM* a = NULL;
  35900. BIGNUM* b = NULL;
  35901. BIGNUM* c = NULL;
  35902. BIGNUM* d = NULL;
  35903. BIGNUM emptyBN;
  35904. /* Setup */
  35905. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35906. /* internal not set emptyBN. */
  35907. ExpectNotNull(a = BN_new());
  35908. ExpectNotNull(b = BN_new());
  35909. ExpectNotNull(c = BN_dup(b));
  35910. ExpectNotNull(d = BN_new());
  35911. /* Invalid parameter testing. */
  35912. BN_free(NULL);
  35913. ExpectNull(BN_dup(NULL));
  35914. ExpectNull(BN_dup(&emptyBN));
  35915. ExpectNull(BN_copy(NULL, NULL));
  35916. ExpectNull(BN_copy(b, NULL));
  35917. ExpectNull(BN_copy(NULL, c));
  35918. ExpectNull(BN_copy(b, &emptyBN));
  35919. ExpectNull(BN_copy(&emptyBN, c));
  35920. BN_clear(NULL);
  35921. BN_clear(&emptyBN);
  35922. ExpectIntEQ(BN_num_bytes(NULL), 0);
  35923. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  35924. ExpectIntEQ(BN_num_bits(NULL), 0);
  35925. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  35926. ExpectIntEQ(BN_is_negative(NULL), 0);
  35927. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  35928. /* END Invalid Parameters */
  35929. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  35930. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  35931. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  35932. ExpectIntEQ(BN_num_bits(a), 2);
  35933. ExpectIntEQ(BN_num_bytes(a), 1);
  35934. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  35935. defined(WOLFSSL_SP_INT_NEGATIVE))
  35936. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  35937. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  35938. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  35939. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  35940. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  35941. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  35942. {
  35943. /* Do additional tests on negative BN conversions. */
  35944. char* ret = NULL;
  35945. ASN1_INTEGER* asn1 = NULL;
  35946. BIGNUM* tmp = NULL;
  35947. /* Sanity check we have a negative BN. */
  35948. ExpectIntEQ(BN_is_negative(c), 1);
  35949. ExpectNotNull(ret = BN_bn2dec(c));
  35950. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35951. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35952. ret = NULL;
  35953. /* Convert to ASN1_INTEGER and back to BN. */
  35954. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  35955. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  35956. /* After converting back BN should be negative and correct. */
  35957. ExpectIntEQ(BN_is_negative(tmp), 1);
  35958. ExpectNotNull(ret = BN_bn2dec(tmp));
  35959. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  35960. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  35961. ASN1_INTEGER_free(asn1);
  35962. BN_free(tmp);
  35963. }
  35964. #endif
  35965. ExpectIntEQ(BN_get_word(c), 4);
  35966. #endif
  35967. ExpectIntEQ(BN_set_word(a, 3), 1);
  35968. ExpectIntEQ(BN_set_word(b, 3), 1);
  35969. ExpectIntEQ(BN_set_word(c, 4), 1);
  35970. /* NULL == NULL, NULL < num, num > NULL */
  35971. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  35972. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  35973. ExpectIntLT(BN_cmp(NULL, b), 0);
  35974. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  35975. ExpectIntGT(BN_cmp(a, NULL), 0);
  35976. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  35977. ExpectIntEQ(BN_cmp(a, b), 0);
  35978. ExpectIntLT(BN_cmp(a, c), 0);
  35979. ExpectIntGT(BN_cmp(c, b), 0);
  35980. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  35981. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  35982. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  35983. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  35984. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  35985. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  35986. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  35987. #endif
  35988. BN_clear(a);
  35989. BN_free(a);
  35990. BN_free(b);
  35991. BN_free(c);
  35992. BN_clear_free(d);
  35993. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35994. return EXPECT_RESULT();
  35995. }
  35996. static int test_wolfSSL_BN_init(void)
  35997. {
  35998. EXPECT_DECLS;
  35999. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36000. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36001. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  36002. BIGNUM* ap = NULL;
  36003. BIGNUM bv;
  36004. BIGNUM cv;
  36005. BIGNUM dv;
  36006. ExpectNotNull(ap = BN_new());
  36007. BN_init(NULL);
  36008. XMEMSET(&bv, 0, sizeof(bv));
  36009. ExpectNull(BN_dup(&bv));
  36010. BN_init(&bv);
  36011. BN_init(&cv);
  36012. BN_init(&dv);
  36013. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  36014. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  36015. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  36016. /* a^b mod c = */
  36017. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  36018. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  36019. /* check result 3^2 mod 5 */
  36020. ExpectIntEQ(BN_get_word(&dv), 4);
  36021. /* a*b mod c = */
  36022. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  36023. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  36024. /* check result 3*2 mod 5 */
  36025. ExpectIntEQ(BN_get_word(&dv), 1);
  36026. BN_free(ap);
  36027. #endif
  36028. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36029. return EXPECT_RESULT();
  36030. }
  36031. static int test_wolfSSL_BN_enc_dec(void)
  36032. {
  36033. EXPECT_DECLS;
  36034. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  36035. BIGNUM* a = NULL;
  36036. BIGNUM* b = NULL;
  36037. BIGNUM* c = NULL;
  36038. BIGNUM emptyBN;
  36039. char* str = NULL;
  36040. const char* emptyStr = "";
  36041. const char* numberStr = "12345";
  36042. const char* badStr = "g12345";
  36043. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36044. const char* twoStr = "2";
  36045. #endif
  36046. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  36047. unsigned char outNum[5];
  36048. /* Setup */
  36049. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36050. ExpectNotNull(a = BN_new());
  36051. ExpectNotNull(b = BN_new());
  36052. ExpectIntEQ(BN_set_word(a, 2), 1);
  36053. /* Invalid parameters */
  36054. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  36055. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  36056. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  36057. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  36058. ExpectNull(BN_bn2hex(NULL));
  36059. ExpectNull(BN_bn2hex(&emptyBN));
  36060. ExpectNull(BN_bn2dec(NULL));
  36061. ExpectNull(BN_bn2dec(&emptyBN));
  36062. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  36063. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  36064. ExpectNull(BN_bin2bn(binNum, -1, a));
  36065. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  36066. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  36067. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  36068. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  36069. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  36070. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  36071. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  36072. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  36073. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  36074. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  36075. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  36076. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  36077. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  36078. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  36079. ExpectIntEQ(BN_set_word(a, 2), 1);
  36080. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  36081. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  36082. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  36083. ExpectIntEQ(BN_cmp(a, b), 0);
  36084. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  36085. ExpectIntEQ(BN_cmp(a, b), -1);
  36086. ExpectNotNull(str = BN_bn2hex(a));
  36087. ExpectNotNull(BN_hex2bn(&b, str));
  36088. ExpectIntEQ(BN_cmp(a, b), 0);
  36089. ExpectNotNull(BN_hex2bn(&b, numberStr));
  36090. ExpectIntEQ(BN_cmp(a, b), -1);
  36091. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36092. str = NULL;
  36093. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  36094. ExpectNotNull(str = BN_bn2dec(a));
  36095. ExpectStrEQ(str, twoStr);
  36096. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36097. str = NULL;
  36098. #ifndef NO_RSA
  36099. ExpectNotNull(str = BN_bn2dec(a));
  36100. ExpectNotNull(BN_dec2bn(&b, str));
  36101. ExpectIntEQ(BN_cmp(a, b), 0);
  36102. ExpectNotNull(BN_dec2bn(&b, numberStr));
  36103. ExpectIntEQ(BN_cmp(a, b), -1);
  36104. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  36105. str = NULL;
  36106. #else
  36107. /* No implementation - fail with good parameters. */
  36108. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  36109. #endif
  36110. #endif
  36111. BN_free(b);
  36112. BN_free(a);
  36113. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36114. return EXPECT_RESULT();
  36115. }
  36116. static int test_wolfSSL_BN_word(void)
  36117. {
  36118. EXPECT_DECLS;
  36119. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  36120. BIGNUM* a = NULL;
  36121. BIGNUM* b = NULL;
  36122. BIGNUM* c = NULL;
  36123. BIGNUM av;
  36124. ExpectNotNull(a = BN_new());
  36125. ExpectNotNull(b = BN_new());
  36126. ExpectNotNull(c = BN_new());
  36127. XMEMSET(&av, 0, sizeof(av));
  36128. /* Invalid parameter. */
  36129. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  36130. ExpectIntEQ(BN_add_word(&av, 3), 0);
  36131. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  36132. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  36133. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  36134. ExpectIntEQ(BN_set_word(&av, 3), 0);
  36135. ExpectIntEQ(BN_get_word(NULL), 0);
  36136. ExpectIntEQ(BN_get_word(&av), 0);
  36137. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  36138. ExpectIntEQ(BN_is_word(&av, 3), 0);
  36139. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  36140. !defined(NO_DSA))
  36141. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  36142. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  36143. #endif
  36144. ExpectIntEQ(BN_one(NULL), 0);
  36145. ExpectIntEQ(BN_one(&av), 0);
  36146. BN_zero(NULL);
  36147. BN_zero(&av);
  36148. ExpectIntEQ(BN_is_one(NULL), 0);
  36149. ExpectIntEQ(BN_is_one(&av), 0);
  36150. ExpectIntEQ(BN_is_zero(NULL), 0);
  36151. ExpectIntEQ(BN_is_zero(&av), 0);
  36152. ExpectIntEQ(BN_set_word(a, 3), 1);
  36153. ExpectIntEQ(BN_set_word(b, 2), 1);
  36154. ExpectIntEQ(BN_set_word(c, 5), 1);
  36155. /* a + 3 = */
  36156. ExpectIntEQ(BN_add_word(a, 3), 1);
  36157. /* check result 3 + 3*/
  36158. ExpectIntEQ(BN_get_word(a), 6);
  36159. ExpectIntEQ(BN_is_word(a, 6), 1);
  36160. ExpectIntEQ(BN_is_word(a, 5), 0);
  36161. /* set a back to 3 */
  36162. ExpectIntEQ(BN_set_word(a, 3), 1);
  36163. /* a - 3 = */
  36164. ExpectIntEQ(BN_sub_word(a, 3), 1);
  36165. /* check result 3 - 3*/
  36166. ExpectIntEQ(BN_get_word(a), 0);
  36167. ExpectIntEQ(BN_one(a), 1);
  36168. ExpectIntEQ(BN_is_word(a, 1), 1);
  36169. ExpectIntEQ(BN_is_word(a, 0), 0);
  36170. ExpectIntEQ(BN_is_one(a), 1);
  36171. ExpectIntEQ(BN_is_zero(a), 0);
  36172. BN_zero(a);
  36173. ExpectIntEQ(BN_is_word(a, 0), 1);
  36174. ExpectIntEQ(BN_is_word(a, 1), 0);
  36175. ExpectIntEQ(BN_is_zero(a), 1);
  36176. ExpectIntEQ(BN_is_one(a), 0);
  36177. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  36178. !defined(NO_DSA))
  36179. ExpectIntEQ(BN_set_word(a, 5), 1);
  36180. ExpectIntEQ(BN_mod_word(a, 3), 2);
  36181. ExpectIntEQ(BN_mod_word(a, 0), -1);
  36182. #endif
  36183. BN_free(c);
  36184. BN_free(b);
  36185. BN_free(a);
  36186. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36187. return EXPECT_RESULT();
  36188. }
  36189. static int test_wolfSSL_BN_bits(void)
  36190. {
  36191. EXPECT_DECLS;
  36192. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36193. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36194. BIGNUM* a = NULL;
  36195. BIGNUM emptyBN;
  36196. /* Setup */
  36197. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36198. ExpectNotNull(a = BN_new());
  36199. /* Invalid parameters. */
  36200. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  36201. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  36202. ExpectIntEQ(BN_set_bit(a, -1), 0);
  36203. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  36204. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  36205. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  36206. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  36207. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  36208. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  36209. ExpectIntEQ(BN_is_odd(NULL), 0);
  36210. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  36211. ExpectIntEQ(BN_set_word(a, 0), 1);
  36212. ExpectIntEQ(BN_is_zero(a), 1);
  36213. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  36214. ExpectIntEQ(BN_is_zero(a), 0);
  36215. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  36216. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  36217. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  36218. ExpectIntEQ(BN_is_zero(a), 1);
  36219. ExpectIntEQ(BN_set_bit(a, 0), 1);
  36220. ExpectIntEQ(BN_is_odd(a), 1);
  36221. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  36222. ExpectIntEQ(BN_is_odd(a), 0);
  36223. ExpectIntEQ(BN_set_bit(a, 1), 1);
  36224. ExpectIntEQ(BN_is_odd(a), 0);
  36225. ExpectIntEQ(BN_set_bit(a, 129), 1);
  36226. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  36227. #ifndef NO_WOLFSSL_STUB
  36228. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  36229. #endif
  36230. BN_free(a);
  36231. #endif
  36232. return EXPECT_RESULT();
  36233. }
  36234. static int test_wolfSSL_BN_shift(void)
  36235. {
  36236. EXPECT_DECLS;
  36237. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36238. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36239. BIGNUM* a = NULL;
  36240. BIGNUM* b = NULL;
  36241. BIGNUM emptyBN;
  36242. /* Setup */
  36243. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36244. ExpectNotNull(a = BN_new());
  36245. ExpectNotNull(b = BN_new());
  36246. /* Invalid parameters. */
  36247. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  36248. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  36249. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  36250. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  36251. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  36252. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  36253. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  36254. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  36255. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  36256. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  36257. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  36258. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  36259. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  36260. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  36261. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  36262. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  36263. ExpectIntEQ(BN_set_word(a, 1), 1);
  36264. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  36265. ExpectIntEQ(BN_is_word(b, 2), 1);
  36266. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  36267. ExpectIntEQ(BN_is_word(a, 2), 1);
  36268. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  36269. ExpectIntEQ(BN_is_word(b, 1), 1);
  36270. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  36271. ExpectIntEQ(BN_is_word(a, 1), 1);
  36272. BN_free(b);
  36273. BN_free(a);
  36274. #endif
  36275. return EXPECT_RESULT();
  36276. }
  36277. static int test_wolfSSL_BN_math(void)
  36278. {
  36279. EXPECT_DECLS;
  36280. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36281. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36282. BIGNUM* a = NULL;
  36283. BIGNUM* b = NULL;
  36284. BIGNUM* r = NULL;
  36285. BIGNUM* rem = NULL;
  36286. BIGNUM emptyBN;
  36287. BN_ULONG val1;
  36288. BN_ULONG val2;
  36289. /* Setup */
  36290. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36291. ExpectNotNull(a = BN_new());
  36292. ExpectNotNull(b = BN_new());
  36293. ExpectNotNull(r = BN_new());
  36294. ExpectNotNull(rem = BN_new());
  36295. /* Invalid parameters. */
  36296. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  36297. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  36298. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  36299. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  36300. ExpectIntEQ(BN_add(r, a, NULL), 0);
  36301. ExpectIntEQ(BN_add(r, NULL, b), 0);
  36302. ExpectIntEQ(BN_add(NULL, a, b), 0);
  36303. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  36304. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  36305. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  36306. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  36307. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  36308. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  36309. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  36310. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  36311. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  36312. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  36313. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  36314. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  36315. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  36316. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  36317. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  36318. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  36319. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  36320. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  36321. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  36322. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  36323. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  36324. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  36325. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  36326. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  36327. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  36328. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  36329. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  36330. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  36331. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36332. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  36333. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  36334. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  36335. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  36336. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  36337. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  36338. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  36339. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  36340. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  36341. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  36342. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  36343. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  36344. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  36345. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  36346. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  36347. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36348. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36349. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  36350. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  36351. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  36352. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  36353. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  36354. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  36355. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  36356. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  36357. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  36358. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  36359. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  36360. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  36361. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  36362. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  36363. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36364. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  36365. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  36366. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  36367. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  36368. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  36369. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  36370. /* END Invalid parameters. */
  36371. val1 = 8;
  36372. val2 = 3;
  36373. ExpectIntEQ(BN_set_word(a, val1), 1);
  36374. ExpectIntEQ(BN_set_word(b, val2), 1);
  36375. ExpectIntEQ(BN_add(r, a, b), 1);
  36376. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  36377. ExpectIntEQ(BN_sub(r, a, b), 1);
  36378. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  36379. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  36380. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  36381. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  36382. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  36383. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  36384. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  36385. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  36386. BN_free(rem);
  36387. BN_free(r);
  36388. BN_free(b);
  36389. BN_free(a);
  36390. #endif
  36391. return EXPECT_RESULT();
  36392. }
  36393. static int test_wolfSSL_BN_math_mod(void)
  36394. {
  36395. EXPECT_DECLS;
  36396. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36397. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36398. BIGNUM* a = NULL;
  36399. BIGNUM* b = NULL;
  36400. BIGNUM* m = NULL;
  36401. BIGNUM* r = NULL;
  36402. BIGNUM* t = NULL;
  36403. BIGNUM emptyBN;
  36404. BN_ULONG val1;
  36405. BN_ULONG val2;
  36406. BN_ULONG val3;
  36407. /* Setup */
  36408. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36409. ExpectNotNull(a = BN_new());
  36410. ExpectNotNull(b = BN_new());
  36411. ExpectNotNull(m = BN_new());
  36412. ExpectNotNull(r = BN_new());
  36413. /* Invalid parameters. */
  36414. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  36415. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  36416. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  36417. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  36418. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  36419. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  36420. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  36421. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  36422. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  36423. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36424. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36425. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36426. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36427. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36428. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  36429. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  36430. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  36431. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  36432. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  36433. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  36434. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  36435. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  36436. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  36437. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  36438. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  36439. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  36440. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  36441. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36442. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36443. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36444. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36445. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36446. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  36447. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  36448. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  36449. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  36450. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  36451. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  36452. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  36453. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  36454. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  36455. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  36456. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  36457. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  36458. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  36459. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36460. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36461. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  36462. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  36463. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  36464. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  36465. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  36466. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  36467. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  36468. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  36469. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  36470. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  36471. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  36472. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  36473. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  36474. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  36475. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  36476. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  36477. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  36478. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  36479. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  36480. /* END Invalid parameters. */
  36481. val1 = 9;
  36482. val2 = 13;
  36483. val3 = 5;
  36484. ExpectIntEQ(BN_set_word(a, val1), 1);
  36485. ExpectIntEQ(BN_set_word(b, val2), 1);
  36486. ExpectIntEQ(BN_set_word(m, val3), 1);
  36487. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  36488. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  36489. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  36490. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  36491. ExpectIntEQ(BN_set_word(a, 2), 1);
  36492. ExpectIntEQ(BN_set_word(b, 3), 1);
  36493. ExpectIntEQ(BN_set_word(m, 5), 1);
  36494. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  36495. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  36496. ExpectIntEQ(BN_is_word(r, 3), 1);
  36497. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  36498. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  36499. ExpectIntEQ(BN_is_word(r, 3), 1);
  36500. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  36501. ExpectIntEQ(BN_is_word(t, 3), 1);
  36502. BN_free(t);
  36503. /* No inverse case. No inverse when a divides b. */
  36504. ExpectIntEQ(BN_set_word(a, 3), 1);
  36505. ExpectIntEQ(BN_set_word(m, 9), 1);
  36506. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  36507. BN_free(r);
  36508. BN_free(m);
  36509. BN_free(b);
  36510. BN_free(a);
  36511. #endif
  36512. return EXPECT_RESULT();
  36513. }
  36514. static int test_wolfSSL_BN_math_other(void)
  36515. {
  36516. EXPECT_DECLS;
  36517. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36518. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36519. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  36520. BIGNUM* a = NULL;
  36521. BIGNUM* b = NULL;
  36522. BIGNUM* r = NULL;
  36523. BIGNUM emptyBN;
  36524. /* Setup */
  36525. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36526. ExpectNotNull(a = BN_new());
  36527. ExpectNotNull(b = BN_new());
  36528. ExpectNotNull(r = BN_new());
  36529. /* Invalid parameters. */
  36530. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  36531. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  36532. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  36533. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  36534. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  36535. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  36536. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  36537. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  36538. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  36539. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  36540. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  36541. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  36542. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  36543. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  36544. /* END Invalid parameters. */
  36545. /* No common factors between 2 and 3. */
  36546. ExpectIntEQ(BN_set_word(a, 2), 1);
  36547. ExpectIntEQ(BN_set_word(b, 3), 1);
  36548. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  36549. ExpectIntEQ(BN_is_word(r, 1), 1);
  36550. /* 3 is largest value that divides both 6 and 9. */
  36551. ExpectIntEQ(BN_set_word(a, 6), 1);
  36552. ExpectIntEQ(BN_set_word(b, 9), 1);
  36553. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  36554. ExpectIntEQ(BN_is_word(r, 3), 1);
  36555. /* GCD of 0 and 0 is undefined. */
  36556. ExpectIntEQ(BN_set_word(a, 0), 1);
  36557. ExpectIntEQ(BN_set_word(b, 0), 1);
  36558. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  36559. /* Teardown */
  36560. BN_free(r);
  36561. BN_free(b);
  36562. BN_free(a);
  36563. #endif
  36564. #endif
  36565. return EXPECT_RESULT();
  36566. }
  36567. static int test_wolfSSL_BN_rand(void)
  36568. {
  36569. EXPECT_DECLS;
  36570. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  36571. BIGNUM* bn = NULL;
  36572. BIGNUM* range = NULL;
  36573. BIGNUM emptyBN;
  36574. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36575. ExpectNotNull(bn = BN_new());
  36576. ExpectNotNull(range = BN_new());
  36577. /* Invalid parameters. */
  36578. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  36579. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  36580. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  36581. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  36582. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  36583. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  36584. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  36585. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  36586. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  36587. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  36588. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  36589. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  36590. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  36591. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  36592. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  36593. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  36594. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  36595. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  36596. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  36597. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36598. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36599. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36600. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36601. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36602. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36603. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36604. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36605. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36606. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36607. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36608. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36609. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36610. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36611. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36612. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36613. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  36614. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36615. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  36616. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36617. /* 1 bit random value must have no more than one top bit set. */
  36618. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36619. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36620. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36621. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36622. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36623. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  36624. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  36625. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  36626. /* END Invalid parameters. */
  36627. /* 0 bit random: 0. */
  36628. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36629. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36630. ExpectIntEQ(BN_is_zero(bn), 1);
  36631. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  36632. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  36633. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36634. ExpectIntEQ(BN_is_zero(bn), 1);
  36635. /* 1 bit random: 0 or 1. */
  36636. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  36637. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36638. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  36639. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36640. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36641. ExpectIntEQ(BN_get_word(bn), 1);
  36642. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36643. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36644. ExpectIntEQ(BN_get_word(bn), 1);
  36645. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  36646. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36647. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  36648. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36649. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36650. ExpectIntEQ(BN_get_word(bn), 1);
  36651. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  36652. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36653. ExpectIntEQ(BN_get_word(bn), 1);
  36654. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36655. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36656. ExpectIntEQ(BN_num_bits(bn), 8);
  36657. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36658. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36659. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36660. ExpectIntEQ(BN_num_bits(bn), 8);
  36661. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36662. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  36663. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36664. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36665. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  36666. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  36667. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36668. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  36669. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  36670. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36671. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36672. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  36673. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  36674. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  36675. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  36676. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  36677. * requested number of bits up to the nearest multiple of 8. E.g. in this
  36678. * case, requesting a 13-bit random number would actually return a 16-bit
  36679. * random number. */
  36680. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  36681. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36682. ExpectIntEQ(BN_num_bits(bn), 13);
  36683. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  36684. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  36685. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36686. ExpectIntEQ(BN_set_word(range, 0), 1);
  36687. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36688. ExpectIntEQ(BN_set_word(range, 1), 1);
  36689. ExpectIntEQ(BN_rand_range(bn, range), 1);
  36690. BN_free(bn);
  36691. BN_free(range);
  36692. #endif
  36693. return EXPECT_RESULT();
  36694. }
  36695. static int test_wolfSSL_BN_prime(void)
  36696. {
  36697. EXPECT_DECLS;
  36698. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  36699. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  36700. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  36701. BIGNUM* a = NULL;
  36702. BIGNUM* add = NULL;
  36703. BIGNUM* rem = NULL;
  36704. BIGNUM emptyBN;
  36705. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  36706. ExpectNotNull(a = BN_new());
  36707. ExpectNotNull(add = BN_new());
  36708. ExpectNotNull(rem = BN_new());
  36709. /* Invalid parameters. */
  36710. /* BN_generate_prime_ex()
  36711. * prime - must have valid BIGNUM
  36712. * bits - Greater then 0
  36713. * safe - not supported, must be 0
  36714. * add - not supported, must be NULL
  36715. * rem - not supported, must be NULL
  36716. * cb - anything
  36717. */
  36718. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  36719. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  36720. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  36721. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  36722. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  36723. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  36724. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  36725. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  36726. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  36727. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  36728. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  36729. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  36730. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  36731. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  36732. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  36733. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  36734. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  36735. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  36736. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  36737. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  36738. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  36739. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  36740. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  36741. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  36742. /* END Invalid parameters. */
  36743. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  36744. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  36745. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  36746. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  36747. BN_free(rem);
  36748. BN_free(add);
  36749. BN_free(a);
  36750. #endif
  36751. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  36752. return EXPECT_RESULT();
  36753. }
  36754. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  36755. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36756. #define TEST_ARG 0x1234
  36757. static void msg_cb(int write_p, int version, int content_type,
  36758. const void *buf, size_t len, SSL *ssl, void *arg)
  36759. {
  36760. (void)write_p;
  36761. (void)version;
  36762. (void)content_type;
  36763. (void)buf;
  36764. (void)len;
  36765. (void)ssl;
  36766. AssertTrue(arg == (void*)TEST_ARG);
  36767. }
  36768. #endif
  36769. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36770. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36771. #if defined(SESSION_CERTS)
  36772. #include "wolfssl/internal.h"
  36773. #endif
  36774. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  36775. {
  36776. EXPECT_DECLS;
  36777. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36778. STACK_OF(X509)* sk = NULL;
  36779. X509* x509 = NULL;
  36780. int i, num;
  36781. BIO* bio = NULL;
  36782. #endif
  36783. ExpectNotNull(ctx);
  36784. ExpectNotNull(ssl);
  36785. fprintf(stderr, "\n===== msgcb called ====\n");
  36786. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  36787. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  36788. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  36789. ExpectNotNull(SSL_get0_verified_chain(ssl));
  36790. #endif
  36791. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  36792. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  36793. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  36794. if (sk == NULL) {
  36795. BIO_free(bio);
  36796. return TEST_FAIL;
  36797. }
  36798. num = sk_X509_num(sk);
  36799. ExpectTrue(num > 0);
  36800. for (i = 0; i < num; i++) {
  36801. ExpectNotNull(x509 = sk_X509_value(sk,i));
  36802. if (x509 == NULL)
  36803. break;
  36804. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  36805. X509_print(bio,x509);
  36806. fprintf(stderr, "\n\n");
  36807. }
  36808. BIO_free(bio);
  36809. #endif
  36810. return EXPECT_RESULT();
  36811. }
  36812. #endif
  36813. static int test_wolfSSL_msgCb(void)
  36814. {
  36815. EXPECT_DECLS;
  36816. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  36817. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36818. test_ssl_cbf client_cb;
  36819. test_ssl_cbf server_cb;
  36820. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36821. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36822. #ifndef WOLFSSL_NO_TLS12
  36823. client_cb.method = wolfTLSv1_2_client_method;
  36824. server_cb.method = wolfTLSv1_2_server_method;
  36825. #else
  36826. client_cb.method = wolfTLSv1_3_client_method;
  36827. server_cb.method = wolfTLSv1_3_server_method;
  36828. #endif
  36829. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36830. &server_cb, msgCb), TEST_SUCCESS);
  36831. #endif
  36832. return EXPECT_RESULT();
  36833. }
  36834. static int test_wolfSSL_either_side(void)
  36835. {
  36836. EXPECT_DECLS;
  36837. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36838. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36839. test_ssl_cbf client_cb;
  36840. test_ssl_cbf server_cb;
  36841. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36842. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36843. /* Use different CTX for client and server */
  36844. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36845. ExpectNotNull(client_cb.ctx);
  36846. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  36847. ExpectNotNull(server_cb.ctx);
  36848. /* we are responsible for free'ing WOLFSSL_CTX */
  36849. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36850. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36851. &server_cb, NULL), TEST_SUCCESS);
  36852. wolfSSL_CTX_free(client_cb.ctx);
  36853. wolfSSL_CTX_free(server_cb.ctx);
  36854. #endif
  36855. return EXPECT_RESULT();
  36856. }
  36857. static int test_wolfSSL_DTLS_either_side(void)
  36858. {
  36859. EXPECT_DECLS;
  36860. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  36861. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  36862. test_ssl_cbf client_cb;
  36863. test_ssl_cbf server_cb;
  36864. XMEMSET(&client_cb, 0, sizeof(client_cb));
  36865. XMEMSET(&server_cb, 0, sizeof(server_cb));
  36866. /* Use different CTX for client and server */
  36867. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36868. ExpectNotNull(client_cb.ctx);
  36869. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  36870. ExpectNotNull(server_cb.ctx);
  36871. /* we are responsible for free'ing WOLFSSL_CTX */
  36872. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  36873. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  36874. &server_cb, NULL), TEST_SUCCESS);
  36875. wolfSSL_CTX_free(client_cb.ctx);
  36876. wolfSSL_CTX_free(server_cb.ctx);
  36877. #endif
  36878. return EXPECT_RESULT();
  36879. }
  36880. static int test_generate_cookie(void)
  36881. {
  36882. EXPECT_DECLS;
  36883. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  36884. SSL_CTX* ctx = NULL;
  36885. SSL* ssl = NULL;
  36886. byte buf[FOURK_BUF] = {0};
  36887. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  36888. ExpectNotNull(ssl = SSL_new(ctx));
  36889. /* Test unconnected */
  36890. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  36891. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  36892. wolfSSL_SetCookieCtx(ssl, ctx);
  36893. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  36894. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  36895. SSL_free(ssl);
  36896. SSL_CTX_free(ctx);
  36897. #endif
  36898. return EXPECT_RESULT();
  36899. }
  36900. static int test_wolfSSL_set_options(void)
  36901. {
  36902. EXPECT_DECLS;
  36903. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  36904. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  36905. WOLFSSL* ssl = NULL;
  36906. WOLFSSL_CTX* ctx = NULL;
  36907. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36908. char appData[] = "extra msg";
  36909. #endif
  36910. #ifdef OPENSSL_EXTRA
  36911. unsigned char protos[] = {
  36912. 7, 't', 'l', 's', '/', '1', '.', '2',
  36913. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  36914. };
  36915. unsigned int len = sizeof(protos);
  36916. void *arg = (void *)TEST_ARG;
  36917. #endif
  36918. #ifndef NO_WOLFSSL_SERVER
  36919. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36920. #else
  36921. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36922. #endif
  36923. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36924. WOLFSSL_FILETYPE_PEM));
  36925. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36926. WOLFSSL_FILETYPE_PEM));
  36927. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  36928. == WOLFSSL_OP_NO_TLSv1);
  36929. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  36930. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  36931. WOLFSSL_OP_NO_SSLv2)), 0);
  36932. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  36933. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  36934. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  36935. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  36936. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36937. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  36938. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  36939. WOLFSSL_OP_NO_COMPRESSION));
  36940. wolfSSL_CTX_free(ctx);
  36941. ctx = NULL;
  36942. #ifndef NO_WOLFSSL_SERVER
  36943. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  36944. #else
  36945. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36946. #endif
  36947. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  36948. WOLFSSL_FILETYPE_PEM));
  36949. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  36950. WOLFSSL_FILETYPE_PEM));
  36951. #ifdef OPENSSL_EXTRA
  36952. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  36953. #endif
  36954. ExpectNotNull(ssl = wolfSSL_new(ctx));
  36955. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  36956. #ifdef HAVE_EX_DATA
  36957. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  36958. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  36959. if (ssl != NULL) {
  36960. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  36961. appData, sizeof(appData)), 0);
  36962. }
  36963. #else
  36964. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  36965. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  36966. #endif
  36967. #endif
  36968. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  36969. WOLFSSL_OP_NO_TLSv1);
  36970. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  36971. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  36972. WOLFSSL_OP_NO_SSLv2)), 0);
  36973. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  36974. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  36975. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  36976. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  36977. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  36978. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  36979. #ifdef OPENSSL_EXTRA
  36980. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  36981. WOLFSSL_OP_NO_COMPRESSION));
  36982. #endif
  36983. #ifdef OPENSSL_EXTRA
  36984. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  36985. wolfSSL_set_msg_callback_arg(ssl, arg);
  36986. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  36987. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  36988. #else
  36989. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  36990. #endif
  36991. #endif
  36992. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  36993. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  36994. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  36995. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  36996. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  36997. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  36998. #else
  36999. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  37000. #endif
  37001. #endif /* HAVE_ALPN && !NO_BIO */
  37002. #endif
  37003. wolfSSL_free(ssl);
  37004. wolfSSL_CTX_free(ctx);
  37005. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37006. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37007. return EXPECT_RESULT();
  37008. }
  37009. static int test_wolfSSL_sk_SSL_CIPHER(void)
  37010. {
  37011. EXPECT_DECLS;
  37012. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  37013. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37014. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37015. SSL* ssl = NULL;
  37016. SSL_CTX* ctx = NULL;
  37017. STACK_OF(SSL_CIPHER) *sk = NULL;
  37018. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  37019. #ifndef NO_WOLFSSL_SERVER
  37020. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37021. #else
  37022. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37023. #endif
  37024. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  37025. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37026. ExpectNotNull(ssl = SSL_new(ctx));
  37027. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  37028. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  37029. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  37030. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  37031. /* error case because connection has not been established yet */
  37032. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  37033. sk_SSL_CIPHER_free(dupSk);
  37034. /* sk is pointer to internal struct that should be free'd in SSL_free */
  37035. SSL_free(ssl);
  37036. SSL_CTX_free(ctx);
  37037. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37038. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37039. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37040. return EXPECT_RESULT();
  37041. }
  37042. static int test_wolfSSL_set1_curves_list(void)
  37043. {
  37044. EXPECT_DECLS;
  37045. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  37046. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37047. SSL* ssl = NULL;
  37048. SSL_CTX* ctx = NULL;
  37049. #ifndef NO_WOLFSSL_SERVER
  37050. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37051. #else
  37052. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37053. #endif
  37054. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  37055. SSL_FILETYPE_PEM));
  37056. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  37057. ExpectNotNull(ssl = SSL_new(ctx));
  37058. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  37059. #ifdef HAVE_ECC
  37060. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  37061. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  37062. #endif
  37063. #ifdef HAVE_CURVE25519
  37064. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  37065. #else
  37066. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  37067. #endif
  37068. #ifdef HAVE_CURVE448
  37069. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  37070. #else
  37071. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  37072. #endif
  37073. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  37074. #ifdef HAVE_ECC
  37075. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  37076. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  37077. #endif
  37078. #ifdef HAVE_CURVE25519
  37079. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  37080. #else
  37081. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  37082. #endif
  37083. #ifdef HAVE_CURVE448
  37084. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  37085. #else
  37086. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  37087. #endif
  37088. SSL_free(ssl);
  37089. SSL_CTX_free(ctx);
  37090. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37091. #endif
  37092. return EXPECT_RESULT();
  37093. }
  37094. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37095. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  37096. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  37097. {
  37098. static int counter = 0;
  37099. EXPECT_DECLS;
  37100. if (counter % 2) {
  37101. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  37102. WOLFSSL_SUCCESS);
  37103. }
  37104. else {
  37105. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  37106. WOLFSSL_SUCCESS);
  37107. }
  37108. /* Ciphersuites that require curves */
  37109. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  37110. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  37111. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  37112. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  37113. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  37114. "ECDHE-ECDSA-CHACHA20-POLY1305");
  37115. counter++;
  37116. return EXPECT_RESULT();
  37117. }
  37118. #endif
  37119. static int test_wolfSSL_curves_mismatch(void)
  37120. {
  37121. EXPECT_DECLS;
  37122. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  37123. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  37124. test_ssl_cbf func_cb_client;
  37125. test_ssl_cbf func_cb_server;
  37126. size_t i;
  37127. struct {
  37128. method_provider client_meth;
  37129. method_provider server_meth;
  37130. const char* desc;
  37131. int client_last_err;
  37132. int server_last_err;
  37133. } test_params[] = {
  37134. #ifdef WOLFSSL_TLS13
  37135. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  37136. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  37137. #endif
  37138. #ifndef WOLFSSL_NO_TLS12
  37139. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  37140. FATAL_ERROR, MATCH_SUITE_ERROR},
  37141. #endif
  37142. #ifndef NO_OLD_TLS
  37143. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  37144. FATAL_ERROR, MATCH_SUITE_ERROR},
  37145. #endif
  37146. };
  37147. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  37148. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  37149. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  37150. printf("\tTesting with %s...\n", test_params[i].desc);
  37151. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  37152. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  37153. func_cb_client.method = test_params[i].client_meth;
  37154. func_cb_server.method = test_params[i].server_meth;
  37155. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  37156. &func_cb_server, NULL), TEST_FAIL);
  37157. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  37158. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  37159. if (!EXPECT_SUCCESS())
  37160. break;
  37161. printf("\t%s passed\n", test_params[i].desc);
  37162. }
  37163. #endif
  37164. return EXPECT_RESULT();
  37165. }
  37166. static int test_wolfSSL_set1_sigalgs_list(void)
  37167. {
  37168. EXPECT_DECLS;
  37169. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37170. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  37171. SSL* ssl = NULL;
  37172. SSL_CTX* ctx = NULL;
  37173. #ifndef NO_WOLFSSL_SERVER
  37174. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37175. #else
  37176. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  37177. #endif
  37178. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  37179. SSL_FILETYPE_PEM));
  37180. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37181. ExpectNotNull(ssl = SSL_new(ctx));
  37182. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  37183. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  37184. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  37185. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  37186. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  37187. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  37188. #ifndef NO_RSA
  37189. #ifndef NO_SHA256
  37190. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  37191. WOLFSSL_FAILURE);
  37192. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  37193. WOLFSSL_FAILURE);
  37194. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  37195. WOLFSSL_SUCCESS);
  37196. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  37197. WOLFSSL_SUCCESS);
  37198. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  37199. WOLFSSL_FAILURE);
  37200. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  37201. WOLFSSL_FAILURE);
  37202. #ifdef WC_RSA_PSS
  37203. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  37204. WOLFSSL_SUCCESS);
  37205. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  37206. WOLFSSL_SUCCESS);
  37207. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  37208. WOLFSSL_SUCCESS);
  37209. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  37210. WOLFSSL_SUCCESS);
  37211. #endif
  37212. #ifdef WOLFSSL_SHA512
  37213. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37214. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  37215. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37216. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  37217. #elif defined(WOLFSSL_SHA384)
  37218. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37219. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  37220. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37221. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  37222. #endif
  37223. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  37224. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  37225. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  37226. WOLFSSL_FAILURE);
  37227. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  37228. WOLFSSL_FAILURE);
  37229. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  37230. WOLFSSL_FAILURE);
  37231. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  37232. WOLFSSL_FAILURE);
  37233. #endif
  37234. #endif
  37235. #ifdef HAVE_ECC
  37236. #ifndef NO_SHA256
  37237. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  37238. WOLFSSL_SUCCESS);
  37239. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  37240. WOLFSSL_SUCCESS);
  37241. #ifdef WOLFSSL_SHA512
  37242. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37243. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  37244. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37245. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  37246. #elif defined(WOLFSSL_SHA384)
  37247. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  37248. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  37249. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  37250. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  37251. #endif
  37252. #endif
  37253. #endif
  37254. #ifdef HAVE_ED25519
  37255. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  37256. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  37257. #endif
  37258. #ifdef HAVE_ED448
  37259. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  37260. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  37261. #endif
  37262. #ifndef NO_DSA
  37263. #ifndef NO_SHA256
  37264. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  37265. WOLFSSL_SUCCESS);
  37266. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  37267. WOLFSSL_SUCCESS);
  37268. #endif
  37269. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  37270. defined(WOLFSSL_ALLOW_TLS_SHA1))
  37271. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  37272. WOLFSSL_SUCCESS);
  37273. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  37274. WOLFSSL_SUCCESS);
  37275. #endif
  37276. #endif
  37277. SSL_free(ssl);
  37278. SSL_CTX_free(ctx);
  37279. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  37280. #endif
  37281. return EXPECT_RESULT();
  37282. }
  37283. /* Testing wolfSSL_set_tlsext_status_type function.
  37284. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  37285. */
  37286. static int test_wolfSSL_set_tlsext_status_type(void)
  37287. {
  37288. EXPECT_DECLS;
  37289. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  37290. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  37291. SSL* ssl = NULL;
  37292. SSL_CTX* ctx = NULL;
  37293. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  37294. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  37295. SSL_FILETYPE_PEM));
  37296. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  37297. ExpectNotNull(ssl = SSL_new(ctx));
  37298. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  37299. SSL_SUCCESS);
  37300. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  37301. SSL_free(ssl);
  37302. SSL_CTX_free(ctx);
  37303. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  37304. return EXPECT_RESULT();
  37305. }
  37306. #ifndef NO_BIO
  37307. static int test_wolfSSL_PEM_read_bio(void)
  37308. {
  37309. EXPECT_DECLS;
  37310. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  37311. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37312. byte buff[6000];
  37313. XFILE f = XBADFILE;
  37314. int bytes;
  37315. X509* x509 = NULL;
  37316. BIO* bio = NULL;
  37317. BUF_MEM* buf = NULL;
  37318. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  37319. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  37320. if (f != XBADFILE)
  37321. XFCLOSE(f);
  37322. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  37323. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  37324. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  37325. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  37326. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  37327. /* BIO should return the set EOF value */
  37328. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  37329. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  37330. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  37331. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  37332. BIO_free(bio);
  37333. BUF_MEM_free(buf);
  37334. X509_free(x509);
  37335. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  37336. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  37337. return EXPECT_RESULT();
  37338. }
  37339. #if defined(OPENSSL_EXTRA)
  37340. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  37341. long argl, long ret)
  37342. {
  37343. (void)bio;
  37344. (void)cmd;
  37345. (void)argp;
  37346. (void)argi;
  37347. (void)argl;
  37348. return ret;
  37349. }
  37350. #endif
  37351. static int test_wolfSSL_BIO(void)
  37352. {
  37353. EXPECT_DECLS;
  37354. #if defined(OPENSSL_EXTRA)
  37355. const unsigned char* p = NULL;
  37356. byte buff[20];
  37357. BIO* bio1 = NULL;
  37358. BIO* bio2 = NULL;
  37359. BIO* bio3 = NULL;
  37360. char* bufPt = NULL;
  37361. int i;
  37362. for (i = 0; i < 20; i++) {
  37363. buff[i] = i;
  37364. }
  37365. /* test BIO_free with NULL */
  37366. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  37367. /* Creating and testing type BIO_s_bio */
  37368. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  37369. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  37370. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  37371. /* read/write before set up */
  37372. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  37373. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  37374. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  37375. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  37376. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  37377. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  37378. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  37379. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  37380. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  37381. /* write buffer full */
  37382. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  37383. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  37384. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  37385. /* write the other direction with pair */
  37386. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  37387. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  37388. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  37389. /* try read */
  37390. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  37391. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  37392. /* try read using ctrl function */
  37393. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  37394. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  37395. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  37396. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  37397. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  37398. for (i = 0; i < 20; i++) {
  37399. ExpectIntEQ((int)bufPt[i], i);
  37400. }
  37401. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  37402. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  37403. for (i = 0; i < 8; i++) {
  37404. ExpectIntEQ((int)bufPt[i], i);
  37405. }
  37406. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  37407. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  37408. /* new pair */
  37409. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  37410. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  37411. bio2 = NULL;
  37412. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  37413. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  37414. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  37415. /* test wrap around... */
  37416. ExpectIntEQ(BIO_reset(bio1), 0);
  37417. ExpectIntEQ(BIO_reset(bio3), 0);
  37418. /* fill write buffer, read only small amount then write again */
  37419. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37420. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37421. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  37422. for (i = 0; i < 4; i++) {
  37423. ExpectIntEQ(bufPt[i], i);
  37424. }
  37425. /* try writing over read index */
  37426. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  37427. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  37428. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  37429. /* read and write 0 bytes */
  37430. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  37431. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  37432. /* should read only to end of write buffer then need to read again */
  37433. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  37434. for (i = 0; i < 16; i++) {
  37435. ExpectIntEQ(bufPt[i], buff[4 + i]);
  37436. }
  37437. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  37438. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  37439. for (i = 0; i < 4; i++) {
  37440. ExpectIntEQ(bufPt[i], 0);
  37441. }
  37442. /* read index should not have advanced with nread0 */
  37443. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  37444. for (i = 0; i < 4; i++) {
  37445. ExpectIntEQ(bufPt[i], 0);
  37446. }
  37447. /* write and fill up buffer checking reset of index state */
  37448. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37449. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37450. /* test reset on data in bio1 write buffer */
  37451. ExpectIntEQ(BIO_reset(bio1), 0);
  37452. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  37453. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  37454. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  37455. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  37456. ExpectNotNull(p);
  37457. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  37458. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  37459. for (i = 0; i < 6; i++) {
  37460. ExpectIntEQ(bufPt[i], i);
  37461. }
  37462. /* test case of writing twice with offset read index */
  37463. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  37464. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  37465. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37466. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  37467. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37468. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  37469. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  37470. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  37471. BIO_free(bio1);
  37472. bio1 = NULL;
  37473. BIO_free(bio3);
  37474. bio3 = NULL;
  37475. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  37476. {
  37477. BIO* bioA = NULL;
  37478. BIO* bioB = NULL;
  37479. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  37480. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  37481. BIO_free(bioA);
  37482. bioA = NULL;
  37483. BIO_free(bioB);
  37484. bioB = NULL;
  37485. }
  37486. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  37487. /* BIOs with file pointers */
  37488. #if !defined(NO_FILESYSTEM)
  37489. {
  37490. XFILE f1 = XBADFILE;
  37491. XFILE f2 = XBADFILE;
  37492. BIO* f_bio1 = NULL;
  37493. BIO* f_bio2 = NULL;
  37494. unsigned char cert[300];
  37495. char testFile[] = "tests/bio_write_test.txt";
  37496. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  37497. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  37498. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  37499. /* Failure due to wrong BIO type */
  37500. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  37501. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  37502. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  37503. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  37504. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  37505. WOLFSSL_SUCCESS);
  37506. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  37507. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  37508. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  37509. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  37510. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  37511. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  37512. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  37513. ExpectIntEQ(BIO_reset(f_bio2), 0);
  37514. ExpectIntEQ(BIO_tell(NULL),-1);
  37515. ExpectIntEQ(BIO_tell(f_bio2),0);
  37516. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  37517. ExpectIntEQ(BIO_tell(f_bio2),4);
  37518. BIO_free(f_bio1);
  37519. f_bio1 = NULL;
  37520. BIO_free(f_bio2);
  37521. f_bio2 = NULL;
  37522. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  37523. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  37524. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  37525. BIO_free(f_bio1);
  37526. f_bio1 = NULL;
  37527. }
  37528. #endif /* !defined(NO_FILESYSTEM) */
  37529. /* BIO info callback */
  37530. {
  37531. const char* testArg = "test";
  37532. BIO* cb_bio = NULL;
  37533. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  37534. BIO_set_callback(cb_bio, bioCallback);
  37535. ExpectNotNull(BIO_get_callback(cb_bio));
  37536. BIO_set_callback(cb_bio, NULL);
  37537. ExpectNull(BIO_get_callback(cb_bio));
  37538. BIO_set_callback_arg(cb_bio, (char*)testArg);
  37539. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  37540. ExpectNull(BIO_get_callback_arg(NULL));
  37541. BIO_free(cb_bio);
  37542. cb_bio = NULL;
  37543. }
  37544. /* BIO_vfree */
  37545. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  37546. BIO_vfree(NULL);
  37547. BIO_vfree(bio1);
  37548. #endif
  37549. return EXPECT_RESULT();
  37550. }
  37551. static int test_wolfSSL_BIO_BIO_ring_read(void)
  37552. {
  37553. EXPECT_DECLS;
  37554. #if defined(OPENSSL_ALL)
  37555. BIO* bio1 = NULL;
  37556. BIO* bio2 = NULL;
  37557. byte data[50];
  37558. byte tmp[50];
  37559. XMEMSET(data, 42, sizeof(data));
  37560. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  37561. SSL_SUCCESS);
  37562. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  37563. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  37564. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  37565. ExpectBufEQ(tmp, data, 20);
  37566. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  37567. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  37568. ExpectBufEQ(tmp, data, 40);
  37569. BIO_free(bio1);
  37570. BIO_free(bio2);
  37571. #endif
  37572. return EXPECT_RESULT();
  37573. }
  37574. #endif /* !NO_BIO */
  37575. static int test_wolfSSL_a2i_IPADDRESS(void)
  37576. {
  37577. EXPECT_DECLS;
  37578. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  37579. const unsigned char* data = NULL;
  37580. int dataSz = 0;
  37581. ASN1_OCTET_STRING *st = NULL;
  37582. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  37583. const unsigned char ipv6_exp[] = {
  37584. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  37585. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  37586. };
  37587. const unsigned char ipv6_home[] = {
  37588. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37589. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  37590. };
  37591. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  37592. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  37593. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37594. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  37595. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  37596. ASN1_STRING_free(st);
  37597. st = NULL;
  37598. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  37599. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37600. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  37601. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  37602. ASN1_STRING_free(st);
  37603. st = NULL;
  37604. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  37605. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  37606. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  37607. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  37608. ASN1_STRING_free(st);
  37609. #endif
  37610. return EXPECT_RESULT();
  37611. }
  37612. static int test_wolfSSL_X509_cmp_time(void)
  37613. {
  37614. EXPECT_DECLS;
  37615. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  37616. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  37617. WOLFSSL_ASN1_TIME asn_time;
  37618. time_t t;
  37619. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  37620. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  37621. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  37622. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  37623. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  37624. #endif
  37625. return EXPECT_RESULT();
  37626. }
  37627. static int test_wolfSSL_X509_time_adj(void)
  37628. {
  37629. EXPECT_DECLS;
  37630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  37631. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  37632. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  37633. !defined(NO_ASN_TIME)
  37634. X509* x509 = NULL;
  37635. time_t t;
  37636. time_t not_before;
  37637. time_t not_after;
  37638. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37639. client_cert_der_2048, sizeof_client_cert_der_2048,
  37640. WOLFSSL_FILETYPE_ASN1));
  37641. t = 0;
  37642. not_before = wc_Time(0);
  37643. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  37644. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  37645. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  37646. /* Check X509_gmtime_adj, too. */
  37647. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  37648. X509_free(x509);
  37649. #endif
  37650. return EXPECT_RESULT();
  37651. }
  37652. static int test_wolfSSL_X509_bad_altname(void)
  37653. {
  37654. EXPECT_DECLS;
  37655. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37656. const unsigned char malformed_alt_name_cert[] = {
  37657. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  37658. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  37659. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  37660. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  37661. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  37662. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  37663. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  37664. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  37665. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  37666. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  37667. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  37668. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  37669. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  37670. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  37671. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  37672. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  37673. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  37674. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  37675. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  37676. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  37677. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  37678. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  37679. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  37680. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  37681. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  37682. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  37683. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  37684. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  37685. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  37686. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  37687. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  37688. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  37689. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  37690. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  37691. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  37692. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  37693. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  37694. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  37695. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  37696. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  37697. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  37698. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  37699. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  37700. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  37701. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  37702. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  37703. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  37704. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  37705. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  37706. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  37707. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  37708. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  37709. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  37710. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  37711. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  37712. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  37713. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  37714. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  37715. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  37716. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  37717. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  37718. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  37719. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  37720. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  37721. };
  37722. X509* x509 = NULL;
  37723. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  37724. const char *name = "aaaaa";
  37725. int nameLen = (int)XSTRLEN(name);
  37726. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37727. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  37728. /* malformed_alt_name_cert has a malformed alternative
  37729. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  37730. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  37731. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  37732. X509_free(x509);
  37733. #endif
  37734. return EXPECT_RESULT();
  37735. }
  37736. static int test_wolfSSL_X509_name_match(void)
  37737. {
  37738. EXPECT_DECLS;
  37739. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37740. /* A certificate with the subject alternative name a* */
  37741. const unsigned char cert_der[] = {
  37742. 0x30, 0x82, 0x03, 0xac, 0x30, 0x82, 0x02, 0x94, 0xa0, 0x03, 0x02, 0x01,
  37743. 0x02, 0x02, 0x14, 0x0f, 0xa5, 0x10, 0x85, 0xef, 0x58, 0x10, 0x59, 0xfc,
  37744. 0x0f, 0x20, 0x1f, 0x53, 0xf5, 0x30, 0x39, 0x34, 0x49, 0x54, 0x05, 0x30,
  37745. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  37746. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  37747. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37748. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  37749. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  37750. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  37751. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  37752. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  37753. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  37754. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  37755. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  37756. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  37757. 0x30, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  37758. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x31, 0x35, 0x35, 0x38, 0x5a, 0x30,
  37759. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  37760. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  37761. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  37762. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  37763. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  37764. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  37765. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  37766. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  37767. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  37768. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  37769. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  37770. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  37771. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xf4, 0xca, 0x3d,
  37772. 0xd4, 0xbc, 0x9b, 0xea, 0x74, 0xfe, 0x73, 0xf4, 0x16, 0x23, 0x0b, 0x4a,
  37773. 0x09, 0x54, 0xf6, 0x7b, 0x10, 0x99, 0x11, 0x93, 0xb2, 0xdb, 0x4d, 0x7d,
  37774. 0x23, 0xab, 0xf9, 0xcd, 0xf6, 0x54, 0xd4, 0xf6, 0x39, 0x57, 0xee, 0x97,
  37775. 0xb2, 0xb9, 0xfc, 0x7e, 0x9c, 0xb3, 0xfb, 0x56, 0xb6, 0x84, 0xd6, 0x2d,
  37776. 0x59, 0x1c, 0xed, 0xda, 0x9b, 0x19, 0xf5, 0x8a, 0xa7, 0x8a, 0x89, 0xd6,
  37777. 0xa1, 0xc0, 0xe6, 0x16, 0xad, 0x04, 0xcf, 0x5a, 0x1f, 0xdf, 0x62, 0x6c,
  37778. 0x68, 0x45, 0xe9, 0x55, 0x2e, 0x42, 0xa3, 0x1b, 0x3b, 0x86, 0x23, 0x22,
  37779. 0xa1, 0x20, 0x48, 0xd1, 0x52, 0xc0, 0x8b, 0xab, 0xe2, 0x8a, 0x15, 0x68,
  37780. 0xbd, 0x89, 0x6f, 0x9f, 0x45, 0x75, 0xb4, 0x27, 0xc1, 0x72, 0x41, 0xfd,
  37781. 0x79, 0x89, 0xb0, 0x74, 0xa2, 0xe9, 0x61, 0x48, 0x4c, 0x54, 0xad, 0x6b,
  37782. 0x61, 0xbf, 0x0e, 0x27, 0x58, 0xb4, 0xf6, 0x9c, 0x2c, 0x9f, 0xc2, 0x3e,
  37783. 0x3b, 0xb3, 0x90, 0x41, 0xbc, 0x61, 0xcd, 0x01, 0x57, 0x90, 0x82, 0xec,
  37784. 0x46, 0xba, 0x4f, 0x89, 0x8e, 0x7f, 0x49, 0x4f, 0x46, 0x69, 0x37, 0x8b,
  37785. 0xa0, 0xba, 0x85, 0xe8, 0x42, 0xff, 0x9a, 0xa1, 0x53, 0x81, 0x5c, 0xf3,
  37786. 0x8e, 0x85, 0x1c, 0xd4, 0x90, 0x60, 0xa0, 0x37, 0x59, 0x04, 0x65, 0xa6,
  37787. 0xb5, 0x12, 0x00, 0xc3, 0x04, 0x51, 0xa7, 0x83, 0x96, 0x62, 0x3d, 0x49,
  37788. 0x97, 0xe8, 0x6b, 0x9a, 0x5d, 0x51, 0x24, 0xee, 0xad, 0x45, 0x18, 0x0f,
  37789. 0x3f, 0x97, 0xec, 0xdf, 0xcf, 0x42, 0x8a, 0x96, 0xc7, 0xd8, 0x82, 0x87,
  37790. 0x7f, 0x57, 0x70, 0x22, 0xfb, 0x29, 0x3e, 0x3c, 0xa3, 0xc1, 0xd5, 0x71,
  37791. 0xb3, 0x84, 0x06, 0x53, 0xa3, 0x86, 0x20, 0x35, 0xe3, 0x41, 0xb9, 0xd8,
  37792. 0x00, 0x22, 0x4f, 0x6d, 0xe6, 0xfd, 0xf0, 0xf4, 0xa2, 0x39, 0x0a, 0x1a,
  37793. 0x23, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x30, 0x30, 0x2e, 0x30, 0x0d,
  37794. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x06, 0x30, 0x04, 0x82, 0x02, 0x61,
  37795. 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  37796. 0x45, 0x05, 0xf3, 0x4d, 0x3e, 0x7e, 0x9c, 0xf5, 0x08, 0xee, 0x2c, 0x13,
  37797. 0x32, 0xe3, 0xf2, 0x14, 0xe8, 0x0e, 0x71, 0x21, 0x30, 0x0d, 0x06, 0x09,
  37798. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  37799. 0x82, 0x01, 0x01, 0x00, 0xa8, 0x28, 0xe5, 0x22, 0x65, 0xcf, 0x47, 0xfe,
  37800. 0x82, 0x17, 0x99, 0x20, 0xdb, 0xb1, 0x57, 0xd4, 0x91, 0x1a, 0x83, 0xde,
  37801. 0xc1, 0xaf, 0xc4, 0x1f, 0xfb, 0xa4, 0x6a, 0xad, 0xdc, 0x58, 0x72, 0xd9,
  37802. 0x9b, 0xab, 0xa5, 0xbb, 0xf4, 0x98, 0xd4, 0xdf, 0x36, 0xcb, 0xb5, 0x78,
  37803. 0xce, 0x4b, 0x25, 0x5b, 0x24, 0x92, 0xfe, 0xe8, 0xd4, 0xe4, 0xbd, 0x6f,
  37804. 0x71, 0x1a, 0x81, 0x2a, 0x6f, 0x35, 0x93, 0xf7, 0xcc, 0xed, 0xe5, 0x06,
  37805. 0xd2, 0x96, 0x41, 0xb5, 0xa9, 0x8a, 0xc0, 0xc9, 0x17, 0xe3, 0x13, 0x5e,
  37806. 0x94, 0x5e, 0xfa, 0xfc, 0xf0, 0x00, 0x2e, 0xe1, 0xd8, 0x1b, 0x23, 0x3f,
  37807. 0x7c, 0x4d, 0x9f, 0xfb, 0xb7, 0x95, 0xc1, 0x94, 0x7f, 0x7f, 0xb5, 0x4f,
  37808. 0x93, 0x6d, 0xc3, 0x2b, 0xb2, 0x28, 0x36, 0xd2, 0x7c, 0x01, 0x3c, 0xae,
  37809. 0x35, 0xdb, 0xc8, 0x95, 0x1b, 0x5f, 0x6c, 0x0f, 0x57, 0xb3, 0xcc, 0x97,
  37810. 0x98, 0x80, 0x06, 0xaa, 0xe4, 0x93, 0x1f, 0xb7, 0xa0, 0x54, 0xf1, 0x4f,
  37811. 0x6f, 0x11, 0xdf, 0xab, 0xd3, 0xbf, 0xf0, 0x3a, 0x81, 0x60, 0xaf, 0x7a,
  37812. 0xf7, 0x09, 0xd5, 0xae, 0x0c, 0x7d, 0xae, 0x8d, 0x47, 0x06, 0xbe, 0x11,
  37813. 0x6e, 0xf8, 0x7e, 0x49, 0xf8, 0xac, 0x24, 0x0a, 0x4b, 0xc2, 0xf6, 0xe8,
  37814. 0x2c, 0xec, 0x35, 0xef, 0xa9, 0x13, 0xb8, 0xd2, 0x9c, 0x92, 0x61, 0x91,
  37815. 0xec, 0x7b, 0x0c, 0xea, 0x9a, 0x71, 0x36, 0x15, 0x34, 0x2b, 0x7a, 0x25,
  37816. 0xac, 0xfe, 0xc7, 0x26, 0x89, 0x70, 0x3e, 0x64, 0x68, 0x97, 0x4b, 0xaa,
  37817. 0xc1, 0x24, 0x14, 0xbd, 0x45, 0x2f, 0xe0, 0xfe, 0xf4, 0x2b, 0x8e, 0x08,
  37818. 0x3e, 0xe4, 0xb5, 0x3d, 0x5d, 0xf4, 0xc3, 0xd6, 0x9c, 0xb5, 0x33, 0x1b,
  37819. 0x3b, 0xda, 0x6e, 0x99, 0x7b, 0x09, 0xd1, 0x30, 0x97, 0x23, 0x52, 0x6d,
  37820. 0x1b, 0x71, 0x3a, 0xf4, 0x54, 0xf0, 0xe5, 0x9e
  37821. };
  37822. WOLFSSL_X509* x509 = NULL;
  37823. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  37824. const char *name1 = "aaaaa";
  37825. int nameLen1 = (int)(XSTRLEN(name1));
  37826. const char *name2 = "a";
  37827. int nameLen2 = (int)(XSTRLEN(name2));
  37828. const char *name3 = "abbbb";
  37829. int nameLen3 = (int)(XSTRLEN(name3));
  37830. const char *name4 = "bbb";
  37831. int nameLen4 = (int)(XSTRLEN(name4));
  37832. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37833. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  37834. /* Ensure that "a*" matches "aaaaa" */
  37835. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  37836. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37837. /* Ensure that "a*" matches "a" */
  37838. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  37839. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37840. /* Ensure that "a*" matches "abbbb" */
  37841. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  37842. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37843. /* Ensure that "a*" does not match "bbb" */
  37844. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  37845. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  37846. wolfSSL_X509_free(x509);
  37847. #endif
  37848. return EXPECT_RESULT();
  37849. }
  37850. static int test_wolfSSL_X509_name_match2(void)
  37851. {
  37852. EXPECT_DECLS;
  37853. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37854. /* A certificate with the subject alternative name a*b* */
  37855. const unsigned char cert_der[] = {
  37856. 0x30, 0x82, 0x03, 0xae, 0x30, 0x82, 0x02, 0x96, 0xa0, 0x03, 0x02, 0x01,
  37857. 0x02, 0x02, 0x14, 0x41, 0x8c, 0x8b, 0xaa, 0x0e, 0xd8, 0x5a, 0xc0, 0x52,
  37858. 0x46, 0x0e, 0xe5, 0xd8, 0xb9, 0x48, 0x93, 0x7e, 0x8a, 0x7c, 0x65, 0x30,
  37859. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  37860. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  37861. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37862. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  37863. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  37864. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  37865. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  37866. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  37867. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  37868. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  37869. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  37870. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  37871. 0x30, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  37872. 0x30, 0x35, 0x32, 0x38, 0x32, 0x30, 0x34, 0x33, 0x34, 0x30, 0x5a, 0x30,
  37873. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  37874. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  37875. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  37876. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  37877. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  37878. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  37879. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  37880. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  37881. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  37882. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  37883. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  37884. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  37885. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa5, 0x60, 0x80,
  37886. 0xf3, 0xee, 0x19, 0xd2, 0xe4, 0x15, 0x94, 0x54, 0x12, 0x88, 0xee, 0xda,
  37887. 0x11, 0x11, 0x87, 0x99, 0x88, 0xb3, 0x71, 0xc7, 0x97, 0x78, 0x1b, 0x57,
  37888. 0x37, 0x1d, 0x0b, 0x1f, 0x2f, 0x2c, 0x35, 0x13, 0x75, 0xd3, 0x31, 0x3e,
  37889. 0x6f, 0x80, 0x21, 0xa5, 0xa3, 0xad, 0x10, 0x81, 0xb6, 0x37, 0xd4, 0x55,
  37890. 0x2e, 0xc1, 0xb8, 0x37, 0xa3, 0x3c, 0xe8, 0x81, 0x03, 0x3c, 0xda, 0x5f,
  37891. 0x6f, 0x45, 0x32, 0x2b, 0x0e, 0x99, 0x27, 0xfd, 0xe5, 0x6c, 0x07, 0xd9,
  37892. 0x4e, 0x0a, 0x8b, 0x23, 0x74, 0x96, 0x25, 0x97, 0xae, 0x6d, 0x19, 0xba,
  37893. 0xbf, 0x0f, 0xc8, 0xa1, 0xe5, 0xea, 0xa8, 0x00, 0x09, 0xc3, 0x9a, 0xef,
  37894. 0x09, 0x33, 0xc1, 0x33, 0x2e, 0x7b, 0x6d, 0xa7, 0x66, 0x87, 0xb6, 0x3a,
  37895. 0xb9, 0xdb, 0x4c, 0x5e, 0xb5, 0x55, 0x69, 0x37, 0x17, 0x92, 0x1f, 0xe3,
  37896. 0x53, 0x1a, 0x2d, 0x25, 0xd0, 0xcf, 0x72, 0x37, 0xc2, 0x89, 0x83, 0x78,
  37897. 0xcf, 0xac, 0x2e, 0x46, 0x92, 0x5c, 0x4a, 0xba, 0x7d, 0xa0, 0x22, 0x34,
  37898. 0xb1, 0x22, 0x26, 0x99, 0xda, 0xe8, 0x97, 0xe2, 0x0c, 0xd3, 0xbc, 0x97,
  37899. 0x7e, 0xa8, 0xb9, 0xe3, 0xe2, 0x7f, 0x56, 0xef, 0x22, 0xee, 0x15, 0x95,
  37900. 0xa6, 0xd1, 0xf4, 0xa7, 0xac, 0x4a, 0xab, 0xc1, 0x1a, 0xda, 0xc5, 0x5f,
  37901. 0xa5, 0x5e, 0x2f, 0x15, 0x9c, 0x36, 0xbe, 0xd3, 0x47, 0xb6, 0x86, 0xb9,
  37902. 0xc6, 0x59, 0x39, 0x36, 0xad, 0x84, 0x53, 0x95, 0x72, 0x91, 0x89, 0x51,
  37903. 0x32, 0x77, 0xf1, 0xa5, 0x93, 0xfe, 0xf0, 0x41, 0x7c, 0x64, 0xf1, 0xb0,
  37904. 0x8b, 0x81, 0x8d, 0x3a, 0x2c, 0x9e, 0xbe, 0x2e, 0x8b, 0xf7, 0x80, 0x63,
  37905. 0x35, 0x32, 0xfa, 0x26, 0xe0, 0x63, 0xbf, 0x5e, 0xaf, 0xf0, 0x08, 0xe0,
  37906. 0x80, 0x65, 0x38, 0xfa, 0x21, 0xaa, 0x91, 0x34, 0x48, 0x3d, 0x32, 0x5c,
  37907. 0xbf, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x32, 0x30, 0x30, 0x30, 0x0f,
  37908. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82, 0x04, 0x61,
  37909. 0x2a, 0x62, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16,
  37910. 0x04, 0x14, 0x3d, 0x55, 0x74, 0xf8, 0x3a, 0x26, 0x03, 0x8c, 0x6a, 0x2e,
  37911. 0x91, 0x0e, 0x18, 0x70, 0xb4, 0xa4, 0xcc, 0x04, 0x00, 0xd3, 0x30, 0x0d,
  37912. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  37913. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x8f, 0x3b, 0xff, 0x46, 0x0c, 0xb5,
  37914. 0x21, 0xdc, 0xcf, 0x61, 0x9a, 0x25, 0x93, 0x99, 0x68, 0x2f, 0x16, 0x71,
  37915. 0x15, 0x00, 0x5f, 0xb0, 0x9b, 0x43, 0x5c, 0x47, 0xe2, 0x8e, 0xc8, 0xea,
  37916. 0xb3, 0x30, 0x4d, 0x87, 0x90, 0xcf, 0x24, 0x37, 0x5c, 0xfd, 0xc8, 0xc6,
  37917. 0x09, 0x36, 0xb2, 0xfb, 0xfd, 0xc1, 0x82, 0x92, 0x77, 0x5b, 0x9d, 0xeb,
  37918. 0xac, 0x47, 0xbc, 0xda, 0x7c, 0x89, 0x19, 0x03, 0x9e, 0xcd, 0x96, 0x2a,
  37919. 0x90, 0x55, 0x23, 0x19, 0xac, 0x9d, 0x49, 0xfb, 0xa0, 0x31, 0x7d, 0x6b,
  37920. 0x1a, 0x16, 0x13, 0xb1, 0xa9, 0xc9, 0xc4, 0xaf, 0xf1, 0xb4, 0xa7, 0x9b,
  37921. 0x08, 0x64, 0x6a, 0x09, 0xcd, 0x4a, 0x03, 0x4c, 0x93, 0xb6, 0xcf, 0x29,
  37922. 0xdb, 0x56, 0x88, 0x8e, 0xed, 0x08, 0x6d, 0x8d, 0x76, 0xa3, 0xd7, 0xc6,
  37923. 0x69, 0xa1, 0xf5, 0xd2, 0xd0, 0x0a, 0x4b, 0xfa, 0x88, 0x66, 0x6c, 0xe5,
  37924. 0x4a, 0xee, 0x13, 0xad, 0xad, 0x22, 0x25, 0x73, 0x39, 0x56, 0x74, 0x0e,
  37925. 0xda, 0xcd, 0x35, 0x67, 0xe3, 0x81, 0x5c, 0xc5, 0xae, 0x3c, 0x4f, 0x47,
  37926. 0x3e, 0x97, 0xde, 0xac, 0xf6, 0xe1, 0x26, 0xe2, 0xe0, 0x66, 0x48, 0x20,
  37927. 0x7c, 0x02, 0x81, 0x3e, 0x7d, 0x34, 0xb7, 0x73, 0x3e, 0x2e, 0xd6, 0x20,
  37928. 0x1c, 0xdf, 0xf1, 0xae, 0x86, 0x8b, 0xb2, 0xc2, 0x9b, 0x68, 0x9c, 0xf6,
  37929. 0x1a, 0x5e, 0x30, 0x06, 0x39, 0x0a, 0x1f, 0x7b, 0xd7, 0x18, 0x4b, 0x06,
  37930. 0x9d, 0xff, 0x84, 0x57, 0xcc, 0x92, 0xad, 0x81, 0x0a, 0x19, 0x11, 0xc4,
  37931. 0xac, 0x59, 0x00, 0xe8, 0x5a, 0x70, 0x78, 0xd6, 0x9f, 0xe0, 0x82, 0x2a,
  37932. 0x1f, 0x09, 0x36, 0x1c, 0x52, 0x98, 0xf7, 0x95, 0x8f, 0xf9, 0x48, 0x4f,
  37933. 0x30, 0x52, 0xb5, 0xf3, 0x8d, 0x13, 0x93, 0x27, 0xbe, 0xb4, 0x75, 0x39,
  37934. 0x65, 0xc6, 0x48, 0x4e, 0x32, 0xd7, 0xf4, 0xc3, 0x26, 0x8d
  37935. };
  37936. WOLFSSL_X509* x509 = NULL;
  37937. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  37938. const char *name1 = "ab";
  37939. int nameLen1 = (int)(XSTRLEN(name1));
  37940. const char *name2 = "acccbccc";
  37941. int nameLen2 = (int)(XSTRLEN(name2));
  37942. const char *name3 = "accb";
  37943. int nameLen3 = (int)(XSTRLEN(name3));
  37944. const char *name4 = "accda";
  37945. int nameLen4 = (int)(XSTRLEN(name4));
  37946. const char *name5 = "acc\0bcc";
  37947. int nameLen5 = 7;
  37948. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  37949. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  37950. /* Ensure that "a*b*" matches "ab" */
  37951. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  37952. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37953. /* Ensure that "a*b*" matches "acccbccc" */
  37954. ExpectIntEQ(wolfSSL_X509_check_host(x509, name2, nameLen2,
  37955. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37956. /* Ensure that "a*b*" matches "accb" */
  37957. ExpectIntEQ(wolfSSL_X509_check_host(x509, name3, nameLen3,
  37958. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37959. /* Ensure that "a*b*" does not match "accda" */
  37960. ExpectIntNE(wolfSSL_X509_check_host(x509, name4, nameLen4,
  37961. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37962. /* Ensure that "a*b*" matches "ab", testing openssl behavior replication
  37963. * on check len input handling, 0 for len is OK as it should then use
  37964. * strlen(name1) */
  37965. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, 0,
  37966. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37967. /* Openssl also allows for len to include NULL terminator */
  37968. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1 + 1,
  37969. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37970. /* Ensure that check string with NULL terminator in middle is
  37971. * rejected */
  37972. ExpectIntNE(wolfSSL_X509_check_host(x509, name5, nameLen5,
  37973. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  37974. wolfSSL_X509_free(x509);
  37975. #endif
  37976. return EXPECT_RESULT();
  37977. }
  37978. static int test_wolfSSL_X509_name_match3(void)
  37979. {
  37980. EXPECT_DECLS;
  37981. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37982. /* A certificate with the subject alternative name *.example.com */
  37983. const unsigned char cert_der[] = {
  37984. 0x30, 0x82, 0x03, 0xb7, 0x30, 0x82, 0x02, 0x9f, 0xa0, 0x03, 0x02, 0x01,
  37985. 0x02, 0x02, 0x14, 0x59, 0xbb, 0xf6, 0xde, 0xb8, 0x3d, 0x0e, 0x8c, 0xe4,
  37986. 0xbd, 0x98, 0xa3, 0xbe, 0x3e, 0x8f, 0xdc, 0xbd, 0x7f, 0xcc, 0xae, 0x30,
  37987. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  37988. 0x05, 0x00, 0x30, 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04,
  37989. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  37990. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31,
  37991. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  37992. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55,
  37993. 0x04, 0x0a, 0x0c, 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  37994. 0x49, 0x6e, 0x63, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  37995. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e,
  37996. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  37997. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e,
  37998. 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x34, 0x30, 0x35, 0x33,
  37999. 0x31, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x17, 0x0d, 0x33, 0x34,
  38000. 0x30, 0x35, 0x32, 0x39, 0x30, 0x30, 0x33, 0x37, 0x34, 0x39, 0x5a, 0x30,
  38001. 0x77, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  38002. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c,
  38003. 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e,
  38004. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d,
  38005. 0x61, 0x6e, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  38006. 0x0b, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x49, 0x6e, 0x63,
  38007. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  38008. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18,
  38009. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  38010. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  38011. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  38012. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00,
  38013. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xda, 0x78, 0x16,
  38014. 0x05, 0x65, 0xf2, 0x85, 0xf2, 0x61, 0x7f, 0xb1, 0x4d, 0x73, 0xe2, 0x82,
  38015. 0xb5, 0x3d, 0xf7, 0x9d, 0x05, 0x65, 0xed, 0x9d, 0xc3, 0x29, 0x7a, 0x92,
  38016. 0x2c, 0x06, 0x5f, 0xc8, 0x13, 0x55, 0x42, 0x4e, 0xbd, 0xe2, 0x56, 0x2a,
  38017. 0x4b, 0xac, 0xe6, 0x1b, 0x10, 0xc9, 0xdb, 0x9a, 0x45, 0x36, 0xed, 0xf3,
  38018. 0x26, 0x8c, 0x22, 0x88, 0x1e, 0x6d, 0x2b, 0x41, 0xfa, 0x0d, 0x43, 0x88,
  38019. 0x88, 0xde, 0x8d, 0x2e, 0xca, 0x6e, 0x7c, 0x62, 0x66, 0x3e, 0xfa, 0x4e,
  38020. 0x71, 0xea, 0x7d, 0x3b, 0x32, 0x33, 0x5c, 0x7a, 0x7e, 0xea, 0x74, 0xbd,
  38021. 0xb6, 0x8f, 0x4c, 0x1c, 0x7a, 0x79, 0x94, 0xf1, 0xe8, 0x02, 0x67, 0x98,
  38022. 0x25, 0xb4, 0x31, 0x80, 0xc1, 0xae, 0xbf, 0xef, 0xf2, 0x6c, 0x78, 0x42,
  38023. 0xef, 0xb5, 0xc6, 0x01, 0x47, 0x79, 0x8d, 0x92, 0xce, 0xc1, 0xb5, 0x98,
  38024. 0x76, 0xf0, 0x84, 0xa2, 0x53, 0x90, 0xe5, 0x39, 0xc7, 0xbd, 0xf2, 0xbb,
  38025. 0xe3, 0x3f, 0x00, 0xf6, 0xf0, 0x46, 0x86, 0xee, 0x55, 0xbd, 0x2c, 0x1f,
  38026. 0x97, 0x24, 0x7c, 0xbc, 0xda, 0x2f, 0x1b, 0x53, 0xef, 0x26, 0x56, 0xcc,
  38027. 0xb7, 0xd8, 0xca, 0x17, 0x20, 0x4e, 0x62, 0x03, 0x66, 0x32, 0xb3, 0xd1,
  38028. 0x71, 0x26, 0x6c, 0xff, 0xd1, 0x9e, 0x44, 0x86, 0x2a, 0xae, 0xba, 0x43,
  38029. 0x00, 0x13, 0x7e, 0x50, 0xdd, 0x3e, 0x27, 0x39, 0x70, 0x1c, 0x0c, 0x0b,
  38030. 0xe8, 0xa2, 0xae, 0x03, 0x09, 0x2e, 0xd8, 0x71, 0xee, 0x7b, 0x1a, 0x09,
  38031. 0x2d, 0xe1, 0xd5, 0xde, 0xf5, 0xa3, 0x36, 0x77, 0x90, 0x97, 0x99, 0xd7,
  38032. 0x6c, 0xb7, 0x5c, 0x9d, 0xf7, 0x7e, 0x41, 0x89, 0xfe, 0xe4, 0x08, 0xc6,
  38033. 0x0b, 0xe4, 0x9b, 0x5f, 0x51, 0xa6, 0x08, 0xb8, 0x99, 0x81, 0xe9, 0xce,
  38034. 0xb4, 0x2d, 0xb2, 0x92, 0x9f, 0xe5, 0x1a, 0x98, 0x76, 0x20, 0x70, 0x54,
  38035. 0x93, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x3b, 0x30, 0x39, 0x30, 0x18,
  38036. 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x11, 0x30, 0x0f, 0x82, 0x0d, 0x2a,
  38037. 0x2e, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d,
  38038. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x60,
  38039. 0xd4, 0x26, 0xbb, 0xcc, 0x7c, 0x29, 0xa2, 0x88, 0x3c, 0x76, 0x7d, 0xb4,
  38040. 0x86, 0x8b, 0x47, 0x64, 0x5b, 0x87, 0xe0, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  38041. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  38042. 0x01, 0x01, 0x00, 0xc3, 0x0d, 0x03, 0x67, 0xbb, 0x47, 0x8b, 0xf3, 0x20,
  38043. 0xdc, 0x7d, 0x2e, 0xe1, 0xd9, 0xf0, 0x01, 0xc4, 0x66, 0xc2, 0xe1, 0xcd,
  38044. 0xc3, 0x4a, 0x72, 0xf0, 0x6e, 0x38, 0xcf, 0x63, 0x01, 0x96, 0x9e, 0x84,
  38045. 0xb9, 0xce, 0x1d, 0xba, 0x4b, 0xe0, 0x70, 0x86, 0x2b, 0x5a, 0xab, 0xec,
  38046. 0xbf, 0xc2, 0xaa, 0x64, 0xa2, 0x6c, 0xd2, 0x42, 0x52, 0xd4, 0xbe, 0x8a,
  38047. 0xca, 0x9c, 0x03, 0xf3, 0xd6, 0x5f, 0xcd, 0x23, 0x9f, 0xf5, 0xa9, 0x04,
  38048. 0x40, 0x5b, 0x66, 0x78, 0xc0, 0xac, 0xa1, 0xdb, 0x5d, 0xd1, 0x94, 0xfc,
  38049. 0x47, 0x94, 0xf5, 0x45, 0xe3, 0x70, 0x13, 0x3f, 0x66, 0x6d, 0xdd, 0x73,
  38050. 0x68, 0x68, 0xe2, 0xd2, 0x89, 0xcb, 0x7f, 0xc6, 0xca, 0xd6, 0x96, 0x0b,
  38051. 0xcc, 0xdd, 0xa1, 0x74, 0xda, 0x33, 0xe8, 0x9e, 0xda, 0xb7, 0xd9, 0x12,
  38052. 0xab, 0x85, 0x9d, 0x0c, 0xde, 0xa0, 0x7d, 0x7e, 0xa1, 0x91, 0xed, 0xe5,
  38053. 0x32, 0x7c, 0xc5, 0xea, 0x1d, 0x4a, 0xb5, 0x38, 0x63, 0x17, 0xf3, 0x4f,
  38054. 0x2c, 0x4a, 0x58, 0x86, 0x09, 0x33, 0x86, 0xc4, 0xe7, 0x56, 0x6f, 0x32,
  38055. 0x71, 0xb7, 0xd0, 0x83, 0x12, 0x9e, 0x26, 0x0a, 0x3a, 0x45, 0xcb, 0xd7,
  38056. 0x4e, 0xab, 0xa4, 0xc3, 0xee, 0x4c, 0xc0, 0x38, 0xa1, 0xfa, 0xba, 0xfa,
  38057. 0xb7, 0x80, 0x69, 0x67, 0xa3, 0xef, 0x89, 0xba, 0xce, 0x89, 0x91, 0x3d,
  38058. 0x6a, 0x76, 0xe9, 0x3b, 0x32, 0x86, 0x76, 0x85, 0x6b, 0x4f, 0x7f, 0xbc,
  38059. 0x7a, 0x5b, 0x31, 0x92, 0x79, 0x35, 0xf8, 0xb9, 0xb1, 0xd7, 0xdb, 0xa9,
  38060. 0x6a, 0x8a, 0x91, 0x60, 0x65, 0xd4, 0x76, 0x54, 0x55, 0x57, 0xb9, 0x35,
  38061. 0xe0, 0xf5, 0xbb, 0x8f, 0xd4, 0x40, 0x75, 0xbb, 0x47, 0xa8, 0xf9, 0x0f,
  38062. 0xea, 0xc9, 0x6e, 0x84, 0xd5, 0xf5, 0x58, 0x2d, 0xe5, 0x76, 0x7b, 0xdf,
  38063. 0x97, 0x05, 0x5e, 0xaf, 0x50, 0xf5, 0x48
  38064. };
  38065. WOLFSSL_X509* x509 = NULL;
  38066. int certSize = (int)(sizeof(cert_der) / sizeof(unsigned char));
  38067. const char *name1 = "foo.example.com";
  38068. int nameLen1 = (int)(XSTRLEN(name1));
  38069. const char *name2 = "x.y.example.com";
  38070. int nameLen2 = (int)(XSTRLEN(name2));
  38071. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  38072. cert_der, certSize, WOLFSSL_FILETYPE_ASN1));
  38073. /* Ensure that "*.example.com" matches "foo.example.com" */
  38074. ExpectIntEQ(wolfSSL_X509_check_host(x509, name1, nameLen1,
  38075. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38076. /* Ensure that "*.example.com" does NOT match "x.y.example.com" */
  38077. ExpectIntNE(wolfSSL_X509_check_host(x509, name2, nameLen2,
  38078. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), WOLFSSL_SUCCESS);
  38079. wolfSSL_X509_free(x509);
  38080. #endif
  38081. return EXPECT_RESULT();
  38082. }
  38083. static int test_wolfSSL_X509_max_altnames(void)
  38084. {
  38085. EXPECT_DECLS;
  38086. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  38087. /* Only test if max alt names has not been modified */
  38088. #if WOLFSSL_MAX_ALT_NAMES == 128
  38089. WOLFSSL_CTX* ctx = NULL;
  38090. /* File contains a certificate encoded with 130 subject alternative names */
  38091. const char* over_max_altnames_cert = \
  38092. "./certs/test/cert-over-max-altnames.pem";
  38093. #ifndef NO_WOLFSSL_SERVER
  38094. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  38095. #else
  38096. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38097. #endif
  38098. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx,
  38099. over_max_altnames_cert, NULL, WOLFSSL_LOAD_FLAG_NONE),
  38100. WOLFSSL_SUCCESS);
  38101. wolfSSL_CTX_free(ctx);
  38102. #endif
  38103. #endif
  38104. return EXPECT_RESULT();
  38105. }
  38106. static int test_wolfSSL_X509_max_name_constraints(void)
  38107. {
  38108. EXPECT_DECLS;
  38109. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  38110. !defined(IGNORE_NAME_CONSTRAINTS)
  38111. /* Only test if max name constraints has not been modified */
  38112. #if WOLFSSL_MAX_NAME_CONSTRAINTS == 128
  38113. WOLFSSL_CTX* ctx = NULL;
  38114. /* File contains a certificate with 130 name constraints */
  38115. const char* over_max_nc = "./certs/test/cert-over-max-nc.pem";
  38116. #ifndef NO_WOLFSSL_SERVER
  38117. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  38118. #else
  38119. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  38120. #endif
  38121. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, over_max_nc,
  38122. NULL, WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  38123. wolfSSL_CTX_free(ctx);
  38124. #endif
  38125. #endif
  38126. return EXPECT_RESULT();
  38127. }
  38128. static int test_wolfSSL_X509(void)
  38129. {
  38130. EXPECT_DECLS;
  38131. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38132. !defined(NO_RSA)
  38133. X509* x509 = NULL;
  38134. #ifndef NO_BIO
  38135. BIO* bio = NULL;
  38136. X509_STORE_CTX* ctx = NULL;
  38137. X509_STORE* store = NULL;
  38138. #endif
  38139. char der[] = "certs/ca-cert.der";
  38140. XFILE fp = XBADFILE;
  38141. ExpectNotNull(x509 = X509_new());
  38142. X509_free(x509);
  38143. x509 = NULL;
  38144. #ifndef NO_BIO
  38145. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  38146. SSL_FILETYPE_PEM));
  38147. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  38148. #ifdef WOLFSSL_CERT_GEN
  38149. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  38150. #endif
  38151. ExpectNotNull(ctx = X509_STORE_CTX_new());
  38152. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  38153. ExpectNotNull(store = X509_STORE_new());
  38154. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  38155. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  38156. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  38157. X509_STORE_CTX_free(ctx);
  38158. X509_STORE_free(store);
  38159. X509_free(x509);
  38160. x509 = NULL;
  38161. BIO_free(bio);
  38162. #endif
  38163. /** d2i_X509_fp test **/
  38164. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  38165. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  38166. ExpectNotNull(x509);
  38167. X509_free(x509);
  38168. x509 = NULL;
  38169. if (fp != XBADFILE) {
  38170. XFCLOSE(fp);
  38171. fp = XBADFILE;
  38172. }
  38173. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  38174. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  38175. ExpectNotNull(x509);
  38176. X509_free(x509);
  38177. if (fp != XBADFILE)
  38178. XFCLOSE(fp);
  38179. /* X509_up_ref test */
  38180. ExpectIntEQ(X509_up_ref(NULL), 0);
  38181. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  38182. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  38183. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  38184. X509_free(x509); /* refCount = 2 */
  38185. X509_free(x509); /* refCount = 1 */
  38186. X509_free(x509); /* refCount = 0, free */
  38187. #endif
  38188. return EXPECT_RESULT();
  38189. }
  38190. static int test_wolfSSL_X509_get_ext_count(void)
  38191. {
  38192. EXPECT_DECLS;
  38193. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  38194. !defined(NO_RSA)
  38195. int ret = 0;
  38196. WOLFSSL_X509* x509 = NULL;
  38197. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  38198. XFILE f = XBADFILE;
  38199. /* NULL parameter check */
  38200. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  38201. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  38202. SSL_FILETYPE_PEM));
  38203. ExpectIntEQ(X509_get_ext_count(x509), 5);
  38204. wolfSSL_X509_free(x509);
  38205. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  38206. SSL_FILETYPE_PEM));
  38207. ExpectIntEQ(X509_get_ext_count(x509), 5);
  38208. wolfSSL_X509_free(x509);
  38209. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  38210. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  38211. if (f != XBADFILE)
  38212. XFCLOSE(f);
  38213. /* wolfSSL_X509_get_ext_count() valid input */
  38214. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  38215. /* wolfSSL_X509_get_ext_count() NULL argument */
  38216. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  38217. wolfSSL_X509_free(x509);
  38218. #endif
  38219. return EXPECT_RESULT();
  38220. }
  38221. static int test_wolfSSL_X509_sign2(void)
  38222. {
  38223. EXPECT_DECLS;
  38224. /* test requires WOLFSSL_AKID_NAME to match expected output */
  38225. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  38226. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  38227. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  38228. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  38229. defined(WOLFSSL_IP_ALT_NAME))
  38230. WOLFSSL_X509 *x509 = NULL;
  38231. WOLFSSL_X509 *ca = NULL;
  38232. const unsigned char *der = NULL;
  38233. const unsigned char *pt = NULL;
  38234. WOLFSSL_EVP_PKEY *priv = NULL;
  38235. WOLFSSL_X509_NAME *name = NULL;
  38236. int derSz;
  38237. #ifndef NO_ASN_TIME
  38238. WOLFSSL_ASN1_TIME *notBefore = NULL;
  38239. WOLFSSL_ASN1_TIME *notAfter = NULL;
  38240. const int year = 365*24*60*60;
  38241. const int day = 24*60*60;
  38242. const int hour = 60*60;
  38243. const int mini = 60;
  38244. time_t t;
  38245. #endif
  38246. const unsigned char expected[] = {
  38247. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  38248. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  38249. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  38250. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  38251. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  38252. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  38253. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  38254. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  38255. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  38256. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  38257. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  38258. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  38259. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  38260. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  38261. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38262. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  38263. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  38264. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  38265. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  38266. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  38267. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  38268. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  38269. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  38270. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  38271. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  38272. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  38273. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  38274. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  38275. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  38276. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  38277. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  38278. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  38279. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  38280. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  38281. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  38282. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  38283. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  38284. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  38285. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  38286. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  38287. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  38288. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  38289. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  38290. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  38291. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  38292. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  38293. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  38294. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  38295. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  38296. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  38297. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  38298. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  38299. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  38300. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  38301. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  38302. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  38303. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  38304. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  38305. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  38306. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  38307. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  38308. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  38309. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  38310. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  38311. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  38312. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  38313. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  38314. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  38315. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  38316. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  38317. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  38318. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  38319. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  38320. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  38321. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  38322. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  38323. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  38324. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  38325. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  38326. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  38327. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  38328. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  38329. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  38330. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  38331. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  38332. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  38333. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  38334. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  38335. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  38336. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  38337. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  38338. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  38339. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  38340. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  38341. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  38342. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  38343. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  38344. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  38345. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  38346. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  38347. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  38348. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  38349. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  38350. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  38351. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  38352. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  38353. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  38354. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  38355. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  38356. };
  38357. pt = ca_key_der_2048;
  38358. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  38359. sizeof_ca_key_der_2048));
  38360. pt = client_cert_der_2048;
  38361. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  38362. sizeof_client_cert_der_2048));
  38363. pt = ca_cert_der_2048;
  38364. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  38365. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  38366. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  38367. #ifndef NO_ASN_TIME
  38368. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  38369. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  38370. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  38371. ExpectIntEQ(notAfter->length, 13);
  38372. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  38373. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  38374. #endif
  38375. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  38376. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  38377. ExpectIntEQ(derSz, sizeof(expected));
  38378. #ifndef NO_ASN_TIME
  38379. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  38380. #endif
  38381. wolfSSL_X509_free(ca);
  38382. wolfSSL_X509_free(x509);
  38383. wolfSSL_EVP_PKEY_free(priv);
  38384. #ifndef NO_ASN_TIME
  38385. wolfSSL_ASN1_TIME_free(notBefore);
  38386. wolfSSL_ASN1_TIME_free(notAfter);
  38387. #endif
  38388. #endif
  38389. return EXPECT_RESULT();
  38390. }
  38391. static int test_wolfSSL_X509_sign(void)
  38392. {
  38393. EXPECT_DECLS;
  38394. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  38395. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  38396. int ret = 0;
  38397. char *cn = NULL;
  38398. word32 cnSz = 0;
  38399. X509_NAME *name = NULL;
  38400. X509 *x509 = NULL;
  38401. X509 *ca = NULL;
  38402. DecodedCert dCert;
  38403. EVP_PKEY *pub = NULL;
  38404. EVP_PKEY *priv = NULL;
  38405. EVP_MD_CTX *mctx = NULL;
  38406. #if defined(USE_CERT_BUFFERS_1024)
  38407. const unsigned char* rsaPriv = client_key_der_1024;
  38408. const unsigned char* rsaPub = client_keypub_der_1024;
  38409. const unsigned char* certIssuer = client_cert_der_1024;
  38410. long clientKeySz = (long)sizeof_client_key_der_1024;
  38411. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  38412. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  38413. #elif defined(USE_CERT_BUFFERS_2048)
  38414. const unsigned char* rsaPriv = client_key_der_2048;
  38415. const unsigned char* rsaPub = client_keypub_der_2048;
  38416. const unsigned char* certIssuer = client_cert_der_2048;
  38417. long clientKeySz = (long)sizeof_client_key_der_2048;
  38418. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  38419. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  38420. #endif
  38421. byte sn[16];
  38422. int snSz = sizeof(sn);
  38423. /* Set X509_NAME fields */
  38424. ExpectNotNull(name = X509_NAME_new());
  38425. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  38426. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  38427. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  38428. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  38429. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  38430. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  38431. /* Get private and public keys */
  38432. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  38433. clientKeySz));
  38434. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  38435. ExpectNotNull(x509 = X509_new());
  38436. /* Set version 3 */
  38437. ExpectIntNE(X509_set_version(x509, 2L), 0);
  38438. /* Set subject name, add pubkey, and sign certificate */
  38439. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  38440. X509_NAME_free(name);
  38441. name = NULL;
  38442. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  38443. #ifdef WOLFSSL_ALT_NAMES
  38444. /* Add some subject alt names */
  38445. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  38446. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  38447. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38448. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  38449. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38450. "sphygmomanometer",
  38451. ASN_DNS_TYPE), SSL_SUCCESS);
  38452. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38453. "supercalifragilisticexpialidocious",
  38454. ASN_DNS_TYPE), SSL_SUCCESS);
  38455. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  38456. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  38457. ASN_DNS_TYPE), SSL_SUCCESS);
  38458. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38459. {
  38460. unsigned char ip4_type[] = {127,128,0,255};
  38461. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  38462. 0xff, 0xee, 0x99, 0x88,
  38463. 0x77, 0x66, 0x55, 0x44,
  38464. 0x00, 0x33, 0x22, 0x11};
  38465. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  38466. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  38467. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  38468. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  38469. }
  38470. #endif
  38471. #endif /* WOLFSSL_ALT_NAMES */
  38472. {
  38473. ASN1_UTCTIME* infinite_past = NULL;
  38474. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  38475. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  38476. ASN1_UTCTIME_free(infinite_past);
  38477. }
  38478. /* test valid sign case */
  38479. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  38480. /* test valid X509_sign_ctx case */
  38481. ExpectNotNull(mctx = EVP_MD_CTX_new());
  38482. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  38483. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  38484. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  38485. ExpectIntEQ(X509_get_ext_count(x509), 1);
  38486. #endif
  38487. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  38488. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  38489. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  38490. #endif
  38491. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  38492. WOLFSSL_SUCCESS);
  38493. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  38494. /* Variation in size depends on ASN.1 encoding when MSB is set.
  38495. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  38496. * with the MSB set. See GenerateInteger in asn.c */
  38497. #ifndef USE_CERT_BUFFERS_1024
  38498. #ifndef WOLFSSL_ALT_NAMES
  38499. /* Valid case - size should be 781-786 with 16 byte serial number */
  38500. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  38501. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38502. /* Valid case - size should be 955-960 with 16 byte serial number */
  38503. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  38504. #else
  38505. /* Valid case - size should be 926-931 with 16 byte serial number */
  38506. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  38507. #endif
  38508. #else
  38509. #ifndef WOLFSSL_ALT_NAMES
  38510. /* Valid case - size should be 537-542 with 16 byte serial number */
  38511. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  38512. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  38513. /* Valid case - size should be 695-670 with 16 byte serial number */
  38514. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  38515. #else
  38516. /* Valid case - size should be 666-671 with 16 byte serial number */
  38517. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  38518. #endif
  38519. #endif
  38520. /* check that issuer name is as expected after signature */
  38521. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  38522. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  38523. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  38524. ExpectNotNull(name = X509_get_subject_name(ca));
  38525. cnSz = X509_NAME_get_sz(name);
  38526. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  38527. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  38528. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  38529. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  38530. cn = NULL;
  38531. #ifdef WOLFSSL_MULTI_ATTRIB
  38532. /* test adding multiple OU's to the signer */
  38533. ExpectNotNull(name = X509_get_subject_name(ca));
  38534. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  38535. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  38536. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  38537. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  38538. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  38539. #endif
  38540. ExpectNotNull(name = X509_get_subject_name(ca));
  38541. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  38542. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38543. ExpectNotNull(name = X509_get_issuer_name(x509));
  38544. cnSz = X509_NAME_get_sz(name);
  38545. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  38546. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  38547. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  38548. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  38549. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  38550. cn = NULL;
  38551. FreeDecodedCert(&dCert);
  38552. /* Test invalid parameters */
  38553. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  38554. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  38555. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  38556. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  38557. EVP_MD_CTX_free(mctx);
  38558. mctx = NULL;
  38559. ExpectNotNull(mctx = EVP_MD_CTX_new());
  38560. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  38561. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  38562. /* test invalid version number */
  38563. #if defined(OPENSSL_ALL)
  38564. ExpectIntNE(X509_set_version(x509, 6L), 0);
  38565. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  38566. /* uses ParseCert which fails on bad version number */
  38567. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  38568. #endif
  38569. EVP_MD_CTX_free(mctx);
  38570. EVP_PKEY_free(priv);
  38571. EVP_PKEY_free(pub);
  38572. X509_free(x509);
  38573. X509_free(ca);
  38574. #endif
  38575. return EXPECT_RESULT();
  38576. }
  38577. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  38578. {
  38579. EXPECT_DECLS;
  38580. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  38581. X509* x509 = NULL;
  38582. const X509_ALGOR* alg;
  38583. ExpectNotNull(x509 = X509_new());
  38584. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  38585. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  38586. X509_free(x509);
  38587. #endif
  38588. return EXPECT_RESULT();
  38589. }
  38590. static int test_wolfSSL_X509_ALGOR_get0(void)
  38591. {
  38592. EXPECT_DECLS;
  38593. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38594. !defined(NO_SHA256) && !defined(NO_RSA)
  38595. X509* x509 = NULL;
  38596. const ASN1_OBJECT* obj = NULL;
  38597. const X509_ALGOR* alg = NULL;
  38598. int pptype = 0;
  38599. const void *ppval = NULL;
  38600. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  38601. SSL_FILETYPE_PEM));
  38602. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  38603. /* Invalid case */
  38604. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  38605. ExpectNull(obj);
  38606. /* Valid case */
  38607. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  38608. ExpectNotNull(obj);
  38609. ExpectNull(ppval);
  38610. ExpectIntNE(pptype, 0);
  38611. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  38612. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  38613. X509_free(x509);
  38614. #endif
  38615. return EXPECT_RESULT();
  38616. }
  38617. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  38618. {
  38619. EXPECT_DECLS;
  38620. #if defined(OPENSSL_EXTRA)
  38621. X509_VERIFY_PARAM *paramTo = NULL;
  38622. X509_VERIFY_PARAM *paramFrom = NULL;
  38623. char testIPv4[] = "127.0.0.1";
  38624. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  38625. char testhostName1[] = "foo.hoge.com";
  38626. char testhostName2[] = "foobar.hoge.com";
  38627. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  38628. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  38629. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  38630. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  38631. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  38632. (int)XSTRLEN(testhostName1)), 1);
  38633. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  38634. (int)XSTRLEN(testhostName1)));
  38635. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  38636. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  38637. ExpectIntEQ(0x01, paramFrom->hostFlags);
  38638. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  38639. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  38640. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  38641. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  38642. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  38643. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38644. /* null pointer */
  38645. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  38646. /* in the case of "from" null, returns success */
  38647. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  38648. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  38649. /* inherit flags test : VPARAM_DEFAULT */
  38650. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38651. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38652. (int)XSTRLEN(testhostName1)));
  38653. ExpectIntEQ(0x01, paramTo->hostFlags);
  38654. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38655. /* inherit flags test : VPARAM OVERWRITE */
  38656. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38657. (int)XSTRLEN(testhostName2)), 1);
  38658. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38659. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  38660. if (paramTo != NULL) {
  38661. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  38662. }
  38663. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38664. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38665. (int)XSTRLEN(testhostName1)));
  38666. ExpectIntEQ(0x01, paramTo->hostFlags);
  38667. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38668. /* inherit flags test : VPARAM_RESET_FLAGS */
  38669. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38670. (int)XSTRLEN(testhostName2)), 1);
  38671. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38672. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  38673. if (paramTo != NULL) {
  38674. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  38675. }
  38676. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38677. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  38678. (int)XSTRLEN(testhostName1)));
  38679. ExpectIntEQ(0x01, paramTo->hostFlags);
  38680. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  38681. /* inherit flags test : VPARAM_LOCKED */
  38682. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  38683. (int)XSTRLEN(testhostName2)), 1);
  38684. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  38685. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  38686. if (paramTo != NULL) {
  38687. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  38688. }
  38689. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  38690. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  38691. (int)XSTRLEN(testhostName2)));
  38692. ExpectIntEQ(0x00, paramTo->hostFlags);
  38693. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  38694. /* test for incorrect parameters */
  38695. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  38696. 0);
  38697. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  38698. /* inherit flags test : VPARAM_ONCE, not testable yet */
  38699. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  38700. 1);
  38701. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  38702. X509_V_FLAG_CRL_CHECK_ALL);
  38703. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  38704. X509_V_FLAG_CRL_CHECK_ALL), 1);
  38705. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  38706. X509_VERIFY_PARAM_free(paramTo);
  38707. X509_VERIFY_PARAM_free(paramFrom);
  38708. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  38709. #endif
  38710. return EXPECT_RESULT();
  38711. }
  38712. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  38713. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  38714. static int test_wolfSSL_check_domain_verify_count = 0;
  38715. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  38716. WOLFSSL_X509_STORE_CTX* store)
  38717. {
  38718. EXPECT_DECLS;
  38719. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  38720. ExpectIntEQ(preverify, 1);
  38721. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  38722. return EXPECT_SUCCESS();
  38723. }
  38724. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  38725. {
  38726. EXPECT_DECLS;
  38727. X509_VERIFY_PARAM *param = NULL;
  38728. ExpectNotNull(param = SSL_get0_param(ssl));
  38729. /* Domain check should only be done on the leaf cert */
  38730. X509_VERIFY_PARAM_set_hostflags(param,
  38731. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  38732. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  38733. "wolfSSL Server Chain", 0), 1);
  38734. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  38735. test_wolfSSL_check_domain_verify_cb);
  38736. return EXPECT_RESULT();
  38737. }
  38738. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  38739. {
  38740. EXPECT_DECLS;
  38741. /* Use a cert with different domains in chain */
  38742. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  38743. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  38744. return EXPECT_RESULT();
  38745. }
  38746. static int test_wolfSSL_check_domain(void)
  38747. {
  38748. EXPECT_DECLS;
  38749. test_ssl_cbf func_cb_client;
  38750. test_ssl_cbf func_cb_server;
  38751. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  38752. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  38753. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  38754. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  38755. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  38756. &func_cb_server, NULL), TEST_SUCCESS);
  38757. /* Should have been called once for each cert in sent chain */
  38758. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  38759. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  38760. #else
  38761. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  38762. #endif
  38763. return EXPECT_RESULT();
  38764. }
  38765. #else
  38766. static int test_wolfSSL_check_domain(void)
  38767. {
  38768. EXPECT_DECLS;
  38769. return EXPECT_RESULT();
  38770. }
  38771. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  38772. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  38773. {
  38774. EXPECT_DECLS;
  38775. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  38776. X509* x509 = NULL;
  38777. X509_PUBKEY* pubKey;
  38778. ExpectNotNull(x509 = X509_new());
  38779. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  38780. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  38781. X509_free(x509);
  38782. #endif
  38783. return EXPECT_RESULT();
  38784. }
  38785. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  38786. {
  38787. EXPECT_DECLS;
  38788. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  38789. !defined(NO_SHA256) && !defined(NO_RSA)
  38790. X509* x509 = NULL;
  38791. ASN1_OBJECT* obj = NULL;
  38792. const ASN1_OBJECT* pa_oid = NULL;
  38793. X509_PUBKEY* pubKey = NULL;
  38794. X509_PUBKEY* pubKey2 = NULL;
  38795. EVP_PKEY* evpKey = NULL;
  38796. const unsigned char *pk = NULL;
  38797. int ppklen;
  38798. int pptype;
  38799. X509_ALGOR *pa = NULL;
  38800. const void *pval;
  38801. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  38802. SSL_FILETYPE_PEM));
  38803. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  38804. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  38805. ExpectNotNull(pk);
  38806. ExpectNotNull(pa);
  38807. ExpectNotNull(pubKey);
  38808. ExpectIntGT(ppklen, 0);
  38809. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  38810. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  38811. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  38812. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  38813. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  38814. ExpectNotNull(pk);
  38815. ExpectNotNull(pa);
  38816. ExpectIntGT(ppklen, 0);
  38817. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38818. ExpectNotNull(pa_oid);
  38819. ExpectNull(pval);
  38820. ExpectIntEQ(pptype, V_ASN1_NULL);
  38821. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  38822. X509_PUBKEY_free(pubKey2);
  38823. X509_free(x509);
  38824. EVP_PKEY_free(evpKey);
  38825. #endif
  38826. return EXPECT_RESULT();
  38827. }
  38828. static int test_wolfSSL_X509_PUBKEY_EC(void)
  38829. {
  38830. EXPECT_DECLS;
  38831. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  38832. X509* x509 = NULL;
  38833. ASN1_OBJECT* obj = NULL;
  38834. ASN1_OBJECT* poid = NULL;
  38835. const ASN1_OBJECT* pa_oid = NULL;
  38836. X509_PUBKEY* pubKey = NULL;
  38837. X509_PUBKEY* pubKey2 = NULL;
  38838. EVP_PKEY* evpKey = NULL;
  38839. const unsigned char *pk = NULL;
  38840. int ppklen;
  38841. int pptype;
  38842. X509_ALGOR *pa = NULL;
  38843. const void *pval;
  38844. char buf[50];
  38845. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  38846. SSL_FILETYPE_PEM));
  38847. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  38848. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  38849. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  38850. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  38851. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  38852. ExpectNotNull(pk);
  38853. ExpectNotNull(pa);
  38854. ExpectIntGT(ppklen, 0);
  38855. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38856. ExpectNotNull(pa_oid);
  38857. ExpectNotNull(pval);
  38858. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  38859. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  38860. poid = (ASN1_OBJECT *)pval;
  38861. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  38862. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  38863. X509_PUBKEY_free(pubKey2);
  38864. X509_free(x509);
  38865. EVP_PKEY_free(evpKey);
  38866. #endif
  38867. return EXPECT_RESULT();
  38868. }
  38869. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  38870. {
  38871. EXPECT_DECLS;
  38872. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  38873. word32 bytes;
  38874. #ifdef USE_CERT_BUFFERS_1024
  38875. byte tmp[ONEK_BUF];
  38876. #elif defined(USE_CERT_BUFFERS_2048)
  38877. byte tmp[TWOK_BUF];
  38878. #else
  38879. byte tmp[TWOK_BUF];
  38880. #endif /* END USE_CERT_BUFFERS_1024 */
  38881. const unsigned char* dsaKeyDer = tmp;
  38882. ASN1_OBJECT* obj = NULL;
  38883. ASN1_STRING* str;
  38884. const ASN1_OBJECT* pa_oid = NULL;
  38885. X509_PUBKEY* pubKey = NULL;
  38886. EVP_PKEY* evpKey = NULL;
  38887. const unsigned char *pk = NULL;
  38888. int ppklen, pptype;
  38889. X509_ALGOR *pa = NULL;
  38890. const void *pval;
  38891. #ifdef USE_CERT_BUFFERS_1024
  38892. XMEMSET(tmp, 0, sizeof(tmp));
  38893. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  38894. bytes = sizeof_dsa_key_der_1024;
  38895. #elif defined(USE_CERT_BUFFERS_2048)
  38896. XMEMSET(tmp, 0, sizeof(tmp));
  38897. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  38898. bytes = sizeof_dsa_key_der_2048;
  38899. #else
  38900. {
  38901. XFILE fp = XBADFILE;
  38902. XMEMSET(tmp, 0, sizeof(tmp));
  38903. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  38904. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  38905. if (fp != XBADFILE)
  38906. XFCLOSE(fp);
  38907. }
  38908. #endif
  38909. /* Initialize pkey with der format dsa key */
  38910. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  38911. ExpectNotNull(pubKey = X509_PUBKEY_new());
  38912. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  38913. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  38914. ExpectNotNull(pk);
  38915. ExpectNotNull(pa);
  38916. ExpectIntGT(ppklen, 0);
  38917. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  38918. ExpectNotNull(pa_oid);
  38919. ExpectNotNull(pval);
  38920. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  38921. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  38922. str = (ASN1_STRING *)pval;
  38923. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  38924. #ifdef USE_CERT_BUFFERS_1024
  38925. ExpectIntEQ(ASN1_STRING_length(str), 291);
  38926. #else
  38927. ExpectIntEQ(ASN1_STRING_length(str), 549);
  38928. #endif /* END USE_CERT_BUFFERS_1024 */
  38929. X509_PUBKEY_free(pubKey);
  38930. EVP_PKEY_free(evpKey);
  38931. #endif
  38932. return EXPECT_RESULT();
  38933. }
  38934. static int test_wolfSSL_BUF(void)
  38935. {
  38936. EXPECT_DECLS;
  38937. #if defined(OPENSSL_EXTRA)
  38938. BUF_MEM* buf = NULL;
  38939. ExpectNotNull(buf = BUF_MEM_new());
  38940. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  38941. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  38942. BUF_MEM_free(buf);
  38943. #endif
  38944. return EXPECT_RESULT();
  38945. }
  38946. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  38947. static int stub_rand_seed(const void *buf, int num)
  38948. {
  38949. (void)buf;
  38950. (void)num;
  38951. return 123;
  38952. }
  38953. static int stub_rand_bytes(unsigned char *buf, int num)
  38954. {
  38955. (void)buf;
  38956. (void)num;
  38957. return 456;
  38958. }
  38959. static byte* was_stub_rand_cleanup_called(void)
  38960. {
  38961. static byte was_called = 0;
  38962. return &was_called;
  38963. }
  38964. static void stub_rand_cleanup(void)
  38965. {
  38966. byte* was_called = was_stub_rand_cleanup_called();
  38967. *was_called = 1;
  38968. return;
  38969. }
  38970. static byte* was_stub_rand_add_called(void)
  38971. {
  38972. static byte was_called = 0;
  38973. return &was_called;
  38974. }
  38975. static int stub_rand_add(const void *buf, int num, double entropy)
  38976. {
  38977. byte* was_called = was_stub_rand_add_called();
  38978. (void)buf;
  38979. (void)num;
  38980. (void)entropy;
  38981. *was_called = 1;
  38982. return 0;
  38983. }
  38984. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  38985. {
  38986. (void)buf;
  38987. (void)num;
  38988. return 9876;
  38989. }
  38990. static int stub_rand_status(void)
  38991. {
  38992. return 5432;
  38993. }
  38994. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  38995. static int test_wolfSSL_RAND_set_rand_method(void)
  38996. {
  38997. EXPECT_DECLS;
  38998. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  38999. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  39000. unsigned char* buf = NULL;
  39001. int num = 0;
  39002. double entropy = 0;
  39003. int ret;
  39004. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  39005. byte* was_add_called = was_stub_rand_add_called();
  39006. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  39007. DYNAMIC_TYPE_TMP_BUFFER));
  39008. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  39009. ExpectIntEQ(*was_cleanup_called, 0);
  39010. RAND_cleanup();
  39011. ExpectIntEQ(*was_cleanup_called, 0);
  39012. rand_methods.seed = &stub_rand_seed;
  39013. rand_methods.bytes = &stub_rand_bytes;
  39014. rand_methods.cleanup = &stub_rand_cleanup;
  39015. rand_methods.add = &stub_rand_add;
  39016. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  39017. rand_methods.status = &stub_rand_status;
  39018. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  39019. ExpectIntEQ(RAND_seed(buf, num), 123);
  39020. ExpectIntEQ(RAND_bytes(buf, num), 456);
  39021. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  39022. ExpectIntEQ(RAND_status(), 5432);
  39023. ExpectIntEQ(*was_add_called, 0);
  39024. /* The function pointer for RAND_add returns int, but RAND_add itself
  39025. * returns void. */
  39026. RAND_add(buf, num, entropy);
  39027. ExpectIntEQ(*was_add_called, 1);
  39028. was_add_called = 0;
  39029. ExpectIntEQ(*was_cleanup_called, 0);
  39030. RAND_cleanup();
  39031. ExpectIntEQ(*was_cleanup_called, 1);
  39032. *was_cleanup_called = 0;
  39033. ret = RAND_set_rand_method(NULL);
  39034. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  39035. ExpectIntNE(RAND_status(), 5432);
  39036. ExpectIntEQ(*was_cleanup_called, 0);
  39037. RAND_cleanup();
  39038. ExpectIntEQ(*was_cleanup_called, 0);
  39039. RAND_set_rand_method(NULL);
  39040. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39041. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  39042. return EXPECT_RESULT();
  39043. }
  39044. static int test_wolfSSL_RAND_bytes(void)
  39045. {
  39046. EXPECT_DECLS;
  39047. #if defined(OPENSSL_EXTRA)
  39048. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  39049. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  39050. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  39051. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  39052. int max_bufsize;
  39053. byte *my_buf = NULL;
  39054. /* sanity check */
  39055. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  39056. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  39057. max_bufsize = size4;
  39058. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  39059. DYNAMIC_TYPE_TMP_BUFFER));
  39060. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  39061. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  39062. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  39063. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  39064. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  39065. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  39066. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  39067. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39068. #endif
  39069. return EXPECT_RESULT();
  39070. }
  39071. static int test_wolfSSL_RAND(void)
  39072. {
  39073. EXPECT_DECLS;
  39074. #if defined(OPENSSL_EXTRA)
  39075. byte seed[16];
  39076. XMEMSET(seed, 0, sizeof(seed));
  39077. /* No global methods set. */
  39078. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  39079. ExpectIntEQ(RAND_poll(), 1);
  39080. RAND_cleanup();
  39081. ExpectIntEQ(RAND_egd(NULL), -1);
  39082. #ifndef NO_FILESYSTEM
  39083. {
  39084. char fname[100];
  39085. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  39086. ExpectIntEQ(RAND_write_file(NULL), 0);
  39087. }
  39088. #endif
  39089. #endif
  39090. return EXPECT_RESULT();
  39091. }
  39092. static int test_wolfSSL_PKCS8_Compat(void)
  39093. {
  39094. EXPECT_DECLS;
  39095. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  39096. !defined(NO_BIO)
  39097. PKCS8_PRIV_KEY_INFO* pt = NULL;
  39098. BIO* bio = NULL;
  39099. XFILE f = XBADFILE;
  39100. int bytes;
  39101. char pkcs8_buffer[512];
  39102. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  39103. EVP_PKEY *pkey = NULL;
  39104. #endif
  39105. /* file from wolfssl/certs/ directory */
  39106. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  39107. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  39108. 0);
  39109. if (f != XBADFILE)
  39110. XFCLOSE(f);
  39111. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39112. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  39113. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  39114. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  39115. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  39116. /* gets PKCS8 pointer to pkey */
  39117. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  39118. EVP_PKEY_free(pkey);
  39119. #endif
  39120. BIO_free(bio);
  39121. PKCS8_PRIV_KEY_INFO_free(pt);
  39122. #endif
  39123. return EXPECT_RESULT();
  39124. }
  39125. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39126. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  39127. {
  39128. (void)passwd;
  39129. (void)sz;
  39130. (void)rw;
  39131. (void)userdata;
  39132. return -1;
  39133. }
  39134. #endif
  39135. static int test_wolfSSL_PKCS8_d2i(void)
  39136. {
  39137. EXPECT_DECLS;
  39138. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  39139. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  39140. * requires a 12 byte password in FIPS mode. This test ends up
  39141. * trying to use an 8 byte password. */
  39142. #ifndef NO_FILESYSTEM
  39143. unsigned char pkcs8_buffer[2048];
  39144. const unsigned char* p = NULL;
  39145. int bytes = 0;
  39146. XFILE file = XBADFILE;
  39147. WOLFSSL_EVP_PKEY* pkey = NULL;
  39148. #ifndef NO_BIO
  39149. BIO* bio = NULL;
  39150. #if defined(OPENSSL_ALL) && \
  39151. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  39152. defined(HAVE_ECC)) && \
  39153. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39154. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  39155. #endif
  39156. #endif
  39157. #ifndef NO_RSA
  39158. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  39159. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  39160. #ifndef NO_DES3
  39161. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  39162. #endif
  39163. #endif /* NO_RSA */
  39164. #ifdef HAVE_ECC
  39165. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  39166. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  39167. #ifndef NO_DES3
  39168. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  39169. #endif
  39170. #endif /* HAVE_ECC */
  39171. #endif /* !NO_FILESYSTEM */
  39172. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  39173. #ifndef NO_RSA
  39174. #ifdef USE_CERT_BUFFERS_1024
  39175. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  39176. int rsaSz = sizeof_server_key_der_1024;
  39177. #else
  39178. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  39179. int rsaSz = sizeof_server_key_der_2048;
  39180. #endif
  39181. #endif
  39182. #ifdef HAVE_ECC
  39183. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  39184. int ecSz = sizeof_ecc_key_der_256;
  39185. #endif
  39186. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  39187. #ifndef NO_FILESYSTEM
  39188. (void)pkcs8_buffer;
  39189. (void)p;
  39190. (void)bytes;
  39191. (void)file;
  39192. #ifndef NO_BIO
  39193. (void)bio;
  39194. #endif
  39195. #endif
  39196. #ifdef OPENSSL_ALL
  39197. #ifndef NO_RSA
  39198. /* Try to auto-detect normal RSA private key */
  39199. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  39200. EVP_PKEY_free(pkey);
  39201. pkey = NULL;
  39202. #endif
  39203. #ifdef HAVE_ECC
  39204. /* Try to auto-detect normal EC private key */
  39205. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  39206. EVP_PKEY_free(pkey);
  39207. pkey = NULL;
  39208. #endif
  39209. #endif /* OPENSSL_ALL */
  39210. #ifndef NO_FILESYSTEM
  39211. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39212. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  39213. NULL), 0);
  39214. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  39215. NULL), 0);
  39216. #endif
  39217. #ifndef NO_RSA
  39218. /* Get DER encoded RSA PKCS#8 data. */
  39219. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  39220. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39221. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39222. file)), 0);
  39223. if (file != XBADFILE) {
  39224. XFCLOSE(file);
  39225. file = XBADFILE;
  39226. }
  39227. p = pkcs8_buffer;
  39228. #ifdef OPENSSL_ALL
  39229. /* Try to decode - auto-detect key type. */
  39230. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  39231. #else
  39232. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  39233. #endif
  39234. /* Get PEM encoded RSA PKCS#8 data. */
  39235. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  39236. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39237. file)), 0);
  39238. if (file != XBADFILE) {
  39239. XFCLOSE(file);
  39240. file = XBADFILE;
  39241. }
  39242. #if defined(OPENSSL_ALL) && \
  39243. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39244. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39245. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  39246. NULL), 0);
  39247. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  39248. NULL), 0);
  39249. /* Write PKCS#8 PEM to BIO. */
  39250. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  39251. NULL), bytes);
  39252. /* Write PKCS#8 PEM to stderr. */
  39253. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  39254. NULL), bytes);
  39255. /* Compare file and written data */
  39256. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  39257. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  39258. BIO_free(bio);
  39259. bio = NULL;
  39260. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  39261. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  39262. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  39263. #endif
  39264. #if !defined(NO_DES3) && !defined(NO_SHA)
  39265. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39266. /* Write Encrypted PKCS#8 PEM to BIO. */
  39267. bytes = 1834;
  39268. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  39269. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39270. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  39271. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39272. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  39273. (void*)"yassl123"));
  39274. EVP_PKEY_free(evpPkey);
  39275. evpPkey = NULL;
  39276. BIO_free(bio);
  39277. bio = NULL;
  39278. #endif /* !NO_DES3 && !NO_SHA */
  39279. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39280. EVP_PKEY_free(pkey);
  39281. pkey = NULL;
  39282. /* PKCS#8 encrypted RSA key */
  39283. #ifndef NO_DES3
  39284. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  39285. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39286. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39287. file)), 0);
  39288. if (file != XBADFILE) {
  39289. XFCLOSE(file);
  39290. file = XBADFILE;
  39291. }
  39292. #if defined(OPENSSL_ALL) && \
  39293. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39294. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39295. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  39296. (void*)"yassl123"));
  39297. EVP_PKEY_free(pkey);
  39298. pkey = NULL;
  39299. BIO_free(bio);
  39300. bio = NULL;
  39301. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39302. #endif /* !NO_DES3 */
  39303. #endif /* NO_RSA */
  39304. #ifdef HAVE_ECC
  39305. /* PKCS#8 encode EC key */
  39306. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  39307. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39308. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39309. file)), 0);
  39310. if (file != XBADFILE) {
  39311. XFCLOSE(file);
  39312. file = XBADFILE;
  39313. }
  39314. p = pkcs8_buffer;
  39315. #ifdef OPENSSL_ALL
  39316. /* Try to decode - auto-detect key type. */
  39317. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  39318. #else
  39319. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  39320. #endif
  39321. /* Get PEM encoded RSA PKCS#8 data. */
  39322. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  39323. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39324. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39325. file)), 0);
  39326. if (file != XBADFILE) {
  39327. XFCLOSE(file);
  39328. file = XBADFILE;
  39329. }
  39330. #if defined(OPENSSL_ALL) && \
  39331. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  39332. defined(HAVE_AES_CBC)
  39333. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39334. /* Write PKCS#8 PEM to BIO. */
  39335. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  39336. NULL), bytes);
  39337. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  39338. NULL), bytes);
  39339. /* Compare file and written data */
  39340. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  39341. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  39342. BIO_free(bio);
  39343. bio = NULL;
  39344. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39345. /* Write Encrypted PKCS#8 PEM to BIO. */
  39346. bytes = 379;
  39347. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  39348. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  39349. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  39350. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39351. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  39352. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39353. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  39354. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  39355. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  39356. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  39357. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  39358. (void*)"yassl123"));
  39359. EVP_PKEY_free(evpPkey);
  39360. evpPkey = NULL;
  39361. BIO_free(bio);
  39362. bio = NULL;
  39363. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  39364. EVP_PKEY_free(pkey);
  39365. pkey = NULL;
  39366. /* PKCS#8 encrypted EC key */
  39367. #ifndef NO_DES3
  39368. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  39369. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  39370. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  39371. file)), 0);
  39372. if (file != XBADFILE) {
  39373. XFCLOSE(file);
  39374. file = XBADFILE;
  39375. }
  39376. #if defined(OPENSSL_ALL) && \
  39377. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  39378. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  39379. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  39380. (void*)"yassl123"));
  39381. EVP_PKEY_free(pkey);
  39382. pkey = NULL;
  39383. BIO_free(bio);
  39384. bio = NULL;
  39385. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  39386. #endif /* !NO_DES3 */
  39387. #endif /* HAVE_ECC */
  39388. #endif /* !NO_FILESYSTEM */
  39389. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  39390. return EXPECT_RESULT();
  39391. }
  39392. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  39393. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  39394. #define LOGGING_THREADS 5
  39395. #define ERROR_COUNT 10
  39396. /* copied from logging.c since this is not exposed otherwise */
  39397. #ifndef ERROR_QUEUE_MAX
  39398. #ifdef ERROR_QUEUE_PER_THREAD
  39399. #define ERROR_QUEUE_MAX 16
  39400. #else
  39401. /* this breaks from compat of unlimited error queue size */
  39402. #define ERROR_QUEUE_MAX 100
  39403. #endif
  39404. #endif
  39405. static volatile int loggingThreadsReady;
  39406. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  39407. {
  39408. const char* file;
  39409. int line;
  39410. unsigned long err;
  39411. int errorCount = 0;
  39412. int i;
  39413. (void)args;
  39414. while (!loggingThreadsReady);
  39415. for (i = 0; i < ERROR_COUNT; i++)
  39416. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  39417. while ((err = ERR_get_error_line(&file, &line))) {
  39418. AssertIntEQ(err, 990 + errorCount);
  39419. errorCount++;
  39420. }
  39421. AssertIntEQ(errorCount, ERROR_COUNT);
  39422. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  39423. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  39424. errorCount = 0;
  39425. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  39426. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  39427. while ((err = ERR_get_error_line(&file, &line))) {
  39428. AssertIntEQ(err, 990 + errorCount);
  39429. errorCount++;
  39430. }
  39431. /* test that the 3 errors over the max were dropped */
  39432. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  39433. WOLFSSL_RETURN_FROM_THREAD(0);
  39434. }
  39435. #endif
  39436. static int test_error_queue_per_thread(void)
  39437. {
  39438. int res = TEST_SKIPPED;
  39439. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  39440. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  39441. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  39442. int i;
  39443. ERR_clear_error(); /* clear out any error nodes */
  39444. loggingThreadsReady = 0;
  39445. for (i = 0; i < LOGGING_THREADS; i++)
  39446. start_thread(test_logging, NULL, &loggingThreads[i]);
  39447. loggingThreadsReady = 1;
  39448. for (i = 0; i < LOGGING_THREADS; i++)
  39449. join_thread(loggingThreads[i]);
  39450. res = TEST_SUCCESS;
  39451. #endif
  39452. return res;
  39453. }
  39454. static int test_wolfSSL_ERR_put_error(void)
  39455. {
  39456. EXPECT_DECLS;
  39457. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39458. defined(DEBUG_WOLFSSL)
  39459. const char* file;
  39460. int line;
  39461. ERR_clear_error(); /* clear out any error nodes */
  39462. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  39463. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39464. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  39465. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  39466. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  39467. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  39468. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  39469. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  39470. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  39471. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  39472. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  39473. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  39474. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  39475. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  39476. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  39477. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  39478. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  39479. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  39480. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  39481. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  39482. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  39483. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  39484. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  39485. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  39486. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  39487. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  39488. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  39489. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  39490. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  39491. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  39492. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  39493. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  39494. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  39495. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  39496. "this file", 100);
  39497. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  39498. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  39499. ExpectIntEQ(line, 100);
  39500. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  39501. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  39502. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  39503. #endif
  39504. /* try reading past end of error queue */
  39505. file = NULL;
  39506. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39507. ExpectNull(file);
  39508. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  39509. PEMerr(4,4);
  39510. ExpectIntEQ(ERR_get_error(), 4);
  39511. /* Empty and free up all error nodes */
  39512. ERR_clear_error();
  39513. /* Verify all nodes are cleared */
  39514. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  39515. ERR_clear_error();
  39516. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  39517. #endif
  39518. return EXPECT_RESULT();
  39519. }
  39520. /*
  39521. * This is a regression test for a bug where the peek/get error functions were
  39522. * drawing from the end of the queue rather than the front.
  39523. */
  39524. static int test_wolfSSL_ERR_get_error_order(void)
  39525. {
  39526. EXPECT_DECLS;
  39527. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  39528. /* Empty the queue. */
  39529. wolfSSL_ERR_clear_error();
  39530. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  39531. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  39532. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  39533. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  39534. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  39535. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  39536. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  39537. return EXPECT_RESULT();
  39538. }
  39539. #ifndef NO_BIO
  39540. static int test_wolfSSL_ERR_print_errors(void)
  39541. {
  39542. EXPECT_DECLS;
  39543. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39544. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  39545. BIO* bio = NULL;
  39546. char buf[1024];
  39547. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39548. ERR_clear_error(); /* clear out any error nodes */
  39549. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  39550. /* Choosing -600 as an unused errno. */
  39551. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  39552. ERR_print_errors(bio);
  39553. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  39554. ExpectIntEQ(XSTRNCMP(
  39555. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  39556. buf, 55), 0);
  39557. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  39558. ExpectIntEQ(XSTRNCMP(
  39559. "error:600:wolfSSL library:unknown error number:asn.c:100",
  39560. buf, 56), 0);
  39561. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  39562. ExpectIntEQ(buf[0], '\0');
  39563. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  39564. BIO_free(bio);
  39565. #endif
  39566. return EXPECT_RESULT();
  39567. }
  39568. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39569. defined(DEBUG_WOLFSSL)
  39570. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  39571. {
  39572. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  39573. return 0;
  39574. }
  39575. #endif
  39576. static int test_wolfSSL_ERR_print_errors_cb(void)
  39577. {
  39578. EXPECT_DECLS;
  39579. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  39580. defined(DEBUG_WOLFSSL)
  39581. BIO* bio = NULL;
  39582. char buf[1024];
  39583. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39584. ERR_clear_error(); /* clear out any error nodes */
  39585. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  39586. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  39587. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  39588. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  39589. ExpectIntEQ(XSTRNCMP(
  39590. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  39591. buf, 53), 0);
  39592. ExpectIntEQ(XSTRNCMP(
  39593. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  39594. buf + 53, 55), 0);
  39595. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  39596. BIO_free(bio);
  39597. #endif
  39598. return EXPECT_RESULT();
  39599. }
  39600. /*
  39601. * Testing WOLFSSL_ERROR_MSG
  39602. */
  39603. static int test_WOLFSSL_ERROR_MSG(void)
  39604. {
  39605. int res = TEST_SKIPPED;
  39606. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  39607. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  39608. const char* msg = TEST_STRING;
  39609. WOLFSSL_ERROR_MSG(msg);
  39610. res = TEST_SUCCESS;
  39611. #endif
  39612. return res;
  39613. } /* End test_WOLFSSL_ERROR_MSG */
  39614. /*
  39615. * Testing wc_ERR_remove_state
  39616. */
  39617. static int test_wc_ERR_remove_state(void)
  39618. {
  39619. int res = TEST_SKIPPED;
  39620. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  39621. wc_ERR_remove_state();
  39622. res = TEST_SUCCESS;
  39623. #endif
  39624. return res;
  39625. } /* End test_wc_ERR_remove_state */
  39626. /*
  39627. * Testing wc_ERR_print_errors_fp
  39628. */
  39629. static int test_wc_ERR_print_errors_fp(void)
  39630. {
  39631. EXPECT_DECLS;
  39632. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  39633. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  39634. long sz;
  39635. XFILE fp = XBADFILE;
  39636. WOLFSSL_ERROR(BAD_FUNC_ARG);
  39637. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  39638. XBADFILE);
  39639. wc_ERR_print_errors_fp(fp);
  39640. #if defined(DEBUG_WOLFSSL)
  39641. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  39642. #ifdef NO_ERROR_QUEUE
  39643. ExpectIntEQ(sz = XFTELL(fp), 0);
  39644. #else
  39645. ExpectIntNE(sz = XFTELL(fp), 0);
  39646. #endif
  39647. #endif
  39648. if (fp != XBADFILE)
  39649. XFCLOSE(fp);
  39650. (void)sz;
  39651. #endif
  39652. return EXPECT_RESULT();
  39653. } /* End test_wc_ERR_print_errors_fp */
  39654. #ifdef DEBUG_WOLFSSL
  39655. static void Logging_cb(const int logLevel, const char *const logMessage)
  39656. {
  39657. (void)logLevel;
  39658. (void)logMessage;
  39659. }
  39660. #endif
  39661. /*
  39662. * Testing wolfSSL_GetLoggingCb
  39663. */
  39664. static int test_wolfSSL_GetLoggingCb(void)
  39665. {
  39666. EXPECT_DECLS;
  39667. #ifdef DEBUG_WOLFSSL
  39668. /* Testing without wolfSSL_SetLoggingCb() */
  39669. ExpectNull(wolfSSL_GetLoggingCb());
  39670. /* Testing with wolfSSL_SetLoggingCb() */
  39671. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  39672. ExpectNotNull(wolfSSL_GetLoggingCb());
  39673. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  39674. #endif
  39675. ExpectNull(wolfSSL_GetLoggingCb());
  39676. return EXPECT_RESULT();
  39677. } /* End test_wolfSSL_GetLoggingCb */
  39678. #endif /* !NO_BIO */
  39679. static int test_wolfSSL_MD4(void)
  39680. {
  39681. EXPECT_DECLS;
  39682. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  39683. MD4_CTX md4;
  39684. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  39685. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  39686. "789012345678901234567890";
  39687. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  39688. "\xcc\x05\x36";
  39689. int msgSz = (int)XSTRLEN(msg);
  39690. XMEMSET(out, 0, sizeof(out));
  39691. MD4_Init(&md4);
  39692. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  39693. MD4_Final(out, &md4);
  39694. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  39695. #endif
  39696. return EXPECT_RESULT();
  39697. }
  39698. static int test_wolfSSL_MD5(void)
  39699. {
  39700. EXPECT_DECLS;
  39701. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39702. byte input1[] = "";
  39703. byte input2[] = "message digest";
  39704. byte hash[WC_MD5_DIGEST_SIZE];
  39705. unsigned char output1[] =
  39706. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  39707. unsigned char output2[] =
  39708. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  39709. WOLFSSL_MD5_CTX md5;
  39710. XMEMSET(&md5, 0, sizeof(md5));
  39711. /* Test cases for illegal parameters */
  39712. ExpectIntEQ(MD5_Init(NULL), 0);
  39713. ExpectIntEQ(MD5_Init(&md5), 1);
  39714. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  39715. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  39716. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  39717. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  39718. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  39719. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  39720. /* Init MD5 CTX */
  39721. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39722. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  39723. 1);
  39724. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39725. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39726. /* Init MD5 CTX */
  39727. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  39728. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  39729. (int)XSTRLEN((const char*)input2)), 1);
  39730. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  39731. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39732. #if !defined(NO_OLD_NAMES) && \
  39733. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  39734. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  39735. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  39736. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  39737. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  39738. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  39739. &hash);
  39740. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  39741. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  39742. &hash);
  39743. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  39744. {
  39745. byte data[] = "Data to be hashed.";
  39746. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  39747. ExpectNotNull(MD5(data, sizeof(data), NULL));
  39748. ExpectNotNull(MD5(data, sizeof(data), hash));
  39749. ExpectNotNull(MD5(NULL, 0, hash));
  39750. ExpectNull(MD5(NULL, sizeof(data), hash));
  39751. }
  39752. #endif
  39753. #endif
  39754. return EXPECT_RESULT();
  39755. }
  39756. static int test_wolfSSL_MD5_Transform(void)
  39757. {
  39758. EXPECT_DECLS;
  39759. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  39760. byte input1[] = "";
  39761. byte input2[] = "abc";
  39762. byte local[WC_MD5_BLOCK_SIZE];
  39763. word32 sLen = 0;
  39764. #ifdef BIG_ENDIAN_ORDER
  39765. unsigned char output1[] =
  39766. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  39767. unsigned char output2[] =
  39768. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  39769. #else
  39770. unsigned char output1[] =
  39771. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  39772. unsigned char output2[] =
  39773. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  39774. #endif
  39775. union {
  39776. wc_Md5 native;
  39777. MD5_CTX compat;
  39778. } md5;
  39779. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  39780. XMEMSET(&local, 0, sizeof(local));
  39781. /* sanity check */
  39782. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  39783. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  39784. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  39785. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  39786. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39787. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  39788. /* Init MD5 CTX */
  39789. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  39790. /* Do Transform*/
  39791. sLen = (word32)XSTRLEN((char*)input1);
  39792. XMEMCPY(local, input1, sLen);
  39793. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39794. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  39795. /* Init MD5 CTX */
  39796. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  39797. sLen = (word32)XSTRLEN((char*)input2);
  39798. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  39799. XMEMCPY(local, input2, sLen);
  39800. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  39801. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  39802. #endif
  39803. return EXPECT_RESULT();
  39804. }
  39805. static int test_wolfSSL_SHA(void)
  39806. {
  39807. EXPECT_DECLS;
  39808. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  39809. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  39810. (!defined(HAVE_FIPS) || \
  39811. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  39812. {
  39813. const unsigned char in[] = "abc";
  39814. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  39815. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  39816. unsigned char out[WC_SHA_DIGEST_SIZE];
  39817. unsigned char* p = NULL;
  39818. WOLFSSL_SHA_CTX sha;
  39819. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  39820. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  39821. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39822. /* SHA interface test */
  39823. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  39824. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  39825. ExpectNotNull(SHA(in, 0, out));
  39826. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  39827. ExpectNotNull(SHA(NULL, 0, out));
  39828. ExpectNotNull(SHA(NULL, 0, NULL));
  39829. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  39830. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39831. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  39832. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  39833. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  39834. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  39835. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  39836. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39837. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  39838. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  39839. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  39840. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  39841. }
  39842. #endif
  39843. #if !defined(NO_SHA256)
  39844. {
  39845. const unsigned char in[] = "abc";
  39846. unsigned char expected[] =
  39847. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  39848. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  39849. "\x15\xAD";
  39850. unsigned char out[WC_SHA256_DIGEST_SIZE];
  39851. unsigned char* p = NULL;
  39852. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  39853. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39854. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  39855. #else
  39856. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  39857. #endif
  39858. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  39859. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39860. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  39861. #else
  39862. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  39863. #endif
  39864. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  39865. }
  39866. #endif
  39867. #if defined(WOLFSSL_SHA384)
  39868. {
  39869. const unsigned char in[] = "abc";
  39870. unsigned char expected[] =
  39871. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  39872. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  39873. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  39874. "\xc8\x25\xa7";
  39875. unsigned char out[WC_SHA384_DIGEST_SIZE];
  39876. unsigned char* p = NULL;
  39877. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  39878. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39879. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  39880. #else
  39881. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  39882. #endif
  39883. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  39884. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39885. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  39886. #else
  39887. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  39888. #endif
  39889. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  39890. }
  39891. #endif
  39892. #if defined(WOLFSSL_SHA512)
  39893. {
  39894. const unsigned char in[] = "abc";
  39895. unsigned char expected[] =
  39896. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  39897. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  39898. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  39899. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  39900. "\xa5\x4c\xa4\x9f";
  39901. unsigned char out[WC_SHA512_DIGEST_SIZE];
  39902. unsigned char* p = NULL;
  39903. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  39904. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39905. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  39906. #else
  39907. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  39908. #endif
  39909. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  39910. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  39911. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  39912. #else
  39913. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  39914. #endif
  39915. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  39916. }
  39917. #endif
  39918. #endif
  39919. return EXPECT_RESULT();
  39920. }
  39921. static int test_wolfSSL_SHA_Transform(void)
  39922. {
  39923. EXPECT_DECLS;
  39924. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  39925. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  39926. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  39927. byte input1[] = "";
  39928. byte input2[] = "abc";
  39929. byte local[WC_SHA_BLOCK_SIZE];
  39930. word32 sLen = 0;
  39931. #ifdef BIG_ENDIAN_ORDER
  39932. unsigned char output1[] =
  39933. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  39934. "\x09\xf7\x3a\x93";
  39935. unsigned char output2[] =
  39936. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  39937. "\x7c\x6e\x08\xb8";
  39938. #else
  39939. unsigned char output1[] =
  39940. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  39941. "\x93\x3a\xf7\x09";
  39942. unsigned char output2[] =
  39943. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  39944. "\xb8\x08\x6e\x7c";
  39945. #endif
  39946. union {
  39947. wc_Sha native;
  39948. SHA_CTX compat;
  39949. } sha;
  39950. union {
  39951. wc_Sha native;
  39952. SHA_CTX compat;
  39953. } sha1;
  39954. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  39955. XMEMSET(&local, 0, sizeof(local));
  39956. /* sanity check */
  39957. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  39958. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  39959. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  39960. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  39961. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  39962. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  39963. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  39964. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  39965. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  39966. /* Init SHA CTX */
  39967. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  39968. /* Do Transform*/
  39969. sLen = (word32)XSTRLEN((char*)input1);
  39970. XMEMCPY(local, input1, sLen);
  39971. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  39972. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  39973. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  39974. /* Init SHA CTX */
  39975. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  39976. sLen = (word32)XSTRLEN((char*)input2);
  39977. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39978. XMEMCPY(local, input2, sLen);
  39979. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  39980. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  39981. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  39982. /* SHA1 */
  39983. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39984. /* Init SHA CTX */
  39985. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  39986. /* Do Transform*/
  39987. sLen = (word32)XSTRLEN((char*)input1);
  39988. XMEMCPY(local, input1, sLen);
  39989. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  39990. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  39991. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  39992. /* Init SHA CTX */
  39993. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  39994. sLen = (word32)XSTRLEN((char*)input2);
  39995. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  39996. XMEMCPY(local, input2, sLen);
  39997. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  39998. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  39999. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  40000. #endif
  40001. #endif
  40002. return EXPECT_RESULT();
  40003. }
  40004. static int test_wolfSSL_SHA224(void)
  40005. {
  40006. EXPECT_DECLS;
  40007. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  40008. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40009. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  40010. unsigned char input[] =
  40011. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40012. unsigned char output[] =
  40013. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  40014. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  40015. size_t inLen;
  40016. byte hash[WC_SHA224_DIGEST_SIZE];
  40017. unsigned char* p;
  40018. inLen = XSTRLEN((char*)input);
  40019. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  40020. ExpectNull(SHA224(NULL, inLen, hash));
  40021. ExpectNotNull(SHA224(input, 0, hash));
  40022. ExpectNotNull(SHA224(input, inLen, NULL));
  40023. ExpectNotNull(SHA224(NULL, 0, hash));
  40024. ExpectNotNull(SHA224(NULL, 0, NULL));
  40025. ExpectNotNull(SHA224(input, inLen, hash));
  40026. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  40027. ExpectNotNull(p = SHA224(input, inLen, NULL));
  40028. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  40029. #endif
  40030. return EXPECT_RESULT();
  40031. }
  40032. static int test_wolfSSL_SHA256(void)
  40033. {
  40034. EXPECT_DECLS;
  40035. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  40036. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  40037. unsigned char input[] =
  40038. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  40039. unsigned char output[] =
  40040. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  40041. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  40042. "\x06\xC1";
  40043. size_t inLen;
  40044. byte hash[WC_SHA256_DIGEST_SIZE];
  40045. inLen = XSTRLEN((char*)input);
  40046. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  40047. ExpectNotNull(SHA256(input, inLen, hash));
  40048. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  40049. #endif
  40050. return EXPECT_RESULT();
  40051. }
  40052. static int test_wolfSSL_SHA256_Transform(void)
  40053. {
  40054. EXPECT_DECLS;
  40055. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  40056. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40057. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40058. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  40059. !defined(WOLFSSL_KCAPI_HASH)
  40060. byte input1[] = "";
  40061. byte input2[] = "abc";
  40062. byte local[WC_SHA256_BLOCK_SIZE];
  40063. word32 sLen = 0;
  40064. #ifdef BIG_ENDIAN_ORDER
  40065. unsigned char output1[] =
  40066. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  40067. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  40068. unsigned char output2[] =
  40069. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  40070. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  40071. #else
  40072. unsigned char output1[] =
  40073. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  40074. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  40075. unsigned char output2[] =
  40076. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  40077. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  40078. #endif
  40079. union {
  40080. wc_Sha256 native;
  40081. SHA256_CTX compat;
  40082. } sha256;
  40083. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  40084. XMEMSET(&local, 0, sizeof(local));
  40085. /* sanity check */
  40086. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  40087. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  40088. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  40089. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  40090. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40091. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  40092. /* Init SHA256 CTX */
  40093. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  40094. /* Do Transform*/
  40095. sLen = (word32)XSTRLEN((char*)input1);
  40096. XMEMCPY(local, input1, sLen);
  40097. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40098. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  40099. 0);
  40100. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40101. /* Init SHA256 CTX */
  40102. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  40103. sLen = (word32)XSTRLEN((char*)input2);
  40104. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  40105. XMEMCPY(local, input2, sLen);
  40106. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  40107. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  40108. 0);
  40109. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  40110. #endif
  40111. #endif
  40112. return EXPECT_RESULT();
  40113. }
  40114. static int test_wolfSSL_SHA512_Transform(void)
  40115. {
  40116. EXPECT_DECLS;
  40117. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  40118. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40119. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40120. !defined(WOLFSSL_KCAPI_HASH)
  40121. byte input1[] = "";
  40122. byte input2[] = "abc";
  40123. byte local[WC_SHA512_BLOCK_SIZE];
  40124. word32 sLen = 0;
  40125. #ifdef BIG_ENDIAN_ORDER
  40126. unsigned char output1[] =
  40127. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  40128. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  40129. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  40130. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  40131. unsigned char output2[] =
  40132. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  40133. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  40134. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  40135. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  40136. #else
  40137. unsigned char output1[] =
  40138. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  40139. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  40140. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  40141. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  40142. unsigned char output2[] =
  40143. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  40144. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  40145. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  40146. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  40147. #endif
  40148. union {
  40149. wc_Sha512 native;
  40150. SHA512_CTX compat;
  40151. } sha512;
  40152. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40153. XMEMSET(&local, 0, sizeof(local));
  40154. /* sanity check */
  40155. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  40156. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  40157. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  40158. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  40159. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  40160. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40161. /* Init SHA512 CTX */
  40162. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  40163. /* Do Transform*/
  40164. sLen = (word32)XSTRLEN((char*)input1);
  40165. XMEMCPY(local, input1, sLen);
  40166. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  40167. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40168. WC_SHA512_DIGEST_SIZE), 0);
  40169. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  40170. /* Init SHA512 CTX */
  40171. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  40172. sLen = (word32)XSTRLEN((char*)input2);
  40173. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40174. XMEMCPY(local, input2, sLen);
  40175. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  40176. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40177. WC_SHA512_DIGEST_SIZE), 0);
  40178. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  40179. (void)input1;
  40180. #endif
  40181. #endif
  40182. return EXPECT_RESULT();
  40183. }
  40184. static int test_wolfSSL_SHA512_224_Transform(void)
  40185. {
  40186. EXPECT_DECLS;
  40187. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  40188. !defined(WOLFSSL_NOSHA512_224)
  40189. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40190. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40191. !defined(WOLFSSL_KCAPI_HASH)
  40192. byte input1[] = "";
  40193. byte input2[] = "abc";
  40194. byte local[WC_SHA512_BLOCK_SIZE];
  40195. word32 sLen = 0;
  40196. unsigned char output1[] =
  40197. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  40198. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  40199. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  40200. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  40201. unsigned char output2[] =
  40202. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  40203. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  40204. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  40205. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  40206. union {
  40207. wc_Sha512 native;
  40208. SHA512_CTX compat;
  40209. } sha512;
  40210. #ifdef BIG_ENDIAN_ORDER
  40211. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  40212. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  40213. #endif
  40214. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40215. XMEMSET(&local, 0, sizeof(local));
  40216. /* sanity check */
  40217. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  40218. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  40219. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  40220. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  40221. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  40222. BAD_FUNC_ARG);
  40223. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40224. /* Init SHA512 CTX */
  40225. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  40226. /* Do Transform*/
  40227. sLen = (word32)XSTRLEN((char*)input1);
  40228. XMEMCPY(local, input1, sLen);
  40229. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  40230. 1);
  40231. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40232. WC_SHA512_DIGEST_SIZE), 0);
  40233. /* frees resources */
  40234. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  40235. /* Init SHA512 CTX */
  40236. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  40237. sLen = (word32)XSTRLEN((char*)input2);
  40238. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40239. XMEMCPY(local, input2, sLen);
  40240. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  40241. 1);
  40242. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40243. WC_SHA512_DIGEST_SIZE), 0);
  40244. /* frees resources */
  40245. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  40246. #endif
  40247. #endif
  40248. return EXPECT_RESULT();
  40249. }
  40250. static int test_wolfSSL_SHA512_256_Transform(void)
  40251. {
  40252. EXPECT_DECLS;
  40253. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  40254. !defined(WOLFSSL_NOSHA512_256)
  40255. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  40256. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  40257. !defined(WOLFSSL_KCAPI_HASH)
  40258. byte input1[] = "";
  40259. byte input2[] = "abc";
  40260. byte local[WC_SHA512_BLOCK_SIZE];
  40261. word32 sLen = 0;
  40262. unsigned char output1[] =
  40263. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  40264. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  40265. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  40266. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  40267. unsigned char output2[] =
  40268. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  40269. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  40270. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  40271. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  40272. union {
  40273. wc_Sha512 native;
  40274. SHA512_CTX compat;
  40275. } sha512;
  40276. #ifdef BIG_ENDIAN_ORDER
  40277. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  40278. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  40279. #endif
  40280. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  40281. XMEMSET(&local, 0, sizeof(local));
  40282. /* sanity check */
  40283. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  40284. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  40285. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  40286. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  40287. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  40288. BAD_FUNC_ARG);
  40289. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  40290. /* Init SHA512 CTX */
  40291. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  40292. /* Do Transform*/
  40293. sLen = (word32)XSTRLEN((char*)input1);
  40294. XMEMCPY(local, input1, sLen);
  40295. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  40296. 1);
  40297. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  40298. WC_SHA512_DIGEST_SIZE), 0);
  40299. /* frees resources */
  40300. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  40301. /* Init SHA512 CTX */
  40302. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  40303. sLen = (word32)XSTRLEN((char*)input2);
  40304. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  40305. XMEMCPY(local, input2, sLen);
  40306. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  40307. 1);
  40308. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  40309. WC_SHA512_DIGEST_SIZE), 0);
  40310. /* frees resources */
  40311. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  40312. #endif
  40313. #endif
  40314. return EXPECT_RESULT();
  40315. }
  40316. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40317. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  40318. * buffer of 64 bytes.
  40319. *
  40320. * returns the size of the digest buffer on success and a negative value on
  40321. * failure.
  40322. */
  40323. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  40324. int* sz)
  40325. {
  40326. EXPECT_DECLS;
  40327. HMAC_CTX ctx1;
  40328. HMAC_CTX ctx2;
  40329. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  40330. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  40331. unsigned char long_key[] =
  40332. "0123456789012345678901234567890123456789"
  40333. "0123456789012345678901234567890123456789"
  40334. "0123456789012345678901234567890123456789"
  40335. "0123456789012345678901234567890123456789";
  40336. unsigned char msg[] = "message to hash";
  40337. unsigned int digestSz = 64;
  40338. int keySz = sizeof(key);
  40339. int long_keySz = sizeof(long_key);
  40340. int msgSz = sizeof(msg);
  40341. unsigned char digest2[64];
  40342. unsigned int digestSz2 = 64;
  40343. HMAC_CTX_init(&ctx1);
  40344. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40345. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40346. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40347. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40348. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40349. HMAC_CTX_cleanup(&ctx1);
  40350. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40351. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  40352. HMAC_CTX_cleanup(&ctx2);
  40353. ExpectIntEQ(digestSz, digestSz2);
  40354. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40355. /* test HMAC_Init with NULL key */
  40356. /* init after copy */
  40357. HMAC_CTX_init(&ctx1);
  40358. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40359. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40360. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40361. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40362. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40363. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40364. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40365. HMAC_CTX_cleanup(&ctx1);
  40366. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40367. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40368. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40369. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40370. HMAC_CTX_cleanup(&ctx2);
  40371. ExpectIntEQ(digestSz, digestSz2);
  40372. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40373. /* long key */
  40374. HMAC_CTX_init(&ctx1);
  40375. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  40376. SSL_SUCCESS);
  40377. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40378. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40379. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40380. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40381. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40382. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40383. HMAC_CTX_cleanup(&ctx1);
  40384. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  40385. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40386. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40387. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40388. HMAC_CTX_cleanup(&ctx2);
  40389. ExpectIntEQ(digestSz, digestSz2);
  40390. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40391. /* init before copy */
  40392. HMAC_CTX_init(&ctx1);
  40393. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  40394. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40395. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  40396. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  40397. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40398. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  40399. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  40400. HMAC_CTX_cleanup(&ctx1);
  40401. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40402. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  40403. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  40404. HMAC_CTX_cleanup(&ctx2);
  40405. ExpectIntEQ(digestSz, digestSz2);
  40406. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  40407. *sz = (int)digestSz;
  40408. return EXPECT_RESULT();
  40409. }
  40410. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  40411. static int test_wolfSSL_HMAC_CTX(void)
  40412. {
  40413. EXPECT_DECLS;
  40414. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  40415. unsigned char digest[64];
  40416. int digestSz;
  40417. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  40418. WOLFSSL_HMAC_CTX ctx1;
  40419. WOLFSSL_HMAC_CTX ctx2;
  40420. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  40421. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  40422. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  40423. wolfSSL_HMAC_CTX_free(NULL);
  40424. wolfSSL_HMAC_CTX_free(hmac_ctx);
  40425. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  40426. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  40427. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  40428. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  40429. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  40430. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  40431. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  40432. ((! defined(HAVE_FIPS_VERSION)) || \
  40433. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  40434. /* Copy object that hasn't had a digest set - MD5. */
  40435. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  40436. #else
  40437. /* Copy object that hasn't had a digest set. */
  40438. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  40439. #endif
  40440. HMAC_CTX_cleanup(NULL);
  40441. HMAC_CTX_cleanup(&ctx2);
  40442. ExpectNull(HMAC_CTX_get_md(NULL));
  40443. #ifndef NO_SHA
  40444. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  40445. TEST_SUCCESS);
  40446. ExpectIntEQ(digestSz, 20);
  40447. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  40448. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  40449. #endif /* !NO_SHA */
  40450. #ifdef WOLFSSL_SHA224
  40451. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  40452. TEST_SUCCESS);
  40453. ExpectIntEQ(digestSz, 28);
  40454. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  40455. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  40456. "\x02\x0E", digest, digestSz), 0);
  40457. #endif /* WOLFSSL_SHA224 */
  40458. #ifndef NO_SHA256
  40459. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  40460. TEST_SUCCESS);
  40461. ExpectIntEQ(digestSz, 32);
  40462. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  40463. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  40464. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  40465. #endif /* !NO_SHA256 */
  40466. #ifdef WOLFSSL_SHA384
  40467. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  40468. TEST_SUCCESS);
  40469. ExpectIntEQ(digestSz, 48);
  40470. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  40471. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  40472. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  40473. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  40474. digest, digestSz), 0);
  40475. #endif /* WOLFSSL_SHA384 */
  40476. #ifdef WOLFSSL_SHA512
  40477. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  40478. TEST_SUCCESS);
  40479. ExpectIntEQ(digestSz, 64);
  40480. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  40481. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  40482. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  40483. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  40484. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  40485. digest, digestSz), 0);
  40486. #endif /* WOLFSSL_SHA512 */
  40487. #ifdef WOLFSSL_SHA3
  40488. #ifndef WOLFSSL_NOSHA3_224
  40489. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  40490. TEST_SUCCESS);
  40491. ExpectIntEQ(digestSz, 28);
  40492. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  40493. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  40494. "\x29\x0f", digest, digestSz), 0);
  40495. #endif
  40496. #ifndef WOLFSSL_NOSHA3_256
  40497. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  40498. TEST_SUCCESS);
  40499. ExpectIntEQ(digestSz, 32);
  40500. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  40501. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  40502. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  40503. #endif
  40504. #ifndef WOLFSSL_NOSHA3_384
  40505. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  40506. TEST_SUCCESS);
  40507. ExpectIntEQ(digestSz, 48);
  40508. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  40509. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  40510. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  40511. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  40512. digest, digestSz), 0);
  40513. #endif
  40514. #ifndef WOLFSSL_NOSHA3_512
  40515. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  40516. TEST_SUCCESS);
  40517. ExpectIntEQ(digestSz, 64);
  40518. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  40519. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  40520. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  40521. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  40522. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  40523. digest, digestSz), 0);
  40524. #endif
  40525. #endif
  40526. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  40527. HAVE_FIPS_VERSION <= 2)
  40528. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  40529. TEST_SUCCESS);
  40530. ExpectIntEQ(digestSz, 16);
  40531. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  40532. "\xE4\x98\xDD", digest, digestSz), 0);
  40533. #endif /* !NO_MD5 */
  40534. #endif
  40535. return EXPECT_RESULT();
  40536. }
  40537. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  40538. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  40539. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  40540. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  40541. {
  40542. EXPECT_DECLS;
  40543. static const unsigned char key[] = "simple test key";
  40544. HMAC_CTX* hmac = NULL;
  40545. ENGINE* e = NULL;
  40546. unsigned char hash[WC_MAX_DIGEST_SIZE];
  40547. unsigned int len;
  40548. ExpectNotNull(hmac = HMAC_CTX_new());
  40549. HMAC_CTX_init(hmac);
  40550. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  40551. ((! defined(HAVE_FIPS_VERSION)) || \
  40552. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  40553. /* Get size on object that hasn't had a digest set - MD5. */
  40554. ExpectIntEQ(HMAC_size(hmac), 16);
  40555. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  40556. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  40557. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  40558. #else
  40559. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  40560. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  40561. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  40562. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  40563. #endif
  40564. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  40565. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  40566. /* re-using test key as data to hash */
  40567. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  40568. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  40569. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  40570. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  40571. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  40572. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  40573. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  40574. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  40575. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  40576. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  40577. ExpectIntEQ(len, md_len);
  40578. ExpectIntEQ(HMAC_size(NULL), 0);
  40579. ExpectIntEQ(HMAC_size(hmac), md_len);
  40580. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  40581. HMAC_cleanup(NULL);
  40582. HMAC_cleanup(hmac);
  40583. HMAC_CTX_free(hmac);
  40584. len = 0;
  40585. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  40586. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  40587. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  40588. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  40589. ExpectIntEQ(len, md_len);
  40590. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  40591. /* With data. */
  40592. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  40593. &len));
  40594. /* With NULL data. */
  40595. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  40596. &len));
  40597. /* With zero length data. */
  40598. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  40599. return EXPECT_RESULT();
  40600. }
  40601. #endif
  40602. static int test_wolfSSL_HMAC(void)
  40603. {
  40604. EXPECT_DECLS;
  40605. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  40606. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  40607. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  40608. #ifndef NO_SHA256
  40609. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  40610. TEST_SUCCESS);
  40611. #endif
  40612. #ifdef WOLFSSL_SHA224
  40613. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  40614. TEST_SUCCESS);
  40615. #endif
  40616. #ifdef WOLFSSL_SHA384
  40617. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  40618. TEST_SUCCESS);
  40619. #endif
  40620. #ifdef WOLFSSL_SHA512
  40621. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  40622. TEST_SUCCESS);
  40623. #endif
  40624. #ifdef WOLFSSL_SHA3
  40625. #ifndef WOLFSSL_NOSHA3_224
  40626. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  40627. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  40628. #endif
  40629. #ifndef WOLFSSL_NOSHA3_256
  40630. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  40631. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  40632. #endif
  40633. #ifndef WOLFSSL_NOSHA3_384
  40634. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  40635. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  40636. #endif
  40637. #ifndef WOLFSSL_NOSHA3_512
  40638. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  40639. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  40640. #endif
  40641. #endif
  40642. #ifndef NO_SHA
  40643. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  40644. TEST_SUCCESS);
  40645. #endif
  40646. #endif
  40647. return EXPECT_RESULT();
  40648. }
  40649. static int test_wolfSSL_CMAC(void)
  40650. {
  40651. EXPECT_DECLS;
  40652. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  40653. defined(WOLFSSL_AES_DIRECT)
  40654. int i;
  40655. byte key[AES_256_KEY_SIZE];
  40656. CMAC_CTX* cmacCtx = NULL;
  40657. byte out[AES_BLOCK_SIZE];
  40658. size_t outLen = AES_BLOCK_SIZE;
  40659. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  40660. key[i] = i;
  40661. }
  40662. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40663. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  40664. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  40665. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40666. NULL), 1);
  40667. /* re-using test key as data to hash */
  40668. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40669. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  40670. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  40671. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  40672. /* No Update works. */
  40673. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40674. NULL), 1);
  40675. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40676. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  40677. NULL), 1);
  40678. /* Test parameters with CMAC_Update. */
  40679. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  40680. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  40681. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  40682. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  40683. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  40684. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  40685. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  40686. /* Test parameters with CMAC_Final. */
  40687. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  40688. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  40689. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  40690. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  40691. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  40692. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  40693. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40694. CMAC_CTX_free(cmacCtx);
  40695. /* Test parameters with CMAC Init. */
  40696. cmacCtx = NULL;
  40697. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40698. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  40699. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  40700. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40701. NULL), 0);
  40702. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40703. NULL), 0);
  40704. /* give a key too small for the cipher, verify we get failure */
  40705. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  40706. NULL), 0);
  40707. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  40708. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  40709. /* Only AES-CBC supported. */
  40710. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  40711. NULL), 0);
  40712. #endif
  40713. CMAC_CTX_free(cmacCtx);
  40714. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  40715. cmacCtx = NULL;
  40716. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40717. /* No Init. */
  40718. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  40719. CMAC_CTX_free(cmacCtx);
  40720. /* Test AES-256-CBC */
  40721. cmacCtx = NULL;
  40722. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40723. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  40724. NULL), 1);
  40725. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40726. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40727. CMAC_CTX_free(cmacCtx);
  40728. /* Test AES-192-CBC */
  40729. cmacCtx = NULL;
  40730. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40731. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  40732. NULL), 1);
  40733. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  40734. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  40735. CMAC_CTX_free(cmacCtx);
  40736. cmacCtx = NULL;
  40737. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  40738. CMAC_CTX_free(cmacCtx);
  40739. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  40740. return EXPECT_RESULT();
  40741. }
  40742. static int test_wolfSSL_DES(void)
  40743. {
  40744. EXPECT_DECLS;
  40745. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40746. const_DES_cblock myDes;
  40747. DES_cblock iv;
  40748. DES_key_schedule key;
  40749. word32 i = 0;
  40750. DES_LONG dl = 0;
  40751. unsigned char msg[] = "hello wolfssl";
  40752. unsigned char weakKey[][8] = {
  40753. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  40754. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  40755. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  40756. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  40757. };
  40758. unsigned char semiWeakKey[][8] = {
  40759. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  40760. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  40761. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  40762. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  40763. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  40764. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  40765. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  40766. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  40767. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  40768. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  40769. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  40770. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  40771. };
  40772. DES_check_key(1);
  40773. DES_set_key(&myDes, &key);
  40774. /* check, check of odd parity */
  40775. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  40776. myDes[0] = 6; /* set even parity */
  40777. XMEMSET(key, 5, sizeof(DES_key_schedule));
  40778. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  40779. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  40780. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  40781. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  40782. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  40783. /* set odd parity for success case */
  40784. DES_set_odd_parity(&myDes);
  40785. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  40786. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  40787. myDes[3]);
  40788. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  40789. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40790. ExpectIntEQ(key[i], myDes[i]);
  40791. }
  40792. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  40793. /* check weak key */
  40794. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  40795. XMEMSET(key, 5, sizeof(DES_key_schedule));
  40796. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  40797. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  40798. DES_set_key_unchecked(NULL, NULL);
  40799. DES_set_key_unchecked(&myDes, NULL);
  40800. DES_set_key_unchecked(NULL, &key);
  40801. /* compare arrays, should be the same */
  40802. /* now do unchecked copy of a weak key over */
  40803. DES_set_key_unchecked(&myDes, &key);
  40804. /* compare arrays, should be the same */
  40805. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40806. ExpectIntEQ(key[i], myDes[i]);
  40807. }
  40808. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  40809. myDes[7] = 2;
  40810. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  40811. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  40812. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  40813. /* Test all weak keys. */
  40814. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  40815. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  40816. }
  40817. /* Test all semi-weak keys. */
  40818. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  40819. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  40820. }
  40821. /* check DES_key_sched API */
  40822. XMEMSET(key, 1, sizeof(DES_key_schedule));
  40823. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  40824. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  40825. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  40826. /* compare arrays, should be the same */
  40827. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  40828. ExpectIntEQ(key[i], myDes[i]);
  40829. }
  40830. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  40831. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  40832. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  40833. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  40834. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  40835. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  40836. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  40837. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  40838. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  40839. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  40840. * DES_cbc_encrypt on the input */
  40841. XMEMSET(iv, 0, sizeof(DES_cblock));
  40842. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  40843. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  40844. ExpectIntEQ(dl, 480052723);
  40845. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  40846. return EXPECT_RESULT();
  40847. }
  40848. static int test_wolfSSL_DES_ncbc(void)
  40849. {
  40850. EXPECT_DECLS;
  40851. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40852. const_DES_cblock myDes;
  40853. DES_cblock iv = {1};
  40854. DES_key_schedule key = {0};
  40855. unsigned char msg[] = "hello wolfssl";
  40856. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  40857. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  40858. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  40859. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  40860. /* partial block test */
  40861. DES_set_key(&key, &myDes);
  40862. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  40863. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  40864. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  40865. DES_set_key(&key, &myDes);
  40866. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40867. *((byte*)&iv) = 1;
  40868. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  40869. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  40870. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  40871. /* full block test */
  40872. DES_set_key(&key, &myDes);
  40873. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  40874. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40875. *((byte*)&iv) = 1;
  40876. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  40877. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  40878. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40879. DES_set_key(&key, &myDes);
  40880. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  40881. *((byte*)&iv) = 1;
  40882. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  40883. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  40884. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  40885. #endif
  40886. return EXPECT_RESULT();
  40887. }
  40888. static int test_wolfSSL_DES_ecb_encrypt(void)
  40889. {
  40890. EXPECT_DECLS;
  40891. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  40892. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  40893. WOLFSSL_DES_key_schedule key;
  40894. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  40895. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  40896. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  40897. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  40898. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  40899. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  40900. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  40901. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  40902. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  40903. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  40904. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  40905. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  40906. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  40907. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  40908. /* Encrypt messages */
  40909. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  40910. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  40911. {
  40912. /* Decrypt messages */
  40913. int ret1 = 0;
  40914. int ret2 = 0;
  40915. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  40916. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  40917. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  40918. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  40919. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  40920. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  40921. }
  40922. #endif
  40923. return EXPECT_RESULT();
  40924. }
  40925. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  40926. {
  40927. EXPECT_DECLS;
  40928. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  40929. unsigned char input1[8], input2[8];
  40930. unsigned char output1[8], output2[8];
  40931. unsigned char back1[8], back2[8];
  40932. WOLFSSL_DES_cblock iv1, iv2;
  40933. WOLFSSL_DES_key_schedule key1, key2, key3;
  40934. int i;
  40935. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  40936. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  40937. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  40938. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  40939. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  40940. XMEMSET(output1, 0, sizeof(output1));
  40941. XMEMSET(output2, 0, sizeof(output2));
  40942. XMEMSET(back1, 0, sizeof(back1));
  40943. XMEMSET(back2, 0, sizeof(back2));
  40944. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40945. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40946. /* Encrypt messages */
  40947. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  40948. DES_ENCRYPT);
  40949. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  40950. DES_ENCRYPT);
  40951. {
  40952. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40953. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40954. /* Decrypt messages */
  40955. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  40956. &iv1, DES_DECRYPT);
  40957. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  40958. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  40959. &iv2, DES_DECRYPT);
  40960. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  40961. }
  40962. for (i = 0; i < 8; i++) {
  40963. XMEMSET(output1, 0, sizeof(output1));
  40964. XMEMSET(output2, 0, sizeof(output2));
  40965. XMEMSET(back1, 0, sizeof(back1));
  40966. XMEMSET(back2, 0, sizeof(back2));
  40967. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40968. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40969. /* Encrypt partial messages */
  40970. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  40971. &iv1, DES_ENCRYPT);
  40972. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  40973. &iv2, DES_ENCRYPT);
  40974. {
  40975. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  40976. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  40977. /* Decrypt messages */
  40978. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  40979. &key3, &iv1, DES_DECRYPT);
  40980. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  40981. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  40982. &key3, &iv2, DES_DECRYPT);
  40983. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  40984. }
  40985. }
  40986. #endif
  40987. return EXPECT_RESULT();
  40988. }
  40989. static int test_wolfSSL_AES_encrypt(void)
  40990. {
  40991. EXPECT_DECLS;
  40992. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  40993. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  40994. AES_KEY enc;
  40995. AES_KEY dec;
  40996. const byte msg[] = {
  40997. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  40998. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  40999. };
  41000. const byte exp[] = {
  41001. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  41002. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  41003. };
  41004. const byte key[] = {
  41005. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  41006. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41007. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41008. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  41009. };
  41010. byte eout[sizeof(msg)];
  41011. byte dout[sizeof(msg)];
  41012. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  41013. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  41014. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  41015. wolfSSL_AES_encrypt(msg, NULL, NULL);
  41016. wolfSSL_AES_encrypt(NULL, eout, NULL);
  41017. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  41018. wolfSSL_AES_encrypt(msg, eout, NULL);
  41019. wolfSSL_AES_encrypt(msg, NULL, &enc);
  41020. wolfSSL_AES_encrypt(NULL, eout, &enc);
  41021. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  41022. wolfSSL_AES_decrypt(eout, NULL, NULL);
  41023. wolfSSL_AES_decrypt(NULL, dout, NULL);
  41024. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  41025. wolfSSL_AES_decrypt(eout, dout, NULL);
  41026. wolfSSL_AES_decrypt(eout, NULL, &dec);
  41027. wolfSSL_AES_decrypt(NULL, dout, &dec);
  41028. wolfSSL_AES_encrypt(msg, eout, &enc);
  41029. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  41030. wolfSSL_AES_decrypt(eout, dout, &dec);
  41031. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  41032. #endif
  41033. return EXPECT_RESULT();
  41034. }
  41035. static int test_wolfSSL_AES_ecb_encrypt(void)
  41036. {
  41037. EXPECT_DECLS;
  41038. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  41039. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41040. AES_KEY aes;
  41041. const byte msg[] =
  41042. {
  41043. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41044. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  41045. };
  41046. const byte verify[] =
  41047. {
  41048. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  41049. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  41050. };
  41051. const byte key[] =
  41052. {
  41053. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41054. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41055. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41056. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  41057. };
  41058. byte out[AES_BLOCK_SIZE];
  41059. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  41060. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41061. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  41062. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  41063. #ifdef HAVE_AES_DECRYPT
  41064. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  41065. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41066. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  41067. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  41068. #endif
  41069. /* test bad arguments */
  41070. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  41071. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  41072. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  41073. #endif
  41074. return EXPECT_RESULT();
  41075. }
  41076. static int test_wolfSSL_AES_cbc_encrypt(void)
  41077. {
  41078. EXPECT_DECLS;
  41079. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  41080. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41081. AES_KEY aes;
  41082. AES_KEY* aesN = NULL;
  41083. size_t len = 0;
  41084. size_t lenB = 0;
  41085. int keySz0 = 0;
  41086. int keySzN = -1;
  41087. byte out[AES_BLOCK_SIZE] = {0};
  41088. byte* outN = NULL;
  41089. /* Test vectors retrieved from:
  41090. * <begin URL>
  41091. * https://csrc.nist.gov/
  41092. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  41093. * documents/aes/KAT_AES.zip
  41094. * </end URL>
  41095. */
  41096. const byte* pt128N = NULL;
  41097. byte* key128N = NULL;
  41098. byte* iv128N = NULL;
  41099. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  41100. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41101. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41102. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  41103. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  41104. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41105. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  41106. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  41107. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  41108. len = sizeof(pt128);
  41109. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  41110. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  41111. ExpectIntNE(XMEMCMP(b, g, h), i)
  41112. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  41113. /* Stressing wolfSSL_AES_cbc_encrypt() */
  41114. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41115. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  41116. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  41117. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41118. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  41119. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41120. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  41121. /* Stressing wolfSSL_AES_set_encrypt_key */
  41122. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41123. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  41124. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  41125. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  41126. /* Stressing wolfSSL_AES_set_decrypt_key */
  41127. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  41128. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  41129. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  41130. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  41131. #ifdef WOLFSSL_AES_128
  41132. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  41133. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41134. RESET_IV(iv128tmp, iv128);
  41135. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41136. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  41137. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  41138. wc_AesFree((Aes*)&aes);
  41139. #ifdef HAVE_AES_DECRYPT
  41140. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  41141. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41142. RESET_IV(iv128tmp, iv128);
  41143. len = sizeof(ct128);
  41144. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  41145. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  41146. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  41147. wc_AesFree((Aes*)&aes);
  41148. #endif
  41149. #endif /* WOLFSSL_AES_128 */
  41150. #ifdef WOLFSSL_AES_192
  41151. {
  41152. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  41153. * Appendix F.2.3 */
  41154. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  41155. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41156. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41157. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  41158. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  41159. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41160. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41161. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  41162. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  41163. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  41164. len = sizeof(pt192);
  41165. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  41166. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41167. RESET_IV(iv192tmp, iv192);
  41168. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41169. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  41170. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  41171. wc_AesFree((Aes*)&aes);
  41172. #ifdef HAVE_AES_DECRYPT
  41173. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  41174. len = sizeof(ct192);
  41175. RESET_IV(iv192tmp, iv192);
  41176. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41177. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  41178. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  41179. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  41180. wc_AesFree((Aes*)&aes);
  41181. #endif
  41182. }
  41183. #endif /* WOLFSSL_AES_192 */
  41184. #ifdef WOLFSSL_AES_256
  41185. {
  41186. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  41187. * Appendix F.2.5 */
  41188. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  41189. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  41190. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  41191. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  41192. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  41193. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  41194. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  41195. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  41196. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  41197. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  41198. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  41199. len = sizeof(pt256);
  41200. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  41201. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41202. RESET_IV(iv256tmp, iv256);
  41203. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41204. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  41205. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  41206. wc_AesFree((Aes*)&aes);
  41207. #ifdef HAVE_AES_DECRYPT
  41208. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  41209. len = sizeof(ct256);
  41210. RESET_IV(iv256tmp, iv256);
  41211. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41212. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41213. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  41214. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  41215. wc_AesFree((Aes*)&aes);
  41216. #endif
  41217. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  41218. !defined(HAVE_SELFTEST)
  41219. {
  41220. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  41221. byte wrapPlain[sizeof(key256)] = { 0 };
  41222. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  41223. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  41224. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41225. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41226. 15), WOLFSSL_FAILURE);
  41227. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  41228. sizeof(key256)), sizeof(wrapCipher));
  41229. wc_AesFree((Aes*)&aes);
  41230. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  41231. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41232. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41233. 23), WOLFSSL_FAILURE);
  41234. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  41235. sizeof(wrapCipher)), sizeof(wrapPlain));
  41236. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41237. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  41238. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  41239. wc_AesFree((Aes*)&aes);
  41240. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  41241. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41242. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  41243. sizeof(key256)), sizeof(wrapCipher));
  41244. wc_AesFree((Aes*)&aes);
  41245. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  41246. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  41247. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  41248. sizeof(wrapCipher)), sizeof(wrapPlain));
  41249. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  41250. wc_AesFree((Aes*)&aes);
  41251. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  41252. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  41253. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  41254. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  41255. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  41256. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  41257. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  41258. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  41259. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  41260. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  41261. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  41262. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  41263. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  41264. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  41265. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  41266. 0);
  41267. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  41268. 0);
  41269. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  41270. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  41271. }
  41272. #endif /* HAVE_AES_KEYWRAP */
  41273. }
  41274. #endif /* WOLFSSL_AES_256 */
  41275. #endif
  41276. return EXPECT_RESULT();
  41277. }
  41278. static int test_wolfSSL_AES_cfb128_encrypt(void)
  41279. {
  41280. EXPECT_DECLS;
  41281. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  41282. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41283. AES_KEY aesEnc;
  41284. AES_KEY aesDec;
  41285. const byte msg[] = {
  41286. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  41287. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  41288. };
  41289. const byte exp[] = {
  41290. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  41291. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  41292. };
  41293. const byte key[] = {
  41294. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  41295. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41296. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41297. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  41298. };
  41299. const byte ivData[] = {
  41300. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  41301. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  41302. };
  41303. byte out[AES_BLOCK_SIZE];
  41304. byte iv[AES_BLOCK_SIZE];
  41305. word32 i;
  41306. int num;
  41307. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  41308. XMEMCPY(iv, ivData, sizeof(iv));
  41309. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41310. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  41311. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  41312. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41313. #ifdef HAVE_AES_DECRYPT
  41314. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  41315. XMEMCPY(iv, ivData, sizeof(iv));
  41316. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41317. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  41318. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41319. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41320. #endif
  41321. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  41322. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  41323. XMEMCPY(iv, ivData, sizeof(iv));
  41324. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41325. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  41326. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  41327. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  41328. if (i == 0) {
  41329. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41330. }
  41331. else {
  41332. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41333. }
  41334. #ifdef HAVE_AES_DECRYPT
  41335. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  41336. XMEMCPY(iv, ivData, sizeof(iv));
  41337. XMEMSET(out, 0, AES_BLOCK_SIZE);
  41338. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  41339. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  41340. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  41341. if (i == 0) {
  41342. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41343. }
  41344. else {
  41345. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  41346. }
  41347. #endif
  41348. }
  41349. if (EXPECT_SUCCESS()) {
  41350. /* test bad arguments */
  41351. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  41352. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  41353. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  41354. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  41355. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  41356. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  41357. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  41358. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  41359. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  41360. }
  41361. #endif
  41362. return EXPECT_RESULT();
  41363. }
  41364. static int test_wolfSSL_CRYPTO_cts128(void)
  41365. {
  41366. EXPECT_DECLS;
  41367. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  41368. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  41369. byte tmp[64]; /* Largest vector size */
  41370. /* Test vectors taken form RFC3962 Appendix B */
  41371. const testVector vects[] = {
  41372. {
  41373. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41374. "\x20",
  41375. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  41376. "\x97",
  41377. 17, 17
  41378. },
  41379. {
  41380. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41381. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  41382. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  41383. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  41384. 31, 31
  41385. },
  41386. {
  41387. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41388. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  41389. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41390. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  41391. 32, 32
  41392. },
  41393. {
  41394. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41395. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41396. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  41397. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41398. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  41399. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  41400. 47, 47
  41401. },
  41402. {
  41403. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41404. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41405. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  41406. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41407. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  41408. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  41409. 48, 48
  41410. },
  41411. {
  41412. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  41413. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  41414. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  41415. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  41416. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  41417. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  41418. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  41419. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  41420. 64, 64
  41421. }
  41422. };
  41423. byte keyBytes[AES_128_KEY_SIZE] = {
  41424. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  41425. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  41426. };
  41427. size_t i;
  41428. AES_KEY encKey;
  41429. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  41430. XMEMSET(tmp, 0, sizeof(tmp));
  41431. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  41432. AES_KEY decKey;
  41433. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  41434. &encKey), 0);
  41435. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  41436. &decKey), 0);
  41437. XMEMSET(iv, 0, sizeof(iv));
  41438. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  41439. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  41440. vects[i].outLen);
  41441. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  41442. XMEMSET(iv, 0, sizeof(iv));
  41443. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  41444. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  41445. vects[i].inLen);
  41446. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  41447. }
  41448. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  41449. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  41450. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  41451. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  41452. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  41453. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  41454. (cbc128_f)AES_cbc_encrypt), 0);
  41455. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  41456. (cbc128_f)AES_cbc_encrypt), 0);
  41457. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  41458. (cbc128_f)AES_cbc_encrypt), 0);
  41459. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  41460. (cbc128_f)AES_cbc_encrypt), 0);
  41461. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  41462. (cbc128_f)AES_cbc_encrypt), 0);
  41463. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  41464. /* Length too small. */
  41465. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  41466. (cbc128_f)AES_cbc_encrypt), 0);
  41467. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  41468. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  41469. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  41470. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  41471. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  41472. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  41473. (cbc128_f)AES_cbc_encrypt), 0);
  41474. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  41475. (cbc128_f)AES_cbc_encrypt), 0);
  41476. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  41477. (cbc128_f)AES_cbc_encrypt), 0);
  41478. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  41479. (cbc128_f)AES_cbc_encrypt), 0);
  41480. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  41481. (cbc128_f)AES_cbc_encrypt), 0);
  41482. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  41483. /* Length too small. */
  41484. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  41485. (cbc128_f)AES_cbc_encrypt), 0);
  41486. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  41487. return EXPECT_RESULT();
  41488. }
  41489. static int test_wolfSSL_RC4(void)
  41490. {
  41491. EXPECT_DECLS;
  41492. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  41493. WOLFSSL_RC4_KEY rc4Key;
  41494. unsigned char key[] = {
  41495. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41496. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41497. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41498. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  41499. };
  41500. unsigned char data[] = {
  41501. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41502. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41503. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41504. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  41505. };
  41506. unsigned char enc[sizeof(data)];
  41507. unsigned char dec[sizeof(data)];
  41508. word32 i;
  41509. word32 j;
  41510. wolfSSL_RC4_set_key(NULL, -1, NULL);
  41511. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  41512. wolfSSL_RC4_set_key(NULL, 0, NULL);
  41513. wolfSSL_RC4_set_key(NULL, -1, key);
  41514. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  41515. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  41516. wolfSSL_RC4_set_key(NULL, 0, key);
  41517. wolfSSL_RC4(NULL, 0, NULL, NULL);
  41518. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  41519. wolfSSL_RC4(NULL, 0, data, NULL);
  41520. wolfSSL_RC4(NULL, 0, NULL, enc);
  41521. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  41522. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  41523. wolfSSL_RC4(NULL, 0, data, enc);
  41524. ExpectIntEQ(1, 1);
  41525. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  41526. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  41527. XMEMSET(enc, 0, sizeof(enc));
  41528. XMEMSET(dec, 0, sizeof(dec));
  41529. /* Encrypt */
  41530. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  41531. wolfSSL_RC4(&rc4Key, j, data, enc);
  41532. /* Decrypt */
  41533. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  41534. wolfSSL_RC4(&rc4Key, j, enc, dec);
  41535. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  41536. }
  41537. }
  41538. #endif
  41539. return EXPECT_RESULT();
  41540. }
  41541. static int test_wolfSSL_OBJ(void)
  41542. {
  41543. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  41544. * mode
  41545. */
  41546. EXPECT_DECLS;
  41547. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  41548. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  41549. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  41550. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  41551. ASN1_OBJECT *obj = NULL;
  41552. ASN1_OBJECT *obj2 = NULL;
  41553. char buf[50];
  41554. XFILE fp = XBADFILE;
  41555. X509 *x509 = NULL;
  41556. X509_NAME *x509Name = NULL;
  41557. X509_NAME_ENTRY *x509NameEntry = NULL;
  41558. ASN1_OBJECT *asn1Name = NULL;
  41559. int numNames = 0;
  41560. BIO *bio = NULL;
  41561. int nid;
  41562. int i, j;
  41563. const char *f[] = {
  41564. #ifndef NO_RSA
  41565. "./certs/ca-cert.der",
  41566. #endif
  41567. #ifdef HAVE_ECC
  41568. "./certs/ca-ecc-cert.der",
  41569. "./certs/ca-ecc384-cert.der",
  41570. #endif
  41571. NULL};
  41572. ASN1_OBJECT *field_name_obj = NULL;
  41573. int lastpos = -1;
  41574. int tmp = -1;
  41575. ASN1_STRING *asn1 = NULL;
  41576. unsigned char *buf_dyn = NULL;
  41577. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  41578. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  41579. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  41580. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  41581. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  41582. ASN1_OBJECT_free(obj);
  41583. obj = NULL;
  41584. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  41585. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  41586. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  41587. #ifdef WOLFSSL_CERT_EXT
  41588. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  41589. #endif
  41590. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  41591. ExpectNotNull(obj2 = OBJ_dup(obj));
  41592. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  41593. ASN1_OBJECT_free(obj);
  41594. obj = NULL;
  41595. ASN1_OBJECT_free(obj2);
  41596. obj2 = NULL;
  41597. for (i = 0; f[i] != NULL; i++)
  41598. {
  41599. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  41600. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  41601. if (fp != XBADFILE) {
  41602. XFCLOSE(fp);
  41603. fp = XBADFILE;
  41604. }
  41605. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  41606. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  41607. /* Get the Common Name by using OBJ_txt2obj */
  41608. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  41609. do
  41610. {
  41611. lastpos = tmp;
  41612. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  41613. } while (tmp > -1);
  41614. ExpectIntNE(lastpos, -1);
  41615. ASN1_OBJECT_free(field_name_obj);
  41616. field_name_obj = NULL;
  41617. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  41618. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  41619. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  41620. /*
  41621. * All Common Names should be www.wolfssl.com
  41622. * This makes testing easier as we can test for the expected value.
  41623. */
  41624. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  41625. OPENSSL_free(buf_dyn);
  41626. buf_dyn = NULL;
  41627. bio = BIO_new(BIO_s_mem());
  41628. ExpectTrue(bio != NULL);
  41629. for (j = 0; j < numNames; j++)
  41630. {
  41631. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  41632. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  41633. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  41634. }
  41635. BIO_free(bio);
  41636. bio = NULL;
  41637. X509_free(x509);
  41638. x509 = NULL;
  41639. }
  41640. #ifdef HAVE_PKCS12
  41641. {
  41642. PKCS12 *p12 = NULL;
  41643. int boolRet;
  41644. EVP_PKEY *pkey = NULL;
  41645. const char *p12_f[] = {
  41646. #if !defined(NO_DES3) && !defined(NO_RSA)
  41647. "./certs/test-servercert.p12",
  41648. #endif
  41649. NULL};
  41650. for (i = 0; p12_f[i] != NULL; i++)
  41651. {
  41652. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  41653. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  41654. if (fp != XBADFILE) {
  41655. XFCLOSE(fp);
  41656. fp = XBADFILE;
  41657. }
  41658. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  41659. &pkey, &x509, NULL)) > 0);
  41660. wc_PKCS12_free(p12);
  41661. p12 = NULL;
  41662. EVP_PKEY_free(pkey);
  41663. x509Name = X509_get_issuer_name(x509);
  41664. ExpectNotNull(x509Name);
  41665. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  41666. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  41667. for (j = 0; j < numNames; j++)
  41668. {
  41669. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  41670. ExpectNotNull(asn1Name =
  41671. X509_NAME_ENTRY_get_object(x509NameEntry));
  41672. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  41673. }
  41674. BIO_free(bio);
  41675. bio = NULL;
  41676. X509_free(x509);
  41677. x509 = NULL;
  41678. }
  41679. }
  41680. #endif /* HAVE_PKCS12 */
  41681. #endif
  41682. return EXPECT_RESULT();
  41683. }
  41684. static int test_wolfSSL_OBJ_cmp(void)
  41685. {
  41686. EXPECT_DECLS;
  41687. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  41688. ASN1_OBJECT *obj = NULL;
  41689. ASN1_OBJECT *obj2 = NULL;
  41690. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  41691. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  41692. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  41693. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  41694. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  41695. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  41696. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  41697. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  41698. ASN1_OBJECT_free(obj);
  41699. ASN1_OBJECT_free(obj2);
  41700. #endif
  41701. return EXPECT_RESULT();
  41702. }
  41703. static int test_wolfSSL_OBJ_txt2nid(void)
  41704. {
  41705. EXPECT_DECLS;
  41706. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  41707. defined(WOLFSSL_APACHE_HTTPD)
  41708. int i;
  41709. static const struct {
  41710. const char* sn;
  41711. const char* ln;
  41712. const char* oid;
  41713. int nid;
  41714. } testVals[] = {
  41715. #ifdef WOLFSSL_APACHE_HTTPD
  41716. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  41717. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  41718. NID_id_on_dnsSRV },
  41719. { "msUPN", "Microsoft User Principal Name",
  41720. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  41721. #endif
  41722. { NULL, NULL, NULL, NID_undef }
  41723. };
  41724. /* Invalid cases */
  41725. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  41726. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  41727. /* Valid cases */
  41728. for (i = 0; testVals[i].sn != NULL; i++) {
  41729. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  41730. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  41731. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  41732. }
  41733. #endif
  41734. return EXPECT_RESULT();
  41735. }
  41736. static int test_wolfSSL_OBJ_txt2obj(void)
  41737. {
  41738. EXPECT_DECLS;
  41739. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  41740. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  41741. int i;
  41742. char buf[50];
  41743. ASN1_OBJECT* obj = NULL;
  41744. static const struct {
  41745. const char* oidStr;
  41746. const char* sn;
  41747. const char* ln;
  41748. } objs_list[] = {
  41749. #if defined(WOLFSSL_APACHE_HTTPD)
  41750. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  41751. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  41752. #endif
  41753. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  41754. { NULL, NULL, NULL }
  41755. };
  41756. static const struct {
  41757. const char* numeric;
  41758. const char* name;
  41759. } objs_named[] = {
  41760. /* In dictionary but not in normal list. */
  41761. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  41762. /* Made up OID. */
  41763. { "1.3.5.7", "1.3.5.7" },
  41764. { NULL, NULL }
  41765. };
  41766. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  41767. ASN1_OBJECT_free(obj);
  41768. obj = NULL;
  41769. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  41770. ASN1_OBJECT_free(obj);
  41771. obj = NULL;
  41772. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  41773. /* Test numerical value of oid (oidStr) */
  41774. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  41775. /* Convert object back to text to confirm oid is correct */
  41776. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41777. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41778. ASN1_OBJECT_free(obj);
  41779. obj = NULL;
  41780. XMEMSET(buf, 0, sizeof(buf));
  41781. /* Test short name (sn) */
  41782. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  41783. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  41784. /* Convert object back to text to confirm oid is correct */
  41785. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41786. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41787. ASN1_OBJECT_free(obj);
  41788. obj = NULL;
  41789. XMEMSET(buf, 0, sizeof(buf));
  41790. /* Test long name (ln) - should fail when no_name = 1 */
  41791. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  41792. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  41793. /* Convert object back to text to confirm oid is correct */
  41794. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41795. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  41796. ASN1_OBJECT_free(obj);
  41797. obj = NULL;
  41798. XMEMSET(buf, 0, sizeof(buf));
  41799. }
  41800. for (i = 0; objs_named[i].numeric != NULL; i++) {
  41801. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  41802. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  41803. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  41804. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  41805. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  41806. ASN1_OBJECT_free(obj);
  41807. obj = NULL;
  41808. }
  41809. #endif
  41810. return EXPECT_RESULT();
  41811. }
  41812. static int test_wolfSSL_PEM_write_bio_X509(void)
  41813. {
  41814. EXPECT_DECLS;
  41815. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  41816. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  41817. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  41818. !defined(NO_FILESYSTEM)
  41819. /* This test contains the hard coded expected
  41820. * lengths. Update if necessary */
  41821. XFILE fp = XBADFILE;
  41822. WOLFSSL_EVP_PKEY *priv = NULL;
  41823. BIO* input = NULL;
  41824. BIO* output = NULL;
  41825. X509* x509a = NULL;
  41826. X509* x509b = NULL;
  41827. ASN1_TIME* notBeforeA = NULL;
  41828. ASN1_TIME* notAfterA = NULL;
  41829. #ifndef NO_ASN_TIME
  41830. ASN1_TIME* notBeforeB = NULL;
  41831. ASN1_TIME* notAfterB = NULL;
  41832. #endif
  41833. int expectedLen;
  41834. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  41835. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  41836. if (fp != XBADFILE) {
  41837. XFCLOSE(fp);
  41838. fp = XBADFILE;
  41839. }
  41840. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  41841. "rb"));
  41842. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  41843. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  41844. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41845. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  41846. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  41847. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  41848. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41849. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41850. /* compare length against expected */
  41851. expectedLen = 2000;
  41852. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  41853. #ifndef NO_ASN_TIME
  41854. /* read exported X509 PEM back into struct, sanity check on export,
  41855. * make sure notBefore/notAfter are the same and certs are identical. */
  41856. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41857. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  41858. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  41859. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  41860. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  41861. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41862. X509_free(x509b);
  41863. x509b = NULL;
  41864. #endif
  41865. /* Reset output buffer */
  41866. BIO_free(output);
  41867. output = NULL;
  41868. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41869. /* Test forcing the AKID to be generated just from KeyIdentifier */
  41870. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  41871. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  41872. x509a->authKeyId = x509a->authKeyIdSrc;
  41873. x509a->authKeyIdSrc = NULL;
  41874. x509a->authKeyIdSrcSz = 0;
  41875. }
  41876. /* Resign to re-generate the der */
  41877. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  41878. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41879. /* Check that we generate a smaller output since the AKID will
  41880. * only contain the KeyIdentifier without any additional
  41881. * information */
  41882. /* Here we copy the validity struct from the original */
  41883. expectedLen = 1688;
  41884. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  41885. /* Reset buffers and x509 */
  41886. BIO_free(input);
  41887. input = NULL;
  41888. BIO_free(output);
  41889. output = NULL;
  41890. X509_free(x509a);
  41891. x509a = NULL;
  41892. /* test CA and basicConstSet values are encoded when
  41893. * the cert is a CA */
  41894. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  41895. /* read PEM into X509 struct */
  41896. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41897. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  41898. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41899. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41900. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  41901. * values are maintained and certs are identical.*/
  41902. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41903. ExpectIntEQ(x509b->isCa, 1);
  41904. ExpectIntEQ(x509b->basicConstSet, 1);
  41905. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41906. X509_free(x509a);
  41907. x509a = NULL;
  41908. X509_free(x509b);
  41909. x509b = NULL;
  41910. BIO_free(input);
  41911. input = NULL;
  41912. BIO_free(output);
  41913. output = NULL;
  41914. /* test CA and basicConstSet values are encoded when
  41915. * the cert is not CA */
  41916. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  41917. /* read PEM into X509 struct */
  41918. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  41919. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  41920. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  41921. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  41922. /* read exported X509 PEM back into struct, ensure isCa and
  41923. * basicConstSet values are maintained and certs are identical */
  41924. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  41925. ExpectIntEQ(x509b->isCa, 0);
  41926. ExpectIntEQ(x509b->basicConstSet, 1);
  41927. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  41928. wolfSSL_EVP_PKEY_free(priv);
  41929. X509_free(x509a);
  41930. X509_free(x509b);
  41931. BIO_free(input);
  41932. BIO_free(output);
  41933. #endif
  41934. return EXPECT_RESULT();
  41935. }
  41936. static int test_wolfSSL_X509_NAME_ENTRY(void)
  41937. {
  41938. EXPECT_DECLS;
  41939. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  41940. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  41941. X509* x509 = NULL;
  41942. #ifndef NO_BIO
  41943. BIO* bio = NULL;
  41944. #endif
  41945. X509_NAME* nm = NULL;
  41946. X509_NAME_ENTRY* entry = NULL;
  41947. unsigned char cn[] = "another name to add";
  41948. #ifdef OPENSSL_ALL
  41949. int i;
  41950. int names_len = 0;
  41951. #endif
  41952. ExpectNotNull(x509 =
  41953. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  41954. #ifndef NO_BIO
  41955. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41956. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  41957. #endif
  41958. #ifdef WOLFSSL_CERT_REQ
  41959. {
  41960. X509_REQ* req = NULL;
  41961. #ifndef NO_BIO
  41962. BIO* bReq = NULL;
  41963. #endif
  41964. ExpectNotNull(req =
  41965. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  41966. #ifndef NO_BIO
  41967. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  41968. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  41969. BIO_free(bReq);
  41970. #endif
  41971. X509_free(req);
  41972. }
  41973. #endif
  41974. ExpectNotNull(nm = X509_get_subject_name(x509));
  41975. /* Test add entry */
  41976. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  41977. 0x0c, cn, (int)sizeof(cn)));
  41978. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  41979. #ifdef WOLFSSL_CERT_EXT
  41980. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  41981. (byte*)"support@wolfssl.com", 19, -1,
  41982. 1), WOLFSSL_SUCCESS);
  41983. #endif
  41984. X509_NAME_ENTRY_free(entry);
  41985. entry = NULL;
  41986. #ifdef WOLFSSL_CERT_REQ
  41987. {
  41988. unsigned char srv_pkcs9p[] = "Server";
  41989. unsigned char fvrtDrnk[] = "tequila";
  41990. unsigned char* der = NULL;
  41991. char* subject = NULL;
  41992. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  41993. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  41994. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  41995. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  41996. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  41997. ExpectNotNull(der);
  41998. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  41999. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  42000. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  42001. #ifdef DEBUG_WOLFSSL
  42002. if (subject != NULL) {
  42003. fprintf(stderr, "\n\t%s\n", subject);
  42004. }
  42005. #endif
  42006. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  42007. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42008. }
  42009. #endif
  42010. /* Test add entry by text */
  42011. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  42012. 0x0c, cn, (int)sizeof(cn)));
  42013. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  42014. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  42015. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  42016. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  42017. #endif
  42018. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  42019. X509_NAME_ENTRY_free(entry);
  42020. entry = NULL;
  42021. /* Test add entry by NID */
  42022. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  42023. cn, -1, -1, 0), SSL_SUCCESS);
  42024. #ifdef OPENSSL_ALL
  42025. /* stack of name entry */
  42026. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  42027. for (i = 0; i < names_len; i++) {
  42028. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  42029. }
  42030. #endif
  42031. #ifndef NO_BIO
  42032. BIO_free(bio);
  42033. #endif
  42034. X509_free(x509); /* free's nm */
  42035. #endif
  42036. return EXPECT_RESULT();
  42037. }
  42038. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  42039. static int test_GENERAL_NAME_set0_othername(void) {
  42040. EXPECT_DECLS;
  42041. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42042. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  42043. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  42044. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  42045. defined(WOLFSSL_FPKI)
  42046. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  42047. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  42048. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  42049. const char * cert_fname = "./certs/server-cert.der";
  42050. const char * key_fname = "./certs/server-key.der";
  42051. X509* x509 = NULL;
  42052. GENERAL_NAME* gn = NULL;
  42053. GENERAL_NAMES* gns = NULL;
  42054. ASN1_OBJECT* upn_oid = NULL;
  42055. ASN1_UTF8STRING *utf8str = NULL;
  42056. ASN1_TYPE *value = NULL;
  42057. X509_EXTENSION * ext = NULL;
  42058. byte* pt = NULL;
  42059. byte der[4096];
  42060. int derSz = 0;
  42061. EVP_PKEY* priv = NULL;
  42062. XFILE f = XBADFILE;
  42063. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  42064. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  42065. if (f != XBADFILE) {
  42066. XFCLOSE(f);
  42067. f = XBADFILE;
  42068. }
  42069. ExpectNotNull(gn = GENERAL_NAME_new());
  42070. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  42071. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  42072. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  42073. ExpectNotNull(value = ASN1_TYPE_new());
  42074. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  42075. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  42076. wolfSSL_ASN1_STRING_free(utf8str);
  42077. }
  42078. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  42079. if (EXPECT_FAIL()) {
  42080. ASN1_TYPE_free(value);
  42081. }
  42082. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  42083. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  42084. if (EXPECT_FAIL()) {
  42085. GENERAL_NAME_free(gn);
  42086. gn = NULL;
  42087. }
  42088. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  42089. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  42090. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  42091. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  42092. if (f != XBADFILE) {
  42093. XFCLOSE(f);
  42094. f = XBADFILE;
  42095. }
  42096. pt = der;
  42097. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42098. (const unsigned char**)&pt, derSz));
  42099. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  42100. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42101. gns = NULL;
  42102. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  42103. NULL));
  42104. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  42105. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  42106. ExpectIntEQ(gn->type, 0);
  42107. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42108. ASN1_OBJECT_free(upn_oid);
  42109. X509_EXTENSION_free(ext);
  42110. X509_free(x509);
  42111. EVP_PKEY_free(priv);
  42112. #endif
  42113. return EXPECT_RESULT();
  42114. }
  42115. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  42116. static int test_othername_and_SID_ext(void) {
  42117. EXPECT_DECLS;
  42118. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42119. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  42120. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  42121. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  42122. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  42123. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  42124. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  42125. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  42126. const char* csr_fname = "./certs/csr.signed.der";
  42127. const char* key_fname = "./certs/server-key.der";
  42128. byte der[4096];
  42129. int derSz = 0;
  42130. X509_REQ* x509 = NULL;
  42131. STACK_OF(X509_EXTENSION) *exts = NULL;
  42132. X509_EXTENSION * san_ext = NULL;
  42133. X509_EXTENSION * ext = NULL;
  42134. GENERAL_NAME* gn = NULL;
  42135. GENERAL_NAMES* gns = NULL;
  42136. ASN1_OBJECT* upn_oid = NULL;
  42137. ASN1_UTF8STRING *utf8str = NULL;
  42138. ASN1_TYPE *value = NULL;
  42139. ASN1_STRING *extval = NULL;
  42140. /* SID extension. SID data format explained here:
  42141. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  42142. */
  42143. byte SidExtension[] = {
  42144. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  42145. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  42146. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  42147. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  42148. byte expectedAltName[] = {
  42149. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  42150. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  42151. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  42152. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  42153. X509_EXTENSION *sid_ext = NULL;
  42154. ASN1_OBJECT* sid_oid = NULL;
  42155. ASN1_OCTET_STRING *sid_data = NULL;
  42156. EVP_PKEY* priv = NULL;
  42157. XFILE f = XBADFILE;
  42158. byte* pt = NULL;
  42159. BIO* bio = NULL;
  42160. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  42161. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  42162. if (f != XBADFILE) {
  42163. XFCLOSE(f);
  42164. f = XBADFILE;
  42165. }
  42166. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  42167. ExpectNotNull(gn = GENERAL_NAME_new());
  42168. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  42169. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  42170. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  42171. ExpectNotNull(value = ASN1_TYPE_new());
  42172. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  42173. if (EXPECT_FAIL()) {
  42174. ASN1_UTF8STRING_free(utf8str);
  42175. }
  42176. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  42177. if (EXPECT_FAIL()) {
  42178. ASN1_TYPE_free(value);
  42179. GENERAL_NAME_free(gn);
  42180. gn = NULL;
  42181. }
  42182. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  42183. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  42184. if (EXPECT_FAIL()) {
  42185. GENERAL_NAME_free(gn);
  42186. }
  42187. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  42188. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  42189. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  42190. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  42191. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  42192. sid_data));
  42193. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  42194. /* Ensure an empty stack doesn't raise an error. */
  42195. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  42196. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  42197. if (EXPECT_FAIL()) {
  42198. X509_EXTENSION_free(san_ext);
  42199. }
  42200. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  42201. if (EXPECT_FAIL()) {
  42202. X509_EXTENSION_free(sid_ext);
  42203. }
  42204. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  42205. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  42206. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  42207. if (f != XBADFILE)
  42208. XFCLOSE(f);
  42209. pt = der;
  42210. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42211. (const unsigned char**)&pt, derSz));
  42212. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  42213. pt = der;
  42214. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  42215. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42216. gns = NULL;
  42217. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  42218. exts = NULL;
  42219. ASN1_OBJECT_free(upn_oid);
  42220. ASN1_OBJECT_free(sid_oid);
  42221. ASN1_OCTET_STRING_free(sid_data);
  42222. X509_REQ_free(x509);
  42223. x509 = NULL;
  42224. EVP_PKEY_free(priv);
  42225. /* At this point everything used to generate what is in der is cleaned up.
  42226. * We now read back from der to confirm the extensions were inserted
  42227. * correctly. */
  42228. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  42229. ExpectNotNull(bio);
  42230. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  42231. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  42232. ExpectNotNull(x509);
  42233. BIO_free(bio);
  42234. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  42235. x509));
  42236. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  42237. /* Check the SID extension. */
  42238. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  42239. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  42240. ExpectIntEQ(extval->length, sizeof(SidExtension));
  42241. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  42242. /* Check the AltNames extension. */
  42243. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  42244. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  42245. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  42246. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  42247. 0);
  42248. /* Cleanup */
  42249. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  42250. NULL));
  42251. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42252. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  42253. ExpectIntEQ(gn->type, 0);
  42254. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  42255. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  42256. X509_REQ_free(x509);
  42257. #endif
  42258. return EXPECT_RESULT();
  42259. }
  42260. static int test_wolfSSL_X509_set_name(void)
  42261. {
  42262. EXPECT_DECLS;
  42263. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  42264. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  42265. X509* x509 = NULL;
  42266. X509_NAME* name = NULL;
  42267. ExpectNotNull(name = X509_NAME_new());
  42268. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42269. (byte*)"wolfssl.com", 11, 0, 1),
  42270. WOLFSSL_SUCCESS);
  42271. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42272. (byte*)"support@wolfssl.com", 19, -1,
  42273. 1), WOLFSSL_SUCCESS);
  42274. ExpectNotNull(x509 = X509_new());
  42275. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  42276. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  42277. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42278. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  42279. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  42280. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  42281. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  42282. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  42283. X509_free(x509);
  42284. X509_NAME_free(name);
  42285. #endif /* OPENSSL_ALL && !NO_CERTS */
  42286. return EXPECT_RESULT();
  42287. }
  42288. static int test_wolfSSL_X509_set_notAfter(void)
  42289. {
  42290. EXPECT_DECLS;
  42291. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  42292. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  42293. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  42294. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  42295. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  42296. /* Generalized time will overflow time_t if not long */
  42297. X509* x = NULL;
  42298. BIO* bio = NULL;
  42299. ASN1_TIME *asn_time = NULL;
  42300. ASN1_TIME *time_check = NULL;
  42301. const int year = 365*24*60*60;
  42302. const int day = 24*60*60;
  42303. const int hour = 60*60;
  42304. const int mini = 60;
  42305. int offset_day;
  42306. unsigned char buf[25];
  42307. time_t t;
  42308. /*
  42309. * Setup asn_time. APACHE HTTPD uses time(NULL)
  42310. */
  42311. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  42312. offset_day = 7;
  42313. /*
  42314. * Free these.
  42315. */
  42316. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  42317. ExpectNotNull(asn_time);
  42318. ExpectNotNull(x = X509_new());
  42319. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42320. /*
  42321. * Tests
  42322. */
  42323. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  42324. /* time_check is simply (ANS1_TIME*)x->notAfter */
  42325. ExpectNotNull(time_check = X509_get_notAfter(x));
  42326. /* ANS1_TIME_check validates by checking if argument can be parsed */
  42327. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  42328. /* Convert to human readable format and compare to intended date */
  42329. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  42330. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  42331. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  42332. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  42333. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  42334. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  42335. /*
  42336. * Cleanup
  42337. */
  42338. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  42339. X509_free(x);
  42340. BIO_free(bio);
  42341. #endif
  42342. return EXPECT_RESULT();
  42343. }
  42344. static int test_wolfSSL_X509_set_notBefore(void)
  42345. {
  42346. EXPECT_DECLS;
  42347. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  42348. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  42349. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  42350. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42351. X509* x = NULL;
  42352. BIO* bio = NULL;
  42353. ASN1_TIME *asn_time = NULL;
  42354. ASN1_TIME *time_check = NULL;
  42355. const int year = 365*24*60*60;
  42356. const int day = 24*60*60;
  42357. const int hour = 60*60;
  42358. const int mini = 60;
  42359. int offset_day;
  42360. unsigned char buf[25];
  42361. time_t t;
  42362. /*
  42363. * Setup asn_time. APACHE HTTPD uses time(NULL)
  42364. */
  42365. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  42366. offset_day = 7;
  42367. /*
  42368. * Free these.
  42369. */
  42370. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  42371. ExpectNotNull(asn_time);
  42372. ExpectNotNull(x = X509_new());
  42373. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42374. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  42375. /*
  42376. * Main Tests
  42377. */
  42378. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  42379. /* time_check == (ANS1_TIME*)x->notBefore */
  42380. ExpectNotNull(time_check = X509_get_notBefore(x));
  42381. /* ANS1_TIME_check validates by checking if argument can be parsed */
  42382. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  42383. /* Convert to human readable format and compare to intended date */
  42384. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  42385. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  42386. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  42387. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  42388. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  42389. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  42390. /*
  42391. * Cleanup
  42392. */
  42393. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  42394. X509_free(x);
  42395. BIO_free(bio);
  42396. #endif
  42397. return EXPECT_RESULT();
  42398. }
  42399. static int test_wolfSSL_X509_set_version(void)
  42400. {
  42401. EXPECT_DECLS;
  42402. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  42403. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  42404. X509* x509 = NULL;
  42405. long v = 2L;
  42406. long maxInt = INT_MAX;
  42407. ExpectNotNull(x509 = X509_new());
  42408. /* These should pass. */
  42409. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  42410. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  42411. /* Fail Case: When v(long) is greater than x509->version(int). */
  42412. v = maxInt+1;
  42413. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  42414. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  42415. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  42416. /* Cleanup */
  42417. X509_free(x509);
  42418. #endif
  42419. return EXPECT_RESULT();
  42420. }
  42421. #ifndef NO_BIO
  42422. static int test_wolfSSL_BIO_gets(void)
  42423. {
  42424. EXPECT_DECLS;
  42425. #if defined(OPENSSL_EXTRA)
  42426. BIO* bio = NULL;
  42427. BIO* bio2 = NULL;
  42428. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  42429. char emp[] = "";
  42430. char bio_buffer[20];
  42431. int bufferSz = 20;
  42432. #ifdef OPENSSL_ALL
  42433. BUF_MEM* emp_bm = NULL;
  42434. BUF_MEM* msg_bm = NULL;
  42435. #endif
  42436. /* try with bad args */
  42437. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  42438. #ifdef OPENSSL_ALL
  42439. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  42440. #endif
  42441. /* try with real msg */
  42442. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  42443. XMEMSET(bio_buffer, 0, bufferSz);
  42444. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  42445. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  42446. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  42447. ExpectFalse(bio2 != BIO_next(bio));
  42448. /* make buffer filled with no terminating characters */
  42449. XMEMSET(bio_buffer, 1, bufferSz);
  42450. /* BIO_gets reads a line of data */
  42451. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42452. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42453. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42454. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42455. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42456. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42457. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42458. #ifdef OPENSSL_ALL
  42459. /* test setting the mem_buf manually */
  42460. BIO_free(bio);
  42461. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  42462. ExpectNotNull(emp_bm = BUF_MEM_new());
  42463. ExpectNotNull(msg_bm = BUF_MEM_new());
  42464. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  42465. if (EXPECT_SUCCESS()) {
  42466. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  42467. msg_bm->data = NULL;
  42468. }
  42469. /* emp size is 1 for terminator */
  42470. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  42471. if (EXPECT_SUCCESS()) {
  42472. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  42473. emp_bm->data = emp;
  42474. msg_bm->data = msg;
  42475. }
  42476. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  42477. /* check reading an empty string */
  42478. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  42479. ExpectStrEQ(emp, bio_buffer);
  42480. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42481. /* BIO_gets reads a line of data */
  42482. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  42483. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42484. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42485. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42486. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42487. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42488. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42489. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42490. if (EXPECT_SUCCESS())
  42491. emp_bm->data = NULL;
  42492. BUF_MEM_free(emp_bm);
  42493. if (EXPECT_SUCCESS())
  42494. msg_bm->data = NULL;
  42495. BUF_MEM_free(msg_bm);
  42496. #endif
  42497. /* check not null terminated string */
  42498. BIO_free(bio);
  42499. bio = NULL;
  42500. msg[0] = 0x33;
  42501. msg[1] = 0x33;
  42502. msg[2] = 0x33;
  42503. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  42504. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  42505. ExpectIntEQ(bio_buffer[0], msg[0]);
  42506. ExpectIntEQ(bio_buffer[1], msg[1]);
  42507. ExpectIntNE(bio_buffer[2], msg[2]);
  42508. BIO_free(bio);
  42509. bio = NULL;
  42510. msg[3] = 0x33;
  42511. bio_buffer[3] = 0x33;
  42512. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  42513. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  42514. ExpectIntEQ(bio_buffer[0], msg[0]);
  42515. ExpectIntEQ(bio_buffer[1], msg[1]);
  42516. ExpectIntEQ(bio_buffer[2], msg[2]);
  42517. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  42518. /* check reading an empty string */
  42519. BIO_free(bio);
  42520. bio = NULL;
  42521. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  42522. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  42523. ExpectStrEQ(emp, bio_buffer);
  42524. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42525. /* check error cases */
  42526. BIO_free(bio);
  42527. bio = NULL;
  42528. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  42529. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42530. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  42531. #if !defined(NO_FILESYSTEM)
  42532. {
  42533. BIO* f_bio = NULL;
  42534. XFILE f = XBADFILE;
  42535. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  42536. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  42537. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  42538. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  42539. if (EXPECT_FAIL() && (f != XBADFILE)) {
  42540. XFCLOSE(f);
  42541. }
  42542. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  42543. BIO_free(f_bio);
  42544. f_bio = NULL;
  42545. }
  42546. #endif /* NO_FILESYSTEM */
  42547. BIO_free(bio);
  42548. bio = NULL;
  42549. BIO_free(bio2);
  42550. bio2 = NULL;
  42551. /* try with type BIO */
  42552. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  42553. sizeof(msg));
  42554. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  42555. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  42556. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  42557. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  42558. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  42559. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  42560. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  42561. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  42562. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  42563. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  42564. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  42565. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  42566. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  42567. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  42568. BIO_free(bio);
  42569. bio = NULL;
  42570. BIO_free(bio2);
  42571. bio2 = NULL;
  42572. /* check reading an empty string */
  42573. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  42574. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  42575. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  42576. ExpectStrEQ(emp, bio_buffer);
  42577. BIO_free(bio);
  42578. bio = NULL;
  42579. #endif
  42580. return EXPECT_RESULT();
  42581. }
  42582. static int test_wolfSSL_BIO_puts(void)
  42583. {
  42584. EXPECT_DECLS;
  42585. #if defined(OPENSSL_EXTRA)
  42586. BIO* bio = NULL;
  42587. char input[] = "hello\0world\n.....ok\n\0";
  42588. char output[128];
  42589. XMEMSET(output, 0, sizeof(output));
  42590. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42591. ExpectIntEQ(BIO_puts(bio, input), 5);
  42592. ExpectIntEQ(BIO_pending(bio), 5);
  42593. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  42594. ExpectIntEQ(BIO_pending(bio), 19);
  42595. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  42596. ExpectStrEQ(output, "helloworld\n");
  42597. ExpectIntEQ(BIO_pending(bio), 8);
  42598. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  42599. ExpectStrEQ(output, ".....ok\n");
  42600. ExpectIntEQ(BIO_pending(bio), 0);
  42601. ExpectIntEQ(BIO_puts(bio, ""), -1);
  42602. BIO_free(bio);
  42603. #endif
  42604. return EXPECT_RESULT();
  42605. }
  42606. static int test_wolfSSL_BIO_dump(void)
  42607. {
  42608. EXPECT_DECLS;
  42609. #if defined(OPENSSL_EXTRA)
  42610. BIO* bio;
  42611. static const unsigned char data[] = {
  42612. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  42613. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  42614. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  42615. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  42616. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  42617. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  42618. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  42619. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  42620. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  42621. 0xB4
  42622. };
  42623. /* Generated with OpenSSL. */
  42624. static const char expected[] =
  42625. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  42626. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  42627. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  42628. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  42629. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  42630. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  42631. static const char expectedAll[] =
  42632. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  42633. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  42634. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  42635. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  42636. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  42637. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  42638. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  42639. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  42640. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  42641. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  42642. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  42643. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  42644. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  42645. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  42646. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  42647. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  42648. char output[16 * 80];
  42649. int i;
  42650. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42651. /* Example key dumped. */
  42652. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  42653. sizeof(expected) - 1);
  42654. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  42655. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  42656. /* Try every possible value for a character. */
  42657. for (i = 0; i < 256; i++)
  42658. output[i] = i;
  42659. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  42660. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  42661. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  42662. BIO_free(bio);
  42663. #endif
  42664. return EXPECT_RESULT();
  42665. }
  42666. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42667. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  42668. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  42669. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  42670. {
  42671. (void)ssl;
  42672. (void)buf;
  42673. (void)sz;
  42674. (void)ctx;
  42675. return WOLFSSL_CBIO_ERR_WANT_READ;
  42676. }
  42677. #endif
  42678. static int test_wolfSSL_BIO_should_retry(void)
  42679. {
  42680. EXPECT_DECLS;
  42681. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42682. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  42683. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  42684. tcp_ready ready;
  42685. func_args server_args;
  42686. THREAD_TYPE serverThread;
  42687. SOCKET_T sockfd = 0;
  42688. WOLFSSL_CTX* ctx = NULL;
  42689. WOLFSSL* ssl = NULL;
  42690. char msg[64] = "hello wolfssl!";
  42691. char reply[1024];
  42692. int msgSz = (int)XSTRLEN(msg);
  42693. int ret;
  42694. BIO* bio = NULL;
  42695. XMEMSET(&server_args, 0, sizeof(func_args));
  42696. #ifdef WOLFSSL_TIRTOS
  42697. fdOpenSession(Task_self());
  42698. #endif
  42699. StartTCP();
  42700. InitTcpReady(&ready);
  42701. #if defined(USE_WINDOWS_API)
  42702. /* use RNG to get random port if using windows */
  42703. ready.port = GetRandomPort();
  42704. #endif
  42705. server_args.signal = &ready;
  42706. start_thread(test_server_nofail, &server_args, &serverThread);
  42707. wait_tcp_ready(&server_args);
  42708. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42709. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  42710. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  42711. #endif
  42712. ExpectIntEQ(WOLFSSL_SUCCESS,
  42713. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42714. ExpectIntEQ(WOLFSSL_SUCCESS,
  42715. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  42716. ExpectIntEQ(WOLFSSL_SUCCESS,
  42717. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  42718. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  42719. /* force retry */
  42720. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  42721. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  42722. ExpectNotNull(ssl);
  42723. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  42724. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  42725. if (EXPECT_FAIL()) {
  42726. wolfSSL_free(ssl);
  42727. ssl = NULL;
  42728. }
  42729. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  42730. ExpectIntNE(BIO_should_retry(bio), 0);
  42731. ExpectIntEQ(BIO_should_read(bio), 0);
  42732. ExpectIntEQ(BIO_should_write(bio), 0);
  42733. /* now perform successful connection */
  42734. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  42735. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  42736. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  42737. ret = wolfSSL_get_error(ssl, -1);
  42738. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  42739. ExpectIntNE(BIO_should_retry(bio), 0);
  42740. if (ret == WOLFSSL_ERROR_WANT_READ)
  42741. ExpectIntEQ(BIO_should_read(bio), 1);
  42742. else
  42743. ExpectIntEQ(BIO_should_read(bio), 0);
  42744. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  42745. ExpectIntEQ(BIO_should_write(bio), 1);
  42746. else
  42747. ExpectIntEQ(BIO_should_write(bio), 0);
  42748. }
  42749. else {
  42750. ExpectIntEQ(BIO_should_retry(bio), 0);
  42751. ExpectIntEQ(BIO_should_read(bio), 0);
  42752. ExpectIntEQ(BIO_should_write(bio), 0);
  42753. }
  42754. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  42755. XSTRLEN("I hear you fa shizzle!")), 0);
  42756. BIO_free(bio);
  42757. wolfSSL_CTX_free(ctx);
  42758. CloseSocket(sockfd);
  42759. join_thread(serverThread);
  42760. FreeTcpReady(&ready);
  42761. #ifdef WOLFSSL_TIRTOS
  42762. fdOpenSession(Task_self());
  42763. #endif
  42764. #endif
  42765. return EXPECT_RESULT();
  42766. }
  42767. static int test_wolfSSL_BIO_connect(void)
  42768. {
  42769. EXPECT_DECLS;
  42770. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42771. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  42772. tcp_ready ready;
  42773. func_args server_args;
  42774. THREAD_TYPE serverThread;
  42775. BIO *tcpBio = NULL;
  42776. BIO *sslBio = NULL;
  42777. SSL_CTX* ctx = NULL;
  42778. SSL *ssl = NULL;
  42779. SSL *sslPtr;
  42780. char msg[] = "hello wolfssl!";
  42781. char reply[30];
  42782. char buff[10] = {0};
  42783. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42784. ExpectIntEQ(WOLFSSL_SUCCESS,
  42785. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  42786. ExpectIntEQ(WOLFSSL_SUCCESS,
  42787. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  42788. ExpectIntEQ(WOLFSSL_SUCCESS,
  42789. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  42790. /* Setup server */
  42791. XMEMSET(&server_args, 0, sizeof(func_args));
  42792. StartTCP();
  42793. InitTcpReady(&ready);
  42794. #if defined(USE_WINDOWS_API)
  42795. /* use RNG to get random port if using windows */
  42796. ready.port = GetRandomPort();
  42797. #endif
  42798. server_args.signal = &ready;
  42799. start_thread(test_server_nofail, &server_args, &serverThread);
  42800. wait_tcp_ready(&server_args);
  42801. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  42802. /* Start the test proper */
  42803. /* Setup the TCP BIO */
  42804. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  42805. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  42806. /* Setup the SSL object */
  42807. ExpectNotNull(ssl = SSL_new(ctx));
  42808. SSL_set_connect_state(ssl);
  42809. /* Setup the SSL BIO */
  42810. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  42811. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  42812. if (EXPECT_FAIL()) {
  42813. wolfSSL_free(ssl);
  42814. }
  42815. /* Verify that BIO_get_ssl works. */
  42816. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  42817. ExpectPtrEq(ssl, sslPtr);
  42818. /* Link BIO's so that sslBio uses tcpBio for IO */
  42819. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  42820. /* Do TCP connect */
  42821. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  42822. /* Do TLS handshake */
  42823. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  42824. /* Test writing */
  42825. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  42826. /* Expect length of default wolfSSL reply */
  42827. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  42828. /* Clean it all up */
  42829. BIO_free_all(sslBio);
  42830. /* Server clean up */
  42831. join_thread(serverThread);
  42832. FreeTcpReady(&ready);
  42833. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  42834. * after. */
  42835. XMEMSET(&server_args, 0, sizeof(func_args));
  42836. StartTCP();
  42837. InitTcpReady(&ready);
  42838. #if defined(USE_WINDOWS_API)
  42839. /* use RNG to get random port if using windows */
  42840. ready.port = GetRandomPort();
  42841. #endif
  42842. server_args.signal = &ready;
  42843. start_thread(test_server_nofail, &server_args, &serverThread);
  42844. wait_tcp_ready(&server_args);
  42845. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  42846. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  42847. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  42848. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  42849. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  42850. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  42851. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  42852. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  42853. /* Attempt to close the TLS connection gracefully. */
  42854. BIO_ssl_shutdown(sslBio);
  42855. BIO_free_all(sslBio);
  42856. join_thread(serverThread);
  42857. FreeTcpReady(&ready);
  42858. SSL_CTX_free(ctx);
  42859. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  42860. wc_ecc_fp_free(); /* free per thread cache */
  42861. #endif
  42862. #endif
  42863. return EXPECT_RESULT();
  42864. }
  42865. static int test_wolfSSL_BIO_tls(void)
  42866. {
  42867. EXPECT_DECLS;
  42868. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  42869. SSL_CTX* ctx = NULL;
  42870. SSL *ssl = NULL;
  42871. BIO *readBio = NULL;
  42872. BIO *writeBio = NULL;
  42873. int ret;
  42874. int err = 0;
  42875. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42876. ExpectNotNull(ssl = SSL_new(ctx));
  42877. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  42878. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  42879. /* Qt reads data from write-bio,
  42880. * then writes the read data into plain packet.
  42881. * Qt reads data from plain packet,
  42882. * then writes the read data into read-bio.
  42883. */
  42884. SSL_set_bio(ssl, readBio, writeBio);
  42885. do {
  42886. #ifdef WOLFSSL_ASYNC_CRYPT
  42887. if (err == WC_PENDING_E) {
  42888. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  42889. if (ret < 0) { break; } else if (ret == 0) { continue; }
  42890. }
  42891. #endif
  42892. ret = SSL_connect(ssl);
  42893. err = SSL_get_error(ssl, 0);
  42894. } while (err == WC_PENDING_E);
  42895. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  42896. /* in this use case, should return WANT READ
  42897. * so that Qt will read the data from plain packet for next state.
  42898. */
  42899. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  42900. SSL_free(ssl);
  42901. SSL_CTX_free(ctx);
  42902. #endif
  42903. return EXPECT_RESULT();
  42904. }
  42905. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42906. defined(HAVE_HTTP_CLIENT)
  42907. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  42908. {
  42909. BIO* clientBio;
  42910. SSL* sslClient;
  42911. SSL_CTX* ctx;
  42912. char connectAddr[20]; /* IP + port */;
  42913. (void)args;
  42914. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  42915. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  42916. AssertIntEQ(BIO_do_connect(clientBio), 1);
  42917. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42918. AssertNotNull(sslClient = SSL_new(ctx));
  42919. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  42920. SSL_set_bio(sslClient, clientBio, clientBio);
  42921. AssertIntEQ(SSL_connect(sslClient), 1);
  42922. SSL_free(sslClient);
  42923. SSL_CTX_free(ctx);
  42924. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  42925. wc_ecc_fp_free(); /* free per thread cache */
  42926. #endif
  42927. WOLFSSL_RETURN_FROM_THREAD(0);
  42928. }
  42929. #endif
  42930. static int test_wolfSSL_BIO_accept(void)
  42931. {
  42932. EXPECT_DECLS;
  42933. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  42934. defined(HAVE_HTTP_CLIENT)
  42935. BIO* serverBindBio = NULL;
  42936. BIO* serverAcceptBio = NULL;
  42937. SSL* sslServer = NULL;
  42938. SSL_CTX* ctx = NULL;
  42939. func_args args;
  42940. THREAD_TYPE thread;
  42941. char port[10]; /* 10 bytes should be enough to store the string
  42942. * representation of the port */
  42943. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  42944. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  42945. /* First BIO_do_accept binds the port */
  42946. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  42947. XMEMSET(&args, 0, sizeof(func_args));
  42948. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  42949. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  42950. /* Let's plug it into SSL to test */
  42951. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  42952. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  42953. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  42954. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  42955. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  42956. ExpectNotNull(sslServer = SSL_new(ctx));
  42957. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  42958. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  42959. ExpectIntEQ(SSL_accept(sslServer), 1);
  42960. join_thread(thread);
  42961. BIO_free(serverBindBio);
  42962. SSL_free(sslServer);
  42963. SSL_CTX_free(ctx);
  42964. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  42965. wc_ecc_fp_free(); /* free per thread cache */
  42966. #endif
  42967. #endif
  42968. return EXPECT_RESULT();
  42969. }
  42970. static int test_wolfSSL_BIO_write(void)
  42971. {
  42972. EXPECT_DECLS;
  42973. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  42974. BIO* bio = NULL;
  42975. BIO* bio64 = NULL;
  42976. BIO* bio_mem = NULL;
  42977. BIO* ptr = NULL;
  42978. int sz;
  42979. char msg[] = "conversion test";
  42980. char out[40];
  42981. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  42982. void* bufPtr = NULL;
  42983. BUF_MEM* buf = NULL;
  42984. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  42985. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  42986. if (EXPECT_FAIL()) {
  42987. BIO_free(bio64);
  42988. }
  42989. /* now should convert to base64 then write to memory */
  42990. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  42991. BIO_flush(bio);
  42992. /* test BIO chain */
  42993. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  42994. ExpectNotNull(buf);
  42995. ExpectIntEQ(buf->length, 25);
  42996. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  42997. ExpectPtrEq(buf->data, bufPtr);
  42998. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  42999. sz = sizeof(out);
  43000. XMEMSET(out, 0, sz);
  43001. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  43002. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  43003. /* write then read should return the same message */
  43004. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43005. sz = sizeof(out);
  43006. XMEMSET(out, 0, sz);
  43007. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  43008. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  43009. /* now try encoding with no line ending */
  43010. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  43011. #ifdef HAVE_EX_DATA
  43012. BIO_set_ex_data(bio64, 0, (void*) "data");
  43013. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  43014. #endif
  43015. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43016. BIO_flush(bio);
  43017. sz = sizeof(out);
  43018. XMEMSET(out, 0, sz);
  43019. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  43020. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  43021. BIO_free_all(bio); /* frees bio64 also */
  43022. bio = NULL;
  43023. /* test with more than one bio64 in list */
  43024. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  43025. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  43026. if (EXPECT_FAIL()) {
  43027. BIO_free(bio64);
  43028. bio64 = NULL;
  43029. }
  43030. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  43031. ExpectNotNull(BIO_push(bio64, bio_mem));
  43032. if (EXPECT_FAIL()) {
  43033. BIO_free(bio_mem);
  43034. }
  43035. /* now should convert to base64 when stored and then decode with read */
  43036. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  43037. BIO_flush(bio);
  43038. sz = sizeof(out);
  43039. XMEMSET(out, 0, sz);
  43040. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  43041. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  43042. BIO_clear_flags(bio64, ~0);
  43043. BIO_set_retry_read(bio);
  43044. BIO_free_all(bio); /* frees bio64s also */
  43045. bio = NULL;
  43046. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  43047. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  43048. BIO_free(bio);
  43049. #endif
  43050. return EXPECT_RESULT();
  43051. }
  43052. static int test_wolfSSL_BIO_printf(void)
  43053. {
  43054. EXPECT_DECLS;
  43055. #if defined(OPENSSL_ALL)
  43056. BIO* bio = NULL;
  43057. int sz = 7;
  43058. char msg[] = "TLS 1.3 for the world";
  43059. char out[60];
  43060. char expected[] = "TLS 1.3 for the world : sz = 7";
  43061. XMEMSET(out, 0, sizeof(out));
  43062. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  43063. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  43064. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  43065. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  43066. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  43067. BIO_free(bio);
  43068. #endif
  43069. return EXPECT_RESULT();
  43070. }
  43071. static int test_wolfSSL_BIO_f_md(void)
  43072. {
  43073. EXPECT_DECLS;
  43074. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  43075. BIO* bio = NULL;
  43076. BIO* mem = NULL;
  43077. char msg[] = "message to hash";
  43078. char out[60];
  43079. EVP_MD_CTX* ctx = NULL;
  43080. const unsigned char testKey[] =
  43081. {
  43082. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43083. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  43084. 0x0b, 0x0b, 0x0b, 0x0b
  43085. };
  43086. const char testData[] = "Hi There";
  43087. const unsigned char testResult[] =
  43088. {
  43089. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  43090. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  43091. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  43092. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  43093. };
  43094. const unsigned char expectedHash[] =
  43095. {
  43096. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  43097. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  43098. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  43099. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  43100. };
  43101. const unsigned char emptyHash[] =
  43102. {
  43103. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  43104. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  43105. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  43106. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  43107. };
  43108. unsigned char check[sizeof(testResult) + 1];
  43109. size_t checkSz = -1;
  43110. EVP_PKEY* key = NULL;
  43111. XMEMSET(out, 0, sizeof(out));
  43112. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43113. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  43114. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  43115. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  43116. /* should not be able to write/read yet since just digest wrapper and no
  43117. * data is passing through the bio */
  43118. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  43119. ExpectIntEQ(BIO_pending(bio), 0);
  43120. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  43121. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  43122. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  43123. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  43124. BIO_reset(bio);
  43125. /* append BIO mem to bio in order to read/write */
  43126. ExpectNotNull(bio = BIO_push(bio, mem));
  43127. XMEMSET(out, 0, sizeof(out));
  43128. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  43129. ExpectIntEQ(BIO_pending(bio), 16);
  43130. /* this just reads the message and does not hash it (gets calls final) */
  43131. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  43132. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  43133. /* create a message digest using BIO */
  43134. XMEMSET(out, 0, sizeof(out));
  43135. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  43136. ExpectIntEQ(BIO_pending(mem), 16);
  43137. ExpectIntEQ(BIO_pending(bio), 16);
  43138. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  43139. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  43140. BIO_free(bio);
  43141. bio = NULL;
  43142. BIO_free(mem);
  43143. mem = NULL;
  43144. /* test with HMAC */
  43145. XMEMSET(out, 0, sizeof(out));
  43146. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43147. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  43148. BIO_get_md_ctx(bio, &ctx);
  43149. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  43150. (int)sizeof(testKey)));
  43151. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  43152. ExpectNotNull(bio = BIO_push(bio, mem));
  43153. BIO_write(bio, testData, (int)strlen(testData));
  43154. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  43155. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  43156. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  43157. EVP_PKEY_free(key);
  43158. BIO_free(bio);
  43159. BIO_free(mem);
  43160. #endif
  43161. return EXPECT_RESULT();
  43162. }
  43163. static int test_wolfSSL_BIO_up_ref(void)
  43164. {
  43165. EXPECT_DECLS;
  43166. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  43167. BIO* bio = NULL;
  43168. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  43169. ExpectIntEQ(BIO_up_ref(NULL), 0);
  43170. ExpectIntEQ(BIO_up_ref(bio), 1);
  43171. BIO_free(bio);
  43172. ExpectIntEQ(BIO_up_ref(bio), 1);
  43173. BIO_free(bio);
  43174. BIO_free(bio);
  43175. #endif
  43176. return EXPECT_RESULT();
  43177. }
  43178. static int test_wolfSSL_BIO_reset(void)
  43179. {
  43180. EXPECT_DECLS;
  43181. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  43182. BIO* bio = NULL;
  43183. byte buf[16];
  43184. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  43185. (word32)XSTRLEN("secure your data")));
  43186. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  43187. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  43188. XMEMSET(buf, 0, 16);
  43189. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  43190. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  43191. /* You cannot write to MEM BIO with read-only mode. */
  43192. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  43193. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  43194. XMEMSET(buf, 0, 16);
  43195. ExpectIntEQ(BIO_reset(bio), 0);
  43196. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  43197. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  43198. BIO_free(bio);
  43199. #endif
  43200. return EXPECT_RESULT();
  43201. }
  43202. #endif /* !NO_BIO */
  43203. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43204. /* test that the callback arg is correct */
  43205. static int certCbArg = 0;
  43206. static int certCb(WOLFSSL* ssl, void* arg)
  43207. {
  43208. if (ssl == NULL || arg != &certCbArg)
  43209. return 0;
  43210. if (wolfSSL_is_server(ssl)) {
  43211. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  43212. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43213. return 0;
  43214. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  43215. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43216. return 0;
  43217. }
  43218. else {
  43219. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  43220. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43221. return 0;
  43222. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  43223. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  43224. return 0;
  43225. }
  43226. return 1;
  43227. }
  43228. static int certSetupCb(WOLFSSL_CTX* ctx)
  43229. {
  43230. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  43231. return TEST_SUCCESS;
  43232. }
  43233. /**
  43234. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  43235. * to stop certificate and key loading
  43236. */
  43237. static int certClearCb(WOLFSSL* ssl)
  43238. {
  43239. /* Clear the loaded certs to force the callbacks to set them up */
  43240. SSL_certs_clear(ssl);
  43241. return TEST_SUCCESS;
  43242. }
  43243. #endif
  43244. static int test_wolfSSL_cert_cb(void)
  43245. {
  43246. EXPECT_DECLS;
  43247. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43248. test_ssl_cbf func_cb_client;
  43249. test_ssl_cbf func_cb_server;
  43250. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  43251. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  43252. func_cb_client.ctx_ready = certSetupCb;
  43253. func_cb_client.ssl_ready = certClearCb;
  43254. func_cb_server.ctx_ready = certSetupCb;
  43255. func_cb_server.ssl_ready = certClearCb;
  43256. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  43257. &func_cb_server, NULL), TEST_SUCCESS);
  43258. #endif
  43259. return EXPECT_RESULT();
  43260. }
  43261. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43262. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  43263. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  43264. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  43265. {
  43266. EXPECT_DECLS;
  43267. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  43268. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  43269. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  43270. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  43271. return EXPECT_RESULT();
  43272. }
  43273. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  43274. {
  43275. const byte* suites = NULL;
  43276. word16 suiteSz = 0;
  43277. const byte* hashSigAlgo = NULL;
  43278. word16 hashSigAlgoSz = 0;
  43279. word16 idx = 0;
  43280. int haveRSA = 0;
  43281. int haveECC = 0;
  43282. (void)arg;
  43283. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  43284. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  43285. return 0;
  43286. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  43287. hashSigAlgoSz == 0)
  43288. return 0;
  43289. for (idx = 0; idx < suiteSz; idx += 2) {
  43290. WOLFSSL_CIPHERSUITE_INFO info =
  43291. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  43292. if (info.rsaAuth)
  43293. haveRSA = 1;
  43294. else if (info.eccAuth)
  43295. haveECC = 1;
  43296. }
  43297. if (hashSigAlgoSz > 0) {
  43298. /* sigalgs extension takes precedence over ciphersuites */
  43299. haveRSA = 0;
  43300. haveECC = 0;
  43301. }
  43302. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  43303. int hashAlgo = 0;
  43304. int sigAlgo = 0;
  43305. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  43306. &hashAlgo, &sigAlgo) != 0)
  43307. return 0;
  43308. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  43309. haveRSA = 1;
  43310. else if (sigAlgo == ECDSAk)
  43311. haveECC = 1;
  43312. }
  43313. if (haveRSA) {
  43314. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  43315. != WOLFSSL_SUCCESS)
  43316. return 0;
  43317. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  43318. != WOLFSSL_SUCCESS)
  43319. return 0;
  43320. }
  43321. else if (haveECC) {
  43322. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  43323. != WOLFSSL_SUCCESS)
  43324. return 0;
  43325. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  43326. != WOLFSSL_SUCCESS)
  43327. return 0;
  43328. }
  43329. return 1;
  43330. }
  43331. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  43332. {
  43333. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  43334. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  43335. return TEST_SUCCESS;
  43336. }
  43337. #endif
  43338. /* Testing dynamic ciphers offered by client */
  43339. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  43340. {
  43341. EXPECT_DECLS;
  43342. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  43343. test_ssl_cbf func_cb_client;
  43344. test_ssl_cbf func_cb_server;
  43345. struct {
  43346. method_provider client_meth;
  43347. const char* client_ciphers;
  43348. const char* client_sigalgs;
  43349. const char* client_ca;
  43350. method_provider server_meth;
  43351. } test_params[] = {
  43352. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  43353. #ifdef WOLFSSL_TLS13
  43354. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  43355. {wolfTLSv1_3_client_method,
  43356. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  43357. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  43358. #endif
  43359. #ifdef HAVE_ECC
  43360. {wolfTLSv1_3_client_method,
  43361. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  43362. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  43363. #endif
  43364. #endif
  43365. #ifndef WOLFSSL_NO_TLS12
  43366. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  43367. {wolfTLSv1_2_client_method,
  43368. "DHE-RSA-AES128-GCM-SHA256",
  43369. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  43370. #endif
  43371. #ifdef HAVE_ECC
  43372. {wolfTLSv1_2_client_method,
  43373. "ECDHE-ECDSA-AES128-GCM-SHA256",
  43374. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  43375. #endif
  43376. #endif
  43377. #endif
  43378. };
  43379. size_t i;
  43380. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  43381. if (testCount > 0) {
  43382. for (i = 0; i < testCount; i++) {
  43383. printf("\tTesting %s ciphers with %s sigalgs\n",
  43384. test_params[i].client_ciphers,
  43385. test_params[i].client_sigalgs);
  43386. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  43387. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  43388. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  43389. test_params[i].client_ciphers;
  43390. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  43391. test_params[i].client_sigalgs;
  43392. func_cb_client.method = test_params[i].client_meth;
  43393. func_cb_client.caPemFile = test_params[i].client_ca;
  43394. func_cb_client.ctx_ready =
  43395. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  43396. func_cb_server.ctx_ready =
  43397. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  43398. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  43399. func_cb_server.method = test_params[i].server_meth;
  43400. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  43401. &func_cb_server, NULL), TEST_SUCCESS);
  43402. }
  43403. }
  43404. #endif
  43405. return EXPECT_RESULT();
  43406. }
  43407. static int test_wolfSSL_ciphersuite_auth(void)
  43408. {
  43409. EXPECT_DECLS;
  43410. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  43411. WOLFSSL_CIPHERSUITE_INFO info;
  43412. (void)info;
  43413. #ifndef WOLFSSL_NO_TLS12
  43414. #ifdef HAVE_CHACHA
  43415. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43416. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  43417. ExpectIntEQ(info.rsaAuth, 1);
  43418. ExpectIntEQ(info.eccAuth, 0);
  43419. ExpectIntEQ(info.eccStatic, 0);
  43420. ExpectIntEQ(info.psk, 0);
  43421. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43422. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  43423. ExpectIntEQ(info.rsaAuth, 0);
  43424. ExpectIntEQ(info.eccAuth, 1);
  43425. ExpectIntEQ(info.eccStatic, 0);
  43426. ExpectIntEQ(info.psk, 0);
  43427. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  43428. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  43429. ExpectIntEQ(info.rsaAuth, 0);
  43430. ExpectIntEQ(info.eccAuth, 0);
  43431. ExpectIntEQ(info.eccStatic, 0);
  43432. ExpectIntEQ(info.psk, 1);
  43433. #endif
  43434. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  43435. #ifndef NO_RSA
  43436. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43437. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  43438. ExpectIntEQ(info.rsaAuth, 1);
  43439. ExpectIntEQ(info.eccAuth, 0);
  43440. ExpectIntEQ(info.eccStatic, 0);
  43441. ExpectIntEQ(info.psk, 0);
  43442. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43443. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  43444. ExpectIntEQ(info.rsaAuth, 1);
  43445. ExpectIntEQ(info.eccAuth, 0);
  43446. ExpectIntEQ(info.eccStatic, 1);
  43447. ExpectIntEQ(info.psk, 0);
  43448. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43449. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  43450. ExpectIntEQ(info.rsaAuth, 1);
  43451. ExpectIntEQ(info.eccAuth, 0);
  43452. ExpectIntEQ(info.eccStatic, 1);
  43453. ExpectIntEQ(info.psk, 0);
  43454. #endif
  43455. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43456. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  43457. ExpectIntEQ(info.rsaAuth, 0);
  43458. ExpectIntEQ(info.eccAuth, 1);
  43459. ExpectIntEQ(info.eccStatic, 0);
  43460. ExpectIntEQ(info.psk, 0);
  43461. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  43462. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  43463. ExpectIntEQ(info.rsaAuth, 0);
  43464. ExpectIntEQ(info.eccAuth, 1);
  43465. ExpectIntEQ(info.eccStatic, 1);
  43466. ExpectIntEQ(info.psk, 0);
  43467. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  43468. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  43469. ExpectIntEQ(info.rsaAuth, 0);
  43470. ExpectIntEQ(info.eccAuth, 0);
  43471. ExpectIntEQ(info.eccStatic, 0);
  43472. ExpectIntEQ(info.psk, 1);
  43473. #endif
  43474. #endif
  43475. #ifdef WOLFSSL_TLS13
  43476. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  43477. TLS_AES_128_GCM_SHA256);
  43478. ExpectIntEQ(info.rsaAuth, 0);
  43479. ExpectIntEQ(info.eccAuth, 0);
  43480. ExpectIntEQ(info.eccStatic, 0);
  43481. ExpectIntEQ(info.psk, 0);
  43482. #endif
  43483. #endif
  43484. return EXPECT_RESULT();
  43485. }
  43486. static int test_wolfSSL_sigalg_info(void)
  43487. {
  43488. EXPECT_DECLS;
  43489. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  43490. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  43491. word16 len = 0;
  43492. word16 idx = 0;
  43493. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  43494. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  43495. for (idx = 0; idx < len; idx += 2) {
  43496. int hashAlgo = 0;
  43497. int sigAlgo = 0;
  43498. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  43499. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  43500. ExpectIntNE(hashAlgo, 0);
  43501. ExpectIntNE(sigAlgo, 0);
  43502. }
  43503. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  43504. 0xFFFFFFFF, &len);
  43505. for (idx = 0; idx < len; idx += 2) {
  43506. int hashAlgo = 0;
  43507. int sigAlgo = 0;
  43508. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  43509. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  43510. ExpectIntNE(hashAlgo, 0);
  43511. }
  43512. #endif
  43513. return EXPECT_RESULT();
  43514. }
  43515. static int test_wolfSSL_SESSION(void)
  43516. {
  43517. EXPECT_DECLS;
  43518. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43519. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43520. !defined(NO_SESSION_CACHE)
  43521. WOLFSSL* ssl = NULL;
  43522. WOLFSSL_CTX* ctx = NULL;
  43523. WOLFSSL_SESSION* sess = NULL;
  43524. WOLFSSL_SESSION* sess_copy = NULL;
  43525. #ifdef OPENSSL_EXTRA
  43526. #ifdef HAVE_EXT_CACHE
  43527. unsigned char* sessDer = NULL;
  43528. unsigned char* ptr = NULL;
  43529. int sz = 0;
  43530. #endif
  43531. const unsigned char context[] = "user app context";
  43532. unsigned int contextSz = (unsigned int)sizeof(context);
  43533. #endif
  43534. int ret = 0, err = 0;
  43535. SOCKET_T sockfd;
  43536. tcp_ready ready;
  43537. func_args server_args;
  43538. THREAD_TYPE serverThread;
  43539. char msg[80];
  43540. const char* sendGET = "GET";
  43541. /* TLS v1.3 requires session tickets */
  43542. /* CHACHA and POLY1305 required for myTicketEncCb */
  43543. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  43544. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  43545. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  43546. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  43547. #else
  43548. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43549. #endif
  43550. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  43551. WOLFSSL_FILETYPE_PEM));
  43552. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  43553. WOLFSSL_FILETYPE_PEM));
  43554. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  43555. WOLFSSL_SUCCESS);
  43556. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43557. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  43558. #endif
  43559. #ifdef HAVE_SESSION_TICKET
  43560. /* Use session tickets, for ticket tests below */
  43561. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  43562. #endif
  43563. XMEMSET(&server_args, 0, sizeof(func_args));
  43564. #ifdef WOLFSSL_TIRTOS
  43565. fdOpenSession(Task_self());
  43566. #endif
  43567. StartTCP();
  43568. InitTcpReady(&ready);
  43569. #if defined(USE_WINDOWS_API)
  43570. /* use RNG to get random port if using windows */
  43571. ready.port = GetRandomPort();
  43572. #endif
  43573. server_args.signal = &ready;
  43574. start_thread(test_server_nofail, &server_args, &serverThread);
  43575. wait_tcp_ready(&server_args);
  43576. /* client connection */
  43577. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43578. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  43579. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  43580. #ifdef WOLFSSL_ASYNC_CRYPT
  43581. err = 0; /* Reset error */
  43582. #endif
  43583. do {
  43584. #ifdef WOLFSSL_ASYNC_CRYPT
  43585. if (err == WC_PENDING_E) {
  43586. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43587. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43588. }
  43589. #endif
  43590. ret = wolfSSL_connect(ssl);
  43591. err = wolfSSL_get_error(ssl, 0);
  43592. } while (err == WC_PENDING_E);
  43593. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  43594. #ifdef WOLFSSL_ASYNC_CRYPT
  43595. err = 0; /* Reset error */
  43596. #endif
  43597. do {
  43598. #ifdef WOLFSSL_ASYNC_CRYPT
  43599. if (err == WC_PENDING_E) {
  43600. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43601. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43602. }
  43603. #endif
  43604. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  43605. err = wolfSSL_get_error(ssl, 0);
  43606. } while (err == WC_PENDING_E);
  43607. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  43608. #ifdef WOLFSSL_ASYNC_CRYPT
  43609. err = 0; /* Reset error */
  43610. #endif
  43611. do {
  43612. #ifdef WOLFSSL_ASYNC_CRYPT
  43613. if (err == WC_PENDING_E) {
  43614. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  43615. if (ret < 0) { break; } else if (ret == 0) { continue; }
  43616. }
  43617. #endif
  43618. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  43619. err = wolfSSL_get_error(ssl, 0);
  43620. } while (err == WC_PENDING_E);
  43621. ExpectIntEQ(ret, 23);
  43622. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  43623. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  43624. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  43625. #ifdef HAVE_EXT_CACHE
  43626. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  43627. * HAVE_EXT_CACHE we get new objects each time */
  43628. #endif
  43629. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  43630. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  43631. sess = wolfSSL_get_session(ssl);
  43632. #ifdef OPENSSL_EXTRA
  43633. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  43634. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  43635. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  43636. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  43637. #ifdef HAVE_SESSION_TICKET
  43638. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  43639. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  43640. SESSION_TICKET_HINT_DEFAULT);
  43641. #else
  43642. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  43643. #endif
  43644. #else
  43645. (void)sess;
  43646. #endif /* OPENSSL_EXTRA */
  43647. /* Retain copy of the session for later testing */
  43648. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  43649. wolfSSL_shutdown(ssl);
  43650. wolfSSL_free(ssl); ssl = NULL;
  43651. CloseSocket(sockfd);
  43652. join_thread(serverThread);
  43653. FreeTcpReady(&ready);
  43654. #ifdef WOLFSSL_TIRTOS
  43655. fdOpenSession(Task_self());
  43656. #endif
  43657. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  43658. {
  43659. X509 *x509 = NULL;
  43660. char buf[30];
  43661. int bufSz = 0;
  43662. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  43663. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  43664. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  43665. sizeof(buf))), 0);
  43666. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  43667. if (bufSz == 7) {
  43668. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  43669. }
  43670. if (bufSz == 16) {
  43671. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  43672. }
  43673. }
  43674. #endif
  43675. #ifdef HAVE_EXT_CACHE
  43676. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  43677. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  43678. sess_copy = NULL;
  43679. #endif
  43680. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  43681. /* get session from DER and update the timeout */
  43682. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  43683. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  43684. wolfSSL_SESSION_free(sess); sess = NULL;
  43685. sess = NULL;
  43686. ptr = sessDer;
  43687. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  43688. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  43689. (const unsigned char**)&ptr, sz));
  43690. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  43691. sessDer = NULL;
  43692. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  43693. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  43694. #endif
  43695. /* successful set session test */
  43696. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43697. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  43698. #ifdef HAVE_SESSION_TICKET
  43699. /* Test set/get session ticket */
  43700. {
  43701. const char* ticket = "This is a session ticket";
  43702. char buf[64] = {0};
  43703. word32 bufSz = (word32)sizeof(buf);
  43704. ExpectIntEQ(SSL_SUCCESS,
  43705. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  43706. (word32)XSTRLEN(ticket)));
  43707. ExpectIntEQ(SSL_SUCCESS,
  43708. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  43709. ExpectStrEQ(ticket, buf);
  43710. }
  43711. #endif
  43712. #ifdef OPENSSL_EXTRA
  43713. /* session timeout case */
  43714. /* make the session to be expired */
  43715. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  43716. XSLEEP_MS(1200);
  43717. /* SSL_set_session should reject specified session but return success
  43718. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  43719. */
  43720. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  43721. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  43722. #else
  43723. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  43724. #endif
  43725. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  43726. #ifdef WOLFSSL_SESSION_ID_CTX
  43727. /* fail case with miss match session context IDs (use compatibility API) */
  43728. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  43729. SSL_SUCCESS);
  43730. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  43731. wolfSSL_free(ssl); ssl = NULL;
  43732. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  43733. SSL_FAILURE);
  43734. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  43735. SSL_SUCCESS);
  43736. ExpectNotNull(ssl = wolfSSL_new(ctx));
  43737. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  43738. #endif
  43739. #endif /* OPENSSL_EXTRA */
  43740. wolfSSL_free(ssl);
  43741. wolfSSL_SESSION_free(sess);
  43742. wolfSSL_CTX_free(ctx);
  43743. #endif
  43744. return EXPECT_RESULT();
  43745. }
  43746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43747. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43748. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  43749. !defined(WOLFSSL_NO_TLS12)
  43750. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  43751. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  43752. {
  43753. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  43754. /* returns previous timeout value */
  43755. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  43756. #else
  43757. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  43758. #endif
  43759. }
  43760. /* set the session to timeout in a second */
  43761. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  43762. {
  43763. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  43764. }
  43765. /* store the client side session from the first successful connection */
  43766. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  43767. {
  43768. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  43769. NULL); /* ref count 1 */
  43770. }
  43771. /* wait till session is expired then set it in the WOLFSSL struct for use */
  43772. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  43773. {
  43774. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  43775. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43776. WOLFSSL_SUCCESS);
  43777. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  43778. }
  43779. /* set expired session in the WOLFSSL struct for use */
  43780. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  43781. {
  43782. XSLEEP_MS(1200); /* wait a second for session to expire */
  43783. /* set the expired session, call to set session fails but continuing on
  43784. after failure should be handled here */
  43785. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  43786. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43787. WOLFSSL_SUCCESS);
  43788. #else
  43789. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  43790. WOLFSSL_SUCCESS);
  43791. #endif
  43792. }
  43793. /* check that the expired session was not reused */
  43794. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  43795. {
  43796. /* since the session has expired it should not have been reused */
  43797. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  43798. }
  43799. #endif
  43800. static int test_wolfSSL_SESSION_expire_downgrade(void)
  43801. {
  43802. EXPECT_DECLS;
  43803. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  43804. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  43805. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  43806. !defined(WOLFSSL_NO_TLS12)
  43807. WOLFSSL_CTX* ctx = NULL;
  43808. callback_functions server_cbf, client_cbf;
  43809. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  43810. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  43811. /* force server side to use TLS 1.2 */
  43812. server_cbf.ctx = ctx;
  43813. server_cbf.method = wolfTLSv1_2_server_method;
  43814. client_cbf.method = wolfSSLv23_client_method;
  43815. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43816. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  43817. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  43818. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43819. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43820. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43821. /* set the previously created session and wait till expired */
  43822. server_cbf.ctx = ctx;
  43823. client_cbf.method = wolfSSLv23_client_method;
  43824. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43825. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  43826. client_cbf.on_result =
  43827. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  43828. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43829. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43830. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43831. /* set the previously created expired session */
  43832. server_cbf.ctx = ctx;
  43833. client_cbf.method = wolfSSLv23_client_method;
  43834. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  43835. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  43836. client_cbf.on_result =
  43837. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  43838. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  43839. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  43840. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  43841. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  43842. wolfSSL_CTX_free(ctx);
  43843. #endif
  43844. return EXPECT_RESULT();
  43845. }
  43846. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  43847. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  43848. static int clientSessRemCountMalloc = 0;
  43849. static int serverSessRemCountMalloc = 0;
  43850. static int clientSessRemCountFree = 0;
  43851. static int serverSessRemCountFree = 0;
  43852. static WOLFSSL_CTX* serverSessCtx = NULL;
  43853. static WOLFSSL_SESSION* serverSess = NULL;
  43854. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43855. !defined(NO_SESSION_CACHE_REF)
  43856. static WOLFSSL_CTX* clientSessCtx = NULL;
  43857. static WOLFSSL_SESSION* clientSess = NULL;
  43858. #endif
  43859. static int serverSessRemIdx = 3;
  43860. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  43861. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  43862. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  43863. {
  43864. int* side;
  43865. (void)ctx;
  43866. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  43867. if (side != NULL) {
  43868. if (*side == WOLFSSL_CLIENT_END)
  43869. clientSessRemCountFree++;
  43870. else
  43871. serverSessRemCountFree++;
  43872. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  43873. }
  43874. }
  43875. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  43876. {
  43877. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  43878. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  43879. !defined(NO_SESSION_CACHE_REF)
  43880. {
  43881. EXPECT_DECLS;
  43882. /* Allow downgrade, set min version, and disable TLS 1.3.
  43883. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  43884. * reference to the session cache. But with WOLFSSL_TLS13 and without
  43885. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  43886. * the session in the cache. In this case we need to downgrade to
  43887. * previous versions to just use the legacy session ID field. */
  43888. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  43889. SSL_SUCCESS);
  43890. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  43891. SSL_SUCCESS);
  43892. return EXPECT_RESULT();
  43893. }
  43894. #else
  43895. return TEST_SUCCESS;
  43896. #endif
  43897. }
  43898. static int SessRemSslSetupCb(WOLFSSL* ssl)
  43899. {
  43900. EXPECT_DECLS;
  43901. int* side;
  43902. if (SSL_is_server(ssl)) {
  43903. side = &sessRemCtx_Server;
  43904. serverSessRemCountMalloc++;
  43905. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  43906. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  43907. SSL_SUCCESS);
  43908. }
  43909. else {
  43910. side = &sessRemCtx_Client;
  43911. clientSessRemCountMalloc++;
  43912. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43913. !defined(NO_SESSION_CACHE_REF)
  43914. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  43915. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  43916. SSL_SUCCESS);
  43917. #endif
  43918. }
  43919. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  43920. serverSessRemIdx, side), SSL_SUCCESS);
  43921. return EXPECT_RESULT();
  43922. }
  43923. #endif
  43924. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  43925. {
  43926. EXPECT_DECLS;
  43927. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  43928. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  43929. /* Check that the remove callback gets called for external data in a
  43930. * session object */
  43931. test_ssl_cbf func_cb;
  43932. XMEMSET(&func_cb, 0, sizeof(func_cb));
  43933. func_cb.ctx_ready = SessRemCtxSetupCb;
  43934. func_cb.on_result = SessRemSslSetupCb;
  43935. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  43936. NULL), TEST_SUCCESS);
  43937. /* Both should have been allocated */
  43938. ExpectIntEQ(clientSessRemCountMalloc, 1);
  43939. ExpectIntEQ(serverSessRemCountMalloc, 1);
  43940. /* This should not be called yet. Session wasn't evicted from cache yet. */
  43941. ExpectIntEQ(clientSessRemCountFree, 0);
  43942. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43943. !defined(NO_SESSION_CACHE_REF)
  43944. /* Force a cache lookup */
  43945. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  43946. /* Force a cache update */
  43947. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  43948. /* This should set the timeout to 0 and call the remove callback from within
  43949. * the session cache. */
  43950. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  43951. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  43952. ExpectIntEQ(clientSessRemCountFree, 1);
  43953. #endif
  43954. /* Server session is in the cache so ex_data isn't free'd with the SSL
  43955. * object */
  43956. ExpectIntEQ(serverSessRemCountFree, 0);
  43957. /* Force a cache lookup */
  43958. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  43959. /* Force a cache update */
  43960. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  43961. /* This should set the timeout to 0 and call the remove callback from within
  43962. * the session cache. */
  43963. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  43964. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  43965. ExpectIntEQ(serverSessRemCountFree, 1);
  43966. /* Need to free the references that we kept */
  43967. SSL_CTX_free(serverSessCtx);
  43968. SSL_SESSION_free(serverSess);
  43969. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  43970. !defined(NO_SESSION_CACHE_REF)
  43971. SSL_CTX_free(clientSessCtx);
  43972. SSL_SESSION_free(clientSess);
  43973. #endif
  43974. #endif
  43975. return EXPECT_RESULT();
  43976. }
  43977. static int test_wolfSSL_ticket_keys(void)
  43978. {
  43979. EXPECT_DECLS;
  43980. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  43981. !defined(NO_WOLFSSL_SERVER)
  43982. WOLFSSL_CTX* ctx = NULL;
  43983. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  43984. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  43985. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  43986. WOLFSSL_FAILURE);
  43987. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  43988. WOLFSSL_FAILURE);
  43989. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  43990. WOLFSSL_FAILURE);
  43991. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  43992. WOLFSSL_FAILURE);
  43993. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  43994. WOLFSSL_FAILURE);
  43995. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  43996. WOLFSSL_FAILURE);
  43997. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  43998. WOLFSSL_FAILURE);
  43999. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  44000. WOLFSSL_FAILURE);
  44001. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  44002. WOLFSSL_FAILURE);
  44003. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  44004. WOLFSSL_FAILURE);
  44005. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  44006. WOLFSSL_FAILURE);
  44007. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  44008. WOLFSSL_FAILURE);
  44009. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  44010. WOLFSSL_FAILURE);
  44011. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  44012. WOLFSSL_FAILURE);
  44013. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  44014. WOLFSSL_SUCCESS);
  44015. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  44016. WOLFSSL_SUCCESS);
  44017. wolfSSL_CTX_free(ctx);
  44018. #endif
  44019. return EXPECT_RESULT();
  44020. }
  44021. #ifndef NO_BIO
  44022. static int test_wolfSSL_d2i_PUBKEY(void)
  44023. {
  44024. EXPECT_DECLS;
  44025. #if defined(OPENSSL_EXTRA)
  44026. BIO* bio = NULL;
  44027. EVP_PKEY* pkey = NULL;
  44028. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44029. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  44030. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  44031. /* RSA PUBKEY test */
  44032. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  44033. sizeof_client_keypub_der_2048), 0);
  44034. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44035. EVP_PKEY_free(pkey);
  44036. pkey = NULL;
  44037. #endif
  44038. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  44039. /* ECC PUBKEY test */
  44040. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  44041. sizeof_ecc_clikeypub_der_256), 0);
  44042. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44043. EVP_PKEY_free(pkey);
  44044. pkey = NULL;
  44045. #endif
  44046. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  44047. /* DSA PUBKEY test */
  44048. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  44049. sizeof_dsa_pub_key_der_2048), 0);
  44050. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44051. EVP_PKEY_free(pkey);
  44052. pkey = NULL;
  44053. #endif
  44054. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  44055. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  44056. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44057. (HAVE_FIPS_VERSION > 2))
  44058. /* DH PUBKEY test */
  44059. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  44060. sizeof_dh_pub_key_der_2048), 0);
  44061. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  44062. EVP_PKEY_free(pkey);
  44063. pkey = NULL;
  44064. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44065. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  44066. BIO_free(bio);
  44067. (void)pkey;
  44068. #endif
  44069. return EXPECT_RESULT();
  44070. }
  44071. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  44072. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  44073. {
  44074. EXPECT_DECLS;
  44075. BIO* bio = NULL;
  44076. EVP_PKEY* pkey = NULL;
  44077. WOLFSSL_CTX* ctx = NULL;
  44078. #if defined(WOLFSSL_KEY_GEN)
  44079. unsigned char buff[4096];
  44080. unsigned char* bufPtr = buff;
  44081. #endif
  44082. /* test creating new EVP_PKEY with bad arg */
  44083. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  44084. /* test loading RSA key using BIO */
  44085. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  44086. {
  44087. XFILE file = XBADFILE;
  44088. const char* fname = "./certs/server-key.der";
  44089. size_t sz = 0;
  44090. byte* buf = NULL;
  44091. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  44092. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  44093. ExpectTrue((sz = XFTELL(file)) != 0);
  44094. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  44095. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  44096. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  44097. if (file != XBADFILE) {
  44098. XFCLOSE(file);
  44099. }
  44100. /* Test using BIO new mem and loading DER private key */
  44101. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  44102. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  44103. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  44104. BIO_free(bio);
  44105. bio = NULL;
  44106. EVP_PKEY_free(pkey);
  44107. pkey = NULL;
  44108. }
  44109. #endif
  44110. /* test loading ECC key using BIO */
  44111. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  44112. {
  44113. XFILE file = XBADFILE;
  44114. const char* fname = "./certs/ecc-key.der";
  44115. size_t sz = 0;
  44116. byte* buf = NULL;
  44117. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  44118. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  44119. ExpectTrue((sz = XFTELL(file)) != 0);
  44120. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  44121. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  44122. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  44123. if (file != XBADFILE)
  44124. XFCLOSE(file);
  44125. /* Test using BIO new mem and loading DER private key */
  44126. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  44127. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  44128. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  44129. BIO_free(bio);
  44130. bio = NULL;
  44131. EVP_PKEY_free(pkey);
  44132. pkey = NULL;
  44133. }
  44134. #endif
  44135. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44136. #ifndef NO_WOLFSSL_SERVER
  44137. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  44138. #else
  44139. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  44140. #endif
  44141. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  44142. {
  44143. RSA* rsa = NULL;
  44144. /* Tests bad parameters */
  44145. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  44146. /* RSA not set yet, expecting to fail*/
  44147. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  44148. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  44149. /* set RSA using bio*/
  44150. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  44151. sizeof_client_key_der_2048), 0);
  44152. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  44153. ExpectNotNull(rsa);
  44154. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  44155. /* i2d RSAprivate key tests */
  44156. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  44157. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  44158. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  44159. sizeof_client_key_der_2048);
  44160. bufPtr -= sizeof_client_key_der_2048;
  44161. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  44162. sizeof_client_key_der_2048), 0);
  44163. bufPtr = NULL;
  44164. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  44165. sizeof_client_key_der_2048);
  44166. ExpectNotNull(bufPtr);
  44167. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  44168. sizeof_client_key_der_2048), 0);
  44169. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  44170. RSA_free(rsa);
  44171. rsa = RSA_new();
  44172. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  44173. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  44174. RSA_free(rsa);
  44175. }
  44176. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  44177. SSL_CTX_free(ctx);
  44178. ctx = NULL;
  44179. BIO_free(bio);
  44180. bio = NULL;
  44181. return EXPECT_RESULT();
  44182. }
  44183. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  44184. #endif /* !NO_BIO */
  44185. static int test_wolfSSL_sk_GENERAL_NAME(void)
  44186. {
  44187. EXPECT_DECLS;
  44188. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  44189. !defined(NO_RSA)
  44190. X509* x509 = NULL;
  44191. GENERAL_NAME* gn = NULL;
  44192. unsigned char buf[4096];
  44193. const unsigned char* bufPt = NULL;
  44194. int bytes = 0;
  44195. int i;
  44196. int j;
  44197. XFILE f = XBADFILE;
  44198. STACK_OF(GENERAL_NAME)* sk = NULL;
  44199. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  44200. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44201. if (f != XBADFILE)
  44202. XFCLOSE(f);
  44203. for (j = 0; j < 2; ++j) {
  44204. bufPt = buf;
  44205. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44206. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  44207. NID_subject_alt_name, NULL, NULL));
  44208. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  44209. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  44210. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  44211. if (gn != NULL) {
  44212. switch (gn->type) {
  44213. case GEN_DNS:
  44214. fprintf(stderr, "found type GEN_DNS\n");
  44215. break;
  44216. case GEN_EMAIL:
  44217. fprintf(stderr, "found type GEN_EMAIL\n");
  44218. break;
  44219. case GEN_URI:
  44220. fprintf(stderr, "found type GEN_URI\n");
  44221. break;
  44222. }
  44223. }
  44224. }
  44225. X509_free(x509);
  44226. x509 = NULL;
  44227. if (j == 0) {
  44228. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  44229. }
  44230. else {
  44231. /*
  44232. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  44233. * it was supposed to. This is a regression test for that bug.
  44234. */
  44235. GENERAL_NAMES_free(sk);
  44236. }
  44237. sk = NULL;
  44238. }
  44239. #endif
  44240. return EXPECT_RESULT();
  44241. }
  44242. static int test_wolfSSL_GENERAL_NAME_print(void)
  44243. {
  44244. EXPECT_DECLS;
  44245. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  44246. X509* x509 = NULL;
  44247. GENERAL_NAME* gn = NULL;
  44248. unsigned char buf[4096];
  44249. const unsigned char* bufPt = NULL;
  44250. int bytes;
  44251. XFILE f = XBADFILE;
  44252. STACK_OF(GENERAL_NAME)* sk = NULL;
  44253. BIO* out = NULL;
  44254. unsigned char outbuf[128];
  44255. X509_EXTENSION* ext = NULL;
  44256. AUTHORITY_INFO_ACCESS* aia = NULL;
  44257. ACCESS_DESCRIPTION* ad = NULL;
  44258. ASN1_IA5STRING *dnsname = NULL;
  44259. const unsigned char v4Addr[] = {192,168,53,1};
  44260. const unsigned char v6Addr[] =
  44261. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  44262. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  44263. const unsigned char email[] =
  44264. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  44265. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  44266. const char* dnsStr = "DNS:example.com";
  44267. const char* uriStr = "URI:http://127.0.0.1:22220";
  44268. const char* v4addStr = "IP Address:192.168.53.1";
  44269. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  44270. const char* emailStr = "email:info@wolfssl.com";
  44271. const char* othrStr = "othername:<unsupported>";
  44272. const char* x400Str = "X400Name:<unsupported>";
  44273. const char* ediStr = "EdiPartyName:<unsupported>";
  44274. /* BIO to output */
  44275. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  44276. /* test for NULL param */
  44277. gn = NULL;
  44278. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  44279. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  44280. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  44281. /* test for GEN_DNS */
  44282. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  44283. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44284. if (f != XBADFILE) {
  44285. XFCLOSE(f);
  44286. f = XBADFILE;
  44287. }
  44288. bufPt = buf;
  44289. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44290. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  44291. NID_subject_alt_name, NULL, NULL));
  44292. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  44293. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44294. XMEMSET(outbuf, 0, sizeof(outbuf));
  44295. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44296. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  44297. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  44298. gn = NULL;
  44299. sk = NULL;
  44300. X509_free(x509);
  44301. x509 = NULL;
  44302. /* Lets test for setting as well. */
  44303. ExpectNotNull(gn = GENERAL_NAME_new());
  44304. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  44305. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  44306. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  44307. dnsname = NULL;
  44308. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44309. XMEMSET(outbuf, 0, sizeof(outbuf));
  44310. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44311. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  44312. GENERAL_NAME_free(gn);
  44313. /* test for GEN_URI */
  44314. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44315. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44316. if (f != XBADFILE) {
  44317. XFCLOSE(f);
  44318. f = XBADFILE;
  44319. }
  44320. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  44321. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  44322. ext));
  44323. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  44324. if (ad != NULL) {
  44325. gn = ad->location;
  44326. }
  44327. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44328. gn = NULL;
  44329. XMEMSET(outbuf,0,sizeof(outbuf));
  44330. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44331. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  44332. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  44333. aia = NULL;
  44334. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  44335. ExpectNotNull(aia);
  44336. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  44337. aia = NULL;
  44338. X509_free(x509);
  44339. x509 = NULL;
  44340. /* test for GEN_IPADD */
  44341. /* ip v4 address */
  44342. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44343. if (gn != NULL) {
  44344. gn->type = GEN_IPADD;
  44345. if (gn->d.iPAddress != NULL) {
  44346. gn->d.iPAddress->length = sizeof(v4Addr);
  44347. }
  44348. }
  44349. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  44350. sizeof(v4Addr)), 1);
  44351. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44352. XMEMSET(outbuf,0,sizeof(outbuf));
  44353. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44354. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  44355. GENERAL_NAME_free(gn);
  44356. gn = NULL;
  44357. /* ip v6 address */
  44358. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44359. if (gn != NULL) {
  44360. gn->type = GEN_IPADD;
  44361. if (gn->d.iPAddress != NULL) {
  44362. gn->d.iPAddress->length = sizeof(v6Addr);
  44363. }
  44364. }
  44365. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  44366. sizeof(v6Addr)), 1);
  44367. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44368. XMEMSET(outbuf,0,sizeof(outbuf));
  44369. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44370. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  44371. GENERAL_NAME_free(gn);
  44372. gn = NULL;
  44373. /* test for GEN_EMAIL */
  44374. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44375. if (gn != NULL) {
  44376. gn->type = GEN_EMAIL;
  44377. if (gn->d.rfc822Name != NULL) {
  44378. gn->d.rfc822Name->length = sizeof(email);
  44379. }
  44380. }
  44381. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  44382. 1);
  44383. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44384. XMEMSET(outbuf,0,sizeof(outbuf));
  44385. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44386. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  44387. GENERAL_NAME_free(gn);
  44388. gn = NULL;
  44389. /* test for GEN_OTHERNAME */
  44390. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44391. if (gn != NULL) {
  44392. gn->type = GEN_OTHERNAME;
  44393. }
  44394. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44395. XMEMSET(outbuf,0,sizeof(outbuf));
  44396. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44397. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  44398. GENERAL_NAME_free(gn);
  44399. gn = NULL;
  44400. /* test for GEN_X400 */
  44401. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44402. if (gn != NULL) {
  44403. gn->type = GEN_X400;
  44404. }
  44405. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44406. XMEMSET(outbuf,0,sizeof(outbuf));
  44407. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44408. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  44409. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  44410. if (gn != NULL) {
  44411. gn->type = GEN_IA5;
  44412. }
  44413. GENERAL_NAME_free(gn);
  44414. gn = NULL;
  44415. /* test for GEN_EDIPARTY */
  44416. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  44417. if (gn != NULL) {
  44418. gn->type = GEN_EDIPARTY;
  44419. }
  44420. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  44421. XMEMSET(outbuf,0,sizeof(outbuf));
  44422. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  44423. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  44424. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  44425. if (gn != NULL) {
  44426. gn->type = GEN_IA5;
  44427. }
  44428. GENERAL_NAME_free(gn);
  44429. gn = NULL;
  44430. BIO_free(out);
  44431. #endif /* OPENSSL_ALL */
  44432. return EXPECT_RESULT();
  44433. }
  44434. static int test_wolfSSL_sk_DIST_POINT(void)
  44435. {
  44436. EXPECT_DECLS;
  44437. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  44438. !defined(NO_RSA)
  44439. X509* x509 = NULL;
  44440. unsigned char buf[4096];
  44441. const unsigned char* bufPt;
  44442. int bytes = 0;
  44443. int i = 0;
  44444. int j = 0;
  44445. XFILE f = XBADFILE;
  44446. DIST_POINT* dp = NULL;
  44447. DIST_POINT_NAME* dpn = NULL;
  44448. GENERAL_NAME* gn = NULL;
  44449. ASN1_IA5STRING* uri = NULL;
  44450. STACK_OF(DIST_POINT)* dps = NULL;
  44451. STACK_OF(GENERAL_NAME)* gns = NULL;
  44452. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  44453. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  44454. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  44455. if (f != XBADFILE)
  44456. XFCLOSE(f);
  44457. bufPt = buf;
  44458. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  44459. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  44460. NID_crl_distribution_points, NULL, NULL));
  44461. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  44462. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  44463. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  44464. ExpectNotNull(dpn = dp->distpoint);
  44465. /* this should be type 0, fullname */
  44466. ExpectIntEQ(dpn->type, 0);
  44467. ExpectNotNull(gns = dp->distpoint->name.fullname);
  44468. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  44469. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  44470. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  44471. ExpectIntEQ(gn->type, GEN_URI);
  44472. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  44473. ExpectNotNull(uri->data);
  44474. ExpectIntGT(uri->length, 0);
  44475. }
  44476. }
  44477. X509_free(x509);
  44478. CRL_DIST_POINTS_free(dps);
  44479. #endif
  44480. return EXPECT_RESULT();
  44481. }
  44482. static int test_wolfSSL_verify_mode(void)
  44483. {
  44484. EXPECT_DECLS;
  44485. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  44486. WOLFSSL* ssl = NULL;
  44487. WOLFSSL_CTX* ctx = NULL;
  44488. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44489. ExpectNotNull(ssl = SSL_new(ctx));
  44490. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44491. SSL_free(ssl);
  44492. ssl = NULL;
  44493. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  44494. ExpectNotNull(ssl = SSL_new(ctx));
  44495. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44496. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  44497. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  44498. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  44499. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  44500. SSL_free(ssl);
  44501. ssl = NULL;
  44502. wolfSSL_CTX_set_verify(ctx,
  44503. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  44504. ExpectNotNull(ssl = SSL_new(ctx));
  44505. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  44506. ExpectIntEQ(SSL_get_verify_mode(ssl),
  44507. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44508. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  44509. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  44510. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44511. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  44512. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  44513. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  44514. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  44515. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44516. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  44517. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  44518. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  44519. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  44520. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  44521. #endif
  44522. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  44523. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  44524. SSL_free(ssl);
  44525. SSL_CTX_free(ctx);
  44526. #endif
  44527. return EXPECT_RESULT();
  44528. }
  44529. static int test_wolfSSL_verify_depth(void)
  44530. {
  44531. EXPECT_DECLS;
  44532. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44533. WOLFSSL* ssl = NULL;
  44534. WOLFSSL_CTX* ctx = NULL;
  44535. long depth;
  44536. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44537. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  44538. ExpectNotNull(ssl = SSL_new(ctx));
  44539. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  44540. SSL_free(ssl);
  44541. ssl = NULL;
  44542. SSL_CTX_set_verify_depth(ctx, -1);
  44543. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  44544. SSL_CTX_set_verify_depth(ctx, 2);
  44545. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  44546. ExpectNotNull(ssl = SSL_new(ctx));
  44547. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  44548. SSL_free(ssl);
  44549. SSL_CTX_free(ctx);
  44550. #endif
  44551. return EXPECT_RESULT();
  44552. }
  44553. static int test_wolfSSL_verify_result(void)
  44554. {
  44555. EXPECT_DECLS;
  44556. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  44557. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  44558. WOLFSSL* ssl = NULL;
  44559. WOLFSSL_CTX* ctx = NULL;
  44560. long result = 0xDEADBEEF;
  44561. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  44562. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44563. ExpectNotNull(ssl = SSL_new(ctx));
  44564. wolfSSL_set_verify_result(ssl, result);
  44565. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  44566. SSL_free(ssl);
  44567. SSL_CTX_free(ctx);
  44568. #endif
  44569. return EXPECT_RESULT();
  44570. }
  44571. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44572. static void sslMsgCb(int w, int version, int type, const void* buf,
  44573. size_t sz, SSL* ssl, void* arg)
  44574. {
  44575. int i;
  44576. unsigned char* pt = (unsigned char*)buf;
  44577. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  44578. (w)?"Writing":"Reading", (int)sz, version, type);
  44579. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  44580. fprintf(stderr, "\n");
  44581. (void)ssl;
  44582. (void)arg;
  44583. }
  44584. #endif /* OPENSSL_EXTRA */
  44585. static int test_wolfSSL_msg_callback(void)
  44586. {
  44587. EXPECT_DECLS;
  44588. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  44589. WOLFSSL* ssl = NULL;
  44590. WOLFSSL_CTX* ctx = NULL;
  44591. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  44592. ExpectNotNull(ssl = SSL_new(ctx));
  44593. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  44594. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  44595. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  44596. SSL_free(ssl);
  44597. SSL_CTX_free(ctx);
  44598. #endif
  44599. return EXPECT_RESULT();
  44600. }
  44601. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  44602. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  44603. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  44604. static void binary_dump(void *ptr, int size)
  44605. {
  44606. #ifdef WOLFSSL_EVP_PRINT
  44607. int i = 0;
  44608. unsigned char *p = (unsigned char *) ptr;
  44609. fprintf(stderr, "{");
  44610. while ((p != NULL) && (i < size)) {
  44611. if ((i % 8) == 0) {
  44612. fprintf(stderr, "\n");
  44613. fprintf(stderr, " ");
  44614. }
  44615. fprintf(stderr, "0x%02x, ", p[i]);
  44616. i++;
  44617. }
  44618. fprintf(stderr, "\n};\n");
  44619. #else
  44620. (void) ptr;
  44621. (void) size;
  44622. #endif
  44623. }
  44624. static int last_val = 0x0f;
  44625. static int check_result(unsigned char *data, int len)
  44626. {
  44627. int i;
  44628. for ( ; len; ) {
  44629. last_val = (last_val + 1) % 16;
  44630. for (i = 0; i < 16; len--, i++, data++)
  44631. if (*data != last_val) {
  44632. return -1;
  44633. }
  44634. }
  44635. return 0;
  44636. }
  44637. static int r_offset;
  44638. static int w_offset;
  44639. static void init_offset(void)
  44640. {
  44641. r_offset = 0;
  44642. w_offset = 0;
  44643. }
  44644. static void get_record(unsigned char *data, unsigned char *buf, int len)
  44645. {
  44646. XMEMCPY(buf, data+r_offset, len);
  44647. r_offset += len;
  44648. }
  44649. static void set_record(unsigned char *data, unsigned char *buf, int len)
  44650. {
  44651. XMEMCPY(data+w_offset, buf, len);
  44652. w_offset += len;
  44653. }
  44654. static void set_plain(unsigned char *plain, int rec)
  44655. {
  44656. int i, j;
  44657. unsigned char *p = plain;
  44658. #define BLOCKSZ 16
  44659. for (i=0; i<(rec/BLOCKSZ); i++) {
  44660. for (j=0; j<BLOCKSZ; j++)
  44661. *p++ = (i % 16);
  44662. }
  44663. }
  44664. #endif
  44665. static int test_wolfSSL_EVP_Cipher_extra(void)
  44666. {
  44667. EXPECT_DECLS;
  44668. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  44669. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  44670. /* aes128-cbc, keylen=16, ivlen=16 */
  44671. byte aes128_cbc_key[] = {
  44672. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  44673. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  44674. };
  44675. byte aes128_cbc_iv[] = {
  44676. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  44677. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  44678. };
  44679. /* teset data size table */
  44680. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  44681. int test_drive2[] = {8, 3, 8, 512, 0};
  44682. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  44683. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  44684. int test_drive_len[100];
  44685. int ret = 0;
  44686. EVP_CIPHER_CTX *evp = NULL;
  44687. int ilen = 0;
  44688. int klen = 0;
  44689. int i, j;
  44690. const EVP_CIPHER *type;
  44691. byte *iv;
  44692. byte *key;
  44693. int ivlen;
  44694. int keylen;
  44695. #define RECORDS 16
  44696. #define BUFFSZ 512
  44697. byte plain [BUFFSZ * RECORDS];
  44698. byte cipher[BUFFSZ * RECORDS];
  44699. byte inb[BUFFSZ];
  44700. byte outb[BUFFSZ+16];
  44701. int outl = 0;
  44702. int inl;
  44703. iv = aes128_cbc_iv;
  44704. ivlen = sizeof(aes128_cbc_iv);
  44705. key = aes128_cbc_key;
  44706. keylen = sizeof(aes128_cbc_key);
  44707. type = EVP_aes_128_cbc();
  44708. set_plain(plain, BUFFSZ * RECORDS);
  44709. SSL_library_init();
  44710. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  44711. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  44712. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  44713. klen = EVP_CIPHER_CTX_key_length(evp);
  44714. if (klen > 0 && keylen != klen) {
  44715. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  44716. }
  44717. ilen = EVP_CIPHER_CTX_iv_length(evp);
  44718. if (ilen > 0 && ivlen != ilen) {
  44719. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  44720. }
  44721. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44722. for (j = 0; j<RECORDS; j++)
  44723. {
  44724. inl = BUFFSZ;
  44725. get_record(plain, inb, inl);
  44726. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  44727. set_record(cipher, outb, outl);
  44728. }
  44729. for (i = 0; test_drive[i]; i++) {
  44730. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44731. init_offset();
  44732. test_drive_len[i] = 0;
  44733. for (j = 0; test_drive[i][j]; j++)
  44734. {
  44735. inl = test_drive[i][j];
  44736. test_drive_len[i] += inl;
  44737. get_record(plain, inb, inl);
  44738. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  44739. 0);
  44740. /* output to cipher buffer, so that following Dec test can detect
  44741. if any error */
  44742. set_record(cipher, outb, outl);
  44743. }
  44744. EVP_CipherFinal(evp, outb, &outl);
  44745. if (outl > 0)
  44746. set_record(cipher, outb, outl);
  44747. }
  44748. for (i = 0; test_drive[i]; i++) {
  44749. last_val = 0x0f;
  44750. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  44751. init_offset();
  44752. for (j = 0; test_drive[i][j]; j++) {
  44753. inl = test_drive[i][j];
  44754. get_record(cipher, inb, inl);
  44755. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  44756. 0);
  44757. binary_dump(outb, outl);
  44758. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  44759. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  44760. }
  44761. ret = EVP_CipherFinal(evp, outb, &outl);
  44762. binary_dump(outb, outl);
  44763. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  44764. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  44765. ExpectTrue(ret);
  44766. }
  44767. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  44768. EVP_CIPHER_CTX_free(evp);
  44769. evp = NULL;
  44770. /* Do an extra test to verify correct behavior with empty input. */
  44771. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  44772. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  44773. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  44774. klen = EVP_CIPHER_CTX_key_length(evp);
  44775. if (klen > 0 && keylen != klen) {
  44776. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  44777. }
  44778. ilen = EVP_CIPHER_CTX_iv_length(evp);
  44779. if (ilen > 0 && ivlen != ilen) {
  44780. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  44781. }
  44782. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  44783. /* outl should be set to 0 after passing NULL, 0 for input args. */
  44784. outl = -1;
  44785. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  44786. ExpectIntEQ(outl, 0);
  44787. EVP_CIPHER_CTX_free(evp);
  44788. #endif /* test_EVP_Cipher */
  44789. return EXPECT_RESULT();
  44790. }
  44791. static int test_wolfSSL_PEM_read_DHparams(void)
  44792. {
  44793. EXPECT_DECLS;
  44794. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  44795. !defined(NO_FILESYSTEM)
  44796. DH* dh = NULL;
  44797. XFILE fp = XBADFILE;
  44798. unsigned char derOut[300];
  44799. unsigned char* derOutBuf = derOut;
  44800. int derOutSz = 0;
  44801. unsigned char derExpected[300];
  44802. int derExpectedSz = 0;
  44803. XMEMSET(derOut, 0, sizeof(derOut));
  44804. XMEMSET(derExpected, 0, sizeof(derExpected));
  44805. /* open DH param file, read into DH struct */
  44806. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  44807. /* bad args */
  44808. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  44809. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  44810. /* good args */
  44811. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  44812. if (fp != XBADFILE) {
  44813. XFCLOSE(fp);
  44814. fp = XBADFILE;
  44815. }
  44816. /* read in certs/dh2048.der for comparison against exported params */
  44817. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  44818. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  44819. fp), 0);
  44820. if (fp != XBADFILE) {
  44821. XFCLOSE(fp);
  44822. fp = XBADFILE;
  44823. }
  44824. /* export DH back to DER and compare */
  44825. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  44826. ExpectIntEQ(derOutSz, derExpectedSz);
  44827. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  44828. DH_free(dh);
  44829. dh = NULL;
  44830. /* Test parsing with X9.42 header */
  44831. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  44832. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  44833. if (fp != XBADFILE)
  44834. XFCLOSE(fp);
  44835. DH_free(dh);
  44836. dh = NULL;
  44837. #endif
  44838. return EXPECT_RESULT();
  44839. }
  44840. static int test_wolfSSL_X509_get_serialNumber(void)
  44841. {
  44842. EXPECT_DECLS;
  44843. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  44844. ASN1_INTEGER* a = NULL;
  44845. BIGNUM* bn = NULL;
  44846. X509* x509 = NULL;
  44847. char *serialHex = NULL;
  44848. byte serial[3];
  44849. int serialSz;
  44850. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44851. SSL_FILETYPE_PEM));
  44852. ExpectNotNull(a = X509_get_serialNumber(x509));
  44853. /* check on value of ASN1 Integer */
  44854. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  44855. a = NULL;
  44856. /* test setting serial number and then retrieving it */
  44857. ExpectNotNull(a = ASN1_INTEGER_new());
  44858. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  44859. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  44860. serialSz = sizeof(serial);
  44861. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  44862. WOLFSSL_SUCCESS);
  44863. ExpectIntEQ(serialSz, 1);
  44864. ExpectIntEQ(serial[0], 3);
  44865. ASN1_INTEGER_free(a);
  44866. a = NULL;
  44867. /* test setting serial number with 0's in it */
  44868. serial[0] = 0x01;
  44869. serial[1] = 0x00;
  44870. serial[2] = 0x02;
  44871. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  44872. if (a != NULL) {
  44873. a->data[0] = ASN_INTEGER;
  44874. a->data[1] = sizeof(serial);
  44875. XMEMCPY(&a->data[2], serial, sizeof(serial));
  44876. a->length = sizeof(serial) + 2;
  44877. }
  44878. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  44879. XMEMSET(serial, 0, sizeof(serial));
  44880. serialSz = sizeof(serial);
  44881. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  44882. WOLFSSL_SUCCESS);
  44883. ExpectIntEQ(serialSz, 3);
  44884. ExpectIntEQ(serial[0], 0x01);
  44885. ExpectIntEQ(serial[1], 0x00);
  44886. ExpectIntEQ(serial[2], 0x02);
  44887. ASN1_INTEGER_free(a);
  44888. a = NULL;
  44889. X509_free(x509); /* free's a */
  44890. ExpectNotNull(serialHex = BN_bn2hex(bn));
  44891. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  44892. ExpectStrEQ(serialHex, "01");
  44893. #else
  44894. ExpectStrEQ(serialHex, "1");
  44895. #endif
  44896. OPENSSL_free(serialHex);
  44897. ExpectIntEQ(BN_get_word(bn), 1);
  44898. BN_free(bn);
  44899. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  44900. ExpectNotNull(a = ASN1_INTEGER_new());
  44901. if (a != NULL) {
  44902. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  44903. DYNAMIC_TYPE_OPENSSL));
  44904. a->isDynamic = 1;
  44905. ASN1_INTEGER_free(a);
  44906. }
  44907. #endif
  44908. return EXPECT_RESULT();
  44909. }
  44910. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  44911. {
  44912. EXPECT_DECLS;
  44913. #if defined(OPENSSL_EXTRA)
  44914. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  44915. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  44916. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  44917. #endif
  44918. return EXPECT_RESULT();
  44919. }
  44920. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  44921. {
  44922. EXPECT_DECLS;
  44923. #if defined(OPENSSL_EXTRA)
  44924. #define MAX_HEXSTR_BUFSZ 9
  44925. #define NUM_CASES 5
  44926. struct Output {
  44927. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  44928. long ret;
  44929. };
  44930. int i;
  44931. int j;
  44932. const char* inputs[NUM_CASES] = {
  44933. "aabcd1357e",
  44934. "01:12:23:34:a5:b6:c7:d8:e9",
  44935. ":01:02",
  44936. "012",
  44937. ":ab:ac:d"
  44938. };
  44939. struct Output expectedOutputs[NUM_CASES] = {
  44940. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  44941. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  44942. {{0x01, 0x02}, 2},
  44943. {{0x00}, 0},
  44944. {{0x00}, 0}
  44945. };
  44946. long len = 0;
  44947. unsigned char* returnedBuf = NULL;
  44948. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  44949. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  44950. if (returnedBuf == NULL) {
  44951. ExpectIntEQ(expectedOutputs[i].ret, 0);
  44952. continue;
  44953. }
  44954. ExpectIntEQ(expectedOutputs[i].ret, len);
  44955. for (j = 0; j < len; ++j) {
  44956. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  44957. }
  44958. OPENSSL_free(returnedBuf);
  44959. returnedBuf = NULL;
  44960. }
  44961. #endif
  44962. return EXPECT_RESULT();
  44963. }
  44964. static int test_wolfSSL_X509_CA_num(void)
  44965. {
  44966. EXPECT_DECLS;
  44967. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  44968. defined(HAVE_ECC) && !defined(NO_RSA)
  44969. WOLFSSL_X509_STORE *store = NULL;
  44970. WOLFSSL_X509 *x509_1 = NULL;
  44971. WOLFSSL_X509 *x509_2 = NULL;
  44972. int ca_num = 0;
  44973. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  44974. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44975. WOLFSSL_FILETYPE_PEM));
  44976. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  44977. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  44978. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  44979. WOLFSSL_FILETYPE_PEM));
  44980. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  44981. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  44982. wolfSSL_X509_free(x509_1);
  44983. wolfSSL_X509_free(x509_2);
  44984. wolfSSL_X509_STORE_free(store);
  44985. #endif
  44986. return EXPECT_RESULT();
  44987. }
  44988. static int test_wolfSSL_X509_check_ca(void)
  44989. {
  44990. EXPECT_DECLS;
  44991. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  44992. WOLFSSL_X509 *x509 = NULL;
  44993. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  44994. WOLFSSL_FILETYPE_PEM));
  44995. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  44996. wolfSSL_X509_free(x509);
  44997. #endif
  44998. return EXPECT_RESULT();
  44999. }
  45000. static int test_wolfSSL_X509_check_ip_asc(void)
  45001. {
  45002. EXPECT_DECLS;
  45003. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  45004. WOLFSSL_X509 *x509 = NULL;
  45005. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  45006. WOLFSSL_FILETYPE_PEM));
  45007. #if 0
  45008. /* TODO: add cert gen for testing positive case */
  45009. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  45010. #endif
  45011. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  45012. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  45013. wolfSSL_X509_free(x509);
  45014. #endif
  45015. return EXPECT_RESULT();
  45016. }
  45017. static int test_wolfSSL_make_cert(void)
  45018. {
  45019. EXPECT_DECLS;
  45020. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  45021. defined(WOLFSSL_CERT_EXT)
  45022. int ret = 0;
  45023. Cert cert;
  45024. CertName name;
  45025. RsaKey key;
  45026. WC_RNG rng;
  45027. byte der[FOURK_BUF];
  45028. word32 idx = 0;
  45029. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  45030. #ifdef OPENSSL_EXTRA
  45031. const unsigned char* pt = NULL;
  45032. int certSz = 0;
  45033. X509* x509 = NULL;
  45034. X509_NAME* x509name = NULL;
  45035. X509_NAME_ENTRY* entry = NULL;
  45036. ASN1_STRING* entryValue = NULL;
  45037. #endif
  45038. XMEMSET(&name, 0, sizeof(CertName));
  45039. /* set up cert name */
  45040. XMEMCPY(name.country, "US", sizeof("US"));
  45041. name.countryEnc = CTC_PRINTABLE;
  45042. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  45043. name.stateEnc = CTC_UTF8;
  45044. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  45045. name.localityEnc = CTC_UTF8;
  45046. XMEMCPY(name.sur, "Test", sizeof("Test"));
  45047. name.surEnc = CTC_UTF8;
  45048. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  45049. name.orgEnc = CTC_UTF8;
  45050. XMEMCPY(name.unit, "Development", sizeof("Development"));
  45051. name.unitEnc = CTC_UTF8;
  45052. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  45053. name.commonNameEnc = CTC_UTF8;
  45054. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  45055. name.serialDevEnc = CTC_PRINTABLE;
  45056. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  45057. name.userIdEnc = CTC_PRINTABLE;
  45058. #ifdef WOLFSSL_MULTI_ATTRIB
  45059. #if CTC_MAX_ATTRIB > 2
  45060. {
  45061. NameAttrib* n;
  45062. n = &name.name[0];
  45063. n->id = ASN_DOMAIN_COMPONENT;
  45064. n->type = CTC_UTF8;
  45065. n->sz = sizeof("com");
  45066. XMEMCPY(n->value, "com", sizeof("com"));
  45067. n = &name.name[1];
  45068. n->id = ASN_DOMAIN_COMPONENT;
  45069. n->type = CTC_UTF8;
  45070. n->sz = sizeof("wolfssl");
  45071. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  45072. }
  45073. #endif
  45074. #endif /* WOLFSSL_MULTI_ATTRIB */
  45075. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  45076. #ifndef HAVE_FIPS
  45077. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  45078. #else
  45079. ExpectIntEQ(wc_InitRng(&rng), 0);
  45080. #endif
  45081. /* load test RSA key */
  45082. idx = 0;
  45083. #if defined(USE_CERT_BUFFERS_1024)
  45084. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  45085. sizeof_server_key_der_1024), 0);
  45086. #elif defined(USE_CERT_BUFFERS_2048)
  45087. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  45088. sizeof_server_key_der_2048), 0);
  45089. #else
  45090. /* error case, no RSA key loaded, happens later */
  45091. (void)idx;
  45092. #endif
  45093. XMEMSET(&cert, 0 , sizeof(Cert));
  45094. ExpectIntEQ(wc_InitCert(&cert), 0);
  45095. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  45096. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  45097. cert.serialSz = (int)sizeof(mySerial);
  45098. cert.isCA = 1;
  45099. #ifndef NO_SHA256
  45100. cert.sigType = CTC_SHA256wRSA;
  45101. #else
  45102. cert.sigType = CTC_SHAwRSA;
  45103. #endif
  45104. /* add SKID from the Public Key */
  45105. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  45106. /* add AKID from the Public Key */
  45107. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  45108. ret = 0;
  45109. do {
  45110. #if defined(WOLFSSL_ASYNC_CRYPT)
  45111. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  45112. #endif
  45113. if (ret >= 0) {
  45114. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  45115. }
  45116. } while (ret == WC_PENDING_E);
  45117. ExpectIntGT(ret, 0);
  45118. #ifdef OPENSSL_EXTRA
  45119. /* der holds a certificate with DC's now check X509 parsing of it */
  45120. certSz = ret;
  45121. pt = der;
  45122. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  45123. ExpectNotNull(x509name = X509_get_subject_name(x509));
  45124. #ifdef WOLFSSL_MULTI_ATTRIB
  45125. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45126. -1)), 5);
  45127. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45128. (int)idx)), 6);
  45129. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45130. (int)idx)), -1);
  45131. #endif /* WOLFSSL_MULTI_ATTRIB */
  45132. /* compare DN at index 0 */
  45133. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  45134. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45135. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  45136. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  45137. #ifndef WOLFSSL_MULTI_ATTRIB
  45138. /* compare Serial Number */
  45139. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  45140. -1)), 7);
  45141. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  45142. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45143. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  45144. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  45145. #endif
  45146. #ifdef WOLFSSL_MULTI_ATTRIB
  45147. /* get first and second DC and compare result */
  45148. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45149. -1)), 5);
  45150. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  45151. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45152. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  45153. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  45154. (int)idx)), 6);
  45155. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  45156. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  45157. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  45158. #endif /* WOLFSSL_MULTI_ATTRIB */
  45159. /* try invalid index locations for regression test and sanity check */
  45160. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  45161. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  45162. X509_free(x509);
  45163. #endif /* OPENSSL_EXTRA */
  45164. wc_FreeRsaKey(&key);
  45165. wc_FreeRng(&rng);
  45166. #endif
  45167. return EXPECT_RESULT();
  45168. }
  45169. static int test_wolfSSL_X509_get_version(void)
  45170. {
  45171. EXPECT_DECLS;
  45172. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45173. WOLFSSL_X509 *x509 = NULL;
  45174. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  45175. WOLFSSL_FILETYPE_PEM));
  45176. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  45177. wolfSSL_X509_free(x509);
  45178. #endif
  45179. return EXPECT_RESULT();
  45180. }
  45181. #if defined(OPENSSL_ALL)
  45182. static int test_wolfSSL_sk_CIPHER_description(void)
  45183. {
  45184. EXPECT_DECLS;
  45185. #if !defined(NO_RSA)
  45186. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  45187. int i;
  45188. int numCiphers = 0;
  45189. const SSL_METHOD *method = NULL;
  45190. const SSL_CIPHER *cipher = NULL;
  45191. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  45192. SSL_CTX *ctx = NULL;
  45193. SSL *ssl = NULL;
  45194. char buf[256];
  45195. char test_str[9] = "0000000";
  45196. const char badStr[] = "unknown";
  45197. const char certPath[] = "./certs/client-cert.pem";
  45198. XMEMSET(buf, 0, sizeof(buf));
  45199. ExpectNotNull(method = TLSv1_2_client_method());
  45200. ExpectNotNull(ctx = SSL_CTX_new(method));
  45201. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  45202. SSL_CTX_set_verify_depth(ctx, 4);
  45203. SSL_CTX_set_options(ctx, flags);
  45204. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  45205. WOLFSSL_SUCCESS);
  45206. ExpectNotNull(ssl = SSL_new(ctx));
  45207. /* SSL_get_ciphers returns a stack of all configured ciphers
  45208. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  45209. */
  45210. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  45211. /* loop through the amount of supportedCiphers */
  45212. numCiphers = sk_num(supportedCiphers);
  45213. for (i = 0; i < numCiphers; ++i) {
  45214. int j;
  45215. /* sk_value increments "sk->data.cipher->cipherOffset".
  45216. * wolfSSL_sk_CIPHER_description sets the description for
  45217. * the cipher based on the provided offset.
  45218. */
  45219. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  45220. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  45221. }
  45222. /* Search cipher description string for "unknown" descriptor */
  45223. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  45224. int k = 0;
  45225. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  45226. test_str[k] = badStr[k];
  45227. j++;
  45228. k++;
  45229. }
  45230. }
  45231. /* Fail if test_str == badStr == "unknown" */
  45232. ExpectStrNE(test_str,badStr);
  45233. }
  45234. SSL_free(ssl);
  45235. SSL_CTX_free(ctx);
  45236. #endif
  45237. return EXPECT_RESULT();
  45238. }
  45239. static int test_wolfSSL_get_ciphers_compat(void)
  45240. {
  45241. EXPECT_DECLS;
  45242. #if !defined(NO_RSA)
  45243. const SSL_METHOD *method = NULL;
  45244. const char certPath[] = "./certs/client-cert.pem";
  45245. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  45246. SSL_CTX *ctx = NULL;
  45247. WOLFSSL *ssl = NULL;
  45248. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  45249. ExpectNotNull(method = SSLv23_client_method());
  45250. ExpectNotNull(ctx = SSL_CTX_new(method));
  45251. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  45252. SSL_CTX_set_verify_depth(ctx, 4);
  45253. SSL_CTX_set_options(ctx, flags);
  45254. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  45255. WOLFSSL_SUCCESS);
  45256. ExpectNotNull(ssl = SSL_new(ctx));
  45257. /* Test Bad NULL input */
  45258. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  45259. /* Test for Good input */
  45260. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  45261. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  45262. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  45263. SSL_free(ssl);
  45264. SSL_CTX_free(ctx);
  45265. #endif
  45266. return EXPECT_RESULT();
  45267. }
  45268. static int test_wolfSSL_X509_PUBKEY_get(void)
  45269. {
  45270. EXPECT_DECLS;
  45271. WOLFSSL_X509_PUBKEY pubkey;
  45272. WOLFSSL_X509_PUBKEY* key;
  45273. WOLFSSL_EVP_PKEY evpkey ;
  45274. WOLFSSL_EVP_PKEY* evpPkey;
  45275. WOLFSSL_EVP_PKEY* retEvpPkey;
  45276. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  45277. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  45278. key = &pubkey;
  45279. evpPkey = &evpkey;
  45280. evpPkey->type = WOLFSSL_SUCCESS;
  45281. key->pkey = evpPkey;
  45282. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  45283. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  45284. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  45285. key->pkey = NULL;
  45286. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  45287. return EXPECT_RESULT();
  45288. }
  45289. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  45290. {
  45291. EXPECT_DECLS;
  45292. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  45293. DSA *dsa = NULL;
  45294. DSA *setDsa = NULL;
  45295. EVP_PKEY *pkey = NULL;
  45296. EVP_PKEY *set1Pkey = NULL;
  45297. SHA_CTX sha;
  45298. byte signature[DSA_SIG_SIZE];
  45299. byte hash[WC_SHA_DIGEST_SIZE];
  45300. word32 bytes;
  45301. int answer;
  45302. #ifdef USE_CERT_BUFFERS_1024
  45303. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  45304. int dsaKeySz = sizeof_dsa_key_der_1024;
  45305. byte tmp[ONEK_BUF];
  45306. XMEMSET(tmp, 0, sizeof(tmp));
  45307. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  45308. bytes = dsaKeySz;
  45309. #elif defined(USE_CERT_BUFFERS_2048)
  45310. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  45311. int dsaKeySz = sizeof_dsa_key_der_2048;
  45312. byte tmp[TWOK_BUF];
  45313. XMEMSET(tmp, 0, sizeof(tmp));
  45314. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  45315. bytes = (word32)dsaKeySz;
  45316. #else
  45317. byte tmp[TWOK_BUF];
  45318. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  45319. int dsaKeySz;
  45320. XFILE fp = XBADFILE;
  45321. XMEMSET(tmp, 0, sizeof(tmp));
  45322. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  45323. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  45324. if (fp != XBADFILE)
  45325. XFCLOSE(fp);
  45326. #endif /* END USE_CERT_BUFFERS_1024 */
  45327. /* Create hash to later Sign and Verify */
  45328. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  45329. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  45330. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  45331. /* Initialize pkey with der format dsa key */
  45332. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  45333. (long)dsaKeySz));
  45334. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  45335. /* Should Fail: NULL argument */
  45336. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  45337. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  45338. /* Should Pass: Initialized pkey argument */
  45339. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  45340. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  45341. #ifdef USE_CERT_BUFFERS_1024
  45342. ExpectIntEQ(DSA_bits(dsa), 1024);
  45343. #else
  45344. ExpectIntEQ(DSA_bits(dsa), 2048);
  45345. #endif
  45346. /* Sign */
  45347. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  45348. /* Verify. */
  45349. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  45350. WOLFSSL_SUCCESS);
  45351. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  45352. /* Should Fail: set1Pkey not initialized */
  45353. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  45354. /* Initialize set1Pkey */
  45355. set1Pkey = EVP_PKEY_new();
  45356. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  45357. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  45358. WOLFSSL_SUCCESS);
  45359. /* Should Pass: set dsa into set1Pkey */
  45360. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  45361. DSA_free(dsa);
  45362. DSA_free(setDsa);
  45363. EVP_PKEY_free(pkey);
  45364. EVP_PKEY_free(set1Pkey);
  45365. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  45366. return EXPECT_RESULT();
  45367. } /* END test_EVP_PKEY_set1_get1_DSA */
  45368. static int test_wolfSSL_DSA_generate_parameters(void)
  45369. {
  45370. EXPECT_DECLS;
  45371. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  45372. !defined(HAVE_FIPS)
  45373. DSA *dsa = NULL;
  45374. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  45375. NULL));
  45376. DSA_free(dsa);
  45377. #endif
  45378. return EXPECT_RESULT();
  45379. }
  45380. static int test_wolfSSL_DSA_SIG(void)
  45381. {
  45382. EXPECT_DECLS;
  45383. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  45384. !defined(HAVE_FIPS)
  45385. DSA *dsa = NULL;
  45386. DSA *dsa2 = NULL;
  45387. DSA_SIG *sig = NULL;
  45388. const BIGNUM *p = NULL;
  45389. const BIGNUM *q = NULL;
  45390. const BIGNUM *g = NULL;
  45391. const BIGNUM *pub = NULL;
  45392. const BIGNUM *priv = NULL;
  45393. BIGNUM *dup_p = NULL;
  45394. BIGNUM *dup_q = NULL;
  45395. BIGNUM *dup_g = NULL;
  45396. BIGNUM *dup_pub = NULL;
  45397. BIGNUM *dup_priv = NULL;
  45398. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  45399. ExpectNotNull(dsa = DSA_new());
  45400. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  45401. NULL), 1);
  45402. ExpectIntEQ(DSA_generate_key(dsa), 1);
  45403. DSA_get0_pqg(dsa, &p, &q, &g);
  45404. DSA_get0_key(dsa, &pub, &priv);
  45405. ExpectNotNull(dup_p = BN_dup(p));
  45406. ExpectNotNull(dup_q = BN_dup(q));
  45407. ExpectNotNull(dup_g = BN_dup(g));
  45408. ExpectNotNull(dup_pub = BN_dup(pub));
  45409. ExpectNotNull(dup_priv = BN_dup(priv));
  45410. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  45411. ExpectNotNull(dsa2 = DSA_new());
  45412. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  45413. if (EXPECT_FAIL()) {
  45414. BN_free(dup_p);
  45415. BN_free(dup_q);
  45416. BN_free(dup_g);
  45417. }
  45418. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  45419. if (EXPECT_FAIL()) {
  45420. BN_free(dup_pub);
  45421. BN_free(dup_priv);
  45422. }
  45423. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  45424. DSA_free(dsa);
  45425. DSA_free(dsa2);
  45426. DSA_SIG_free(sig);
  45427. #endif
  45428. return EXPECT_RESULT();
  45429. }
  45430. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  45431. {
  45432. EXPECT_DECLS;
  45433. #ifdef HAVE_ECC
  45434. WOLFSSL_EC_KEY* ecKey = NULL;
  45435. WOLFSSL_EC_KEY* ecGet1 = NULL;
  45436. EVP_PKEY* pkey = NULL;
  45437. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45438. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45439. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  45440. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  45441. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  45442. /* Should fail since ecKey is empty */
  45443. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  45444. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45445. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  45446. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  45447. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  45448. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  45449. wolfSSL_EC_KEY_free(ecKey);
  45450. wolfSSL_EC_KEY_free(ecGet1);
  45451. EVP_PKEY_free(pkey);
  45452. #endif /* HAVE_ECC */
  45453. return EXPECT_RESULT();
  45454. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  45455. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  45456. {
  45457. EXPECT_DECLS;
  45458. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45459. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45460. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  45461. DH *dh = NULL;
  45462. DH *setDh = NULL;
  45463. EVP_PKEY *pkey = NULL;
  45464. XFILE f = XBADFILE;
  45465. unsigned char buf[4096];
  45466. const unsigned char* pt = buf;
  45467. const char* dh2048 = "./certs/dh2048.der";
  45468. long len = 0;
  45469. int code = -1;
  45470. XMEMSET(buf, 0, sizeof(buf));
  45471. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  45472. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45473. if (f != XBADFILE)
  45474. XFCLOSE(f);
  45475. /* Load dh2048.der into DH with internal format */
  45476. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  45477. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  45478. ExpectIntEQ(code, 0);
  45479. code = -1;
  45480. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45481. /* Set DH into PKEY */
  45482. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  45483. /* Get DH from PKEY */
  45484. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  45485. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  45486. ExpectIntEQ(code, 0);
  45487. EVP_PKEY_free(pkey);
  45488. DH_free(setDh);
  45489. setDh = NULL;
  45490. DH_free(dh);
  45491. dh = NULL;
  45492. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  45493. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45494. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45495. return EXPECT_RESULT();
  45496. } /* END test_EVP_PKEY_set1_get1_DH */
  45497. static int test_wolfSSL_CTX_ctrl(void)
  45498. {
  45499. EXPECT_DECLS;
  45500. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45501. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45502. char caFile[] = "./certs/client-ca.pem";
  45503. char clientFile[] = "./certs/client-cert.pem";
  45504. SSL_CTX* ctx = NULL;
  45505. X509* x509 = NULL;
  45506. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45507. byte buf[6000];
  45508. char file[] = "./certs/dsaparams.pem";
  45509. XFILE f = XBADFILE;
  45510. int bytes = 0;
  45511. BIO* bio = NULL;
  45512. DSA* dsa = NULL;
  45513. DH* dh = NULL;
  45514. #endif
  45515. #ifdef HAVE_ECC
  45516. WOLFSSL_EC_KEY* ecKey = NULL;
  45517. #endif
  45518. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  45519. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  45520. WOLFSSL_FILETYPE_PEM));
  45521. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  45522. if (EXPECT_FAIL()) {
  45523. wolfSSL_X509_free(x509);
  45524. }
  45525. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  45526. WOLFSSL_FILETYPE_PEM));
  45527. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45528. /* Initialize DH */
  45529. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  45530. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  45531. if (f != XBADFILE)
  45532. XFCLOSE(f);
  45533. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  45534. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  45535. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  45536. #endif
  45537. #ifdef HAVE_ECC
  45538. /* Initialize WOLFSSL_EC_KEY */
  45539. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45540. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45541. #endif
  45542. /* additional test of getting EVP_PKEY key size from X509
  45543. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  45544. * allowed with user RSA */
  45545. {
  45546. EVP_PKEY* pkey = NULL;
  45547. #if defined(HAVE_ECC)
  45548. X509* ecX509 = NULL;
  45549. #endif /* HAVE_ECC */
  45550. ExpectNotNull(pkey = X509_get_pubkey(x509));
  45551. /* current RSA key is 2048 bit (256 bytes) */
  45552. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  45553. EVP_PKEY_free(pkey);
  45554. pkey = NULL;
  45555. #if defined(HAVE_ECC)
  45556. #if defined(USE_CERT_BUFFERS_256)
  45557. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  45558. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  45559. SSL_FILETYPE_ASN1));
  45560. #else
  45561. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  45562. cliEccCertFile, SSL_FILETYPE_PEM));
  45563. #endif
  45564. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  45565. /* current ECC key is 256 bit (32 bytes) */
  45566. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  45567. X509_free(ecX509);
  45568. EVP_PKEY_free(pkey);
  45569. #endif /* HAVE_ECC */
  45570. }
  45571. /* Tests should fail with passed in NULL pointer */
  45572. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  45573. SSL_FAILURE);
  45574. #if !defined(NO_DH) && !defined(NO_DSA)
  45575. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  45576. SSL_FAILURE);
  45577. #endif
  45578. #ifdef HAVE_ECC
  45579. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  45580. SSL_FAILURE);
  45581. #endif
  45582. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  45583. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  45584. */
  45585. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  45586. SSL_SUCCESS);
  45587. if (EXPECT_FAIL()) {
  45588. wolfSSL_X509_free(x509);
  45589. }
  45590. /* Test with SSL_CTRL_OPTIONS
  45591. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  45592. */
  45593. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  45594. NULL) == SSL_OP_NO_TLSv1);
  45595. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  45596. /* Test with SSL_CTRL_SET_TMP_DH
  45597. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  45598. */
  45599. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  45600. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  45601. SSL_SUCCESS);
  45602. #endif
  45603. /* Test with SSL_CTRL_SET_TMP_ECDH
  45604. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  45605. */
  45606. #ifdef HAVE_ECC
  45607. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  45608. SSL_SUCCESS);
  45609. #endif
  45610. #ifdef WOLFSSL_ENCRYPTED_KEYS
  45611. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  45612. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  45613. #endif
  45614. /* Test for min/max proto */
  45615. #ifndef WOLFSSL_NO_TLS12
  45616. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  45617. 0, NULL), SSL_SUCCESS);
  45618. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  45619. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  45620. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  45621. #endif
  45622. #ifdef WOLFSSL_TLS13
  45623. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45624. 0, NULL), SSL_SUCCESS);
  45625. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45626. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  45627. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  45628. #ifndef WOLFSSL_NO_TLS12
  45629. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  45630. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  45631. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  45632. #endif
  45633. #endif
  45634. /* Cleanup and Pass */
  45635. #if !defined(NO_DH) && !defined(NO_DSA)
  45636. #ifndef NO_BIO
  45637. BIO_free(bio);
  45638. DSA_free(dsa);
  45639. DH_free(dh);
  45640. dh = NULL;
  45641. #endif
  45642. #endif
  45643. #ifdef HAVE_ECC
  45644. wolfSSL_EC_KEY_free(ecKey);
  45645. #endif
  45646. SSL_CTX_free(ctx);
  45647. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  45648. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  45649. return EXPECT_RESULT();
  45650. }
  45651. static int test_wolfSSL_EVP_PKEY_assign(void)
  45652. {
  45653. EXPECT_DECLS;
  45654. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  45655. int type;
  45656. WOLFSSL_EVP_PKEY* pkey = NULL;
  45657. #ifndef NO_RSA
  45658. WOLFSSL_RSA* rsa = NULL;
  45659. #endif
  45660. #ifndef NO_DSA
  45661. WOLFSSL_DSA* dsa = NULL;
  45662. #endif
  45663. #ifdef HAVE_ECC
  45664. WOLFSSL_EC_KEY* ecKey = NULL;
  45665. #endif
  45666. #ifndef NO_RSA
  45667. type = EVP_PKEY_RSA;
  45668. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45669. ExpectNotNull(rsa = wolfSSL_RSA_new());
  45670. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  45671. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45672. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  45673. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  45674. if (EXPECT_FAIL()) {
  45675. wolfSSL_RSA_free(rsa);
  45676. }
  45677. wolfSSL_EVP_PKEY_free(pkey);
  45678. pkey = NULL;
  45679. #endif /* NO_RSA */
  45680. #ifndef NO_DSA
  45681. type = EVP_PKEY_DSA;
  45682. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45683. ExpectNotNull(dsa = wolfSSL_DSA_new());
  45684. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  45685. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45686. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  45687. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  45688. if (EXPECT_FAIL()) {
  45689. wolfSSL_DSA_free(dsa);
  45690. }
  45691. wolfSSL_EVP_PKEY_free(pkey);
  45692. pkey = NULL;
  45693. #endif /* NO_DSA */
  45694. #ifdef HAVE_ECC
  45695. type = EVP_PKEY_EC;
  45696. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45697. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  45698. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  45699. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  45700. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  45701. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  45702. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  45703. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  45704. if (EXPECT_FAIL()) {
  45705. wolfSSL_EC_KEY_free(ecKey);
  45706. }
  45707. wolfSSL_EVP_PKEY_free(pkey);
  45708. pkey = NULL;
  45709. #endif /* HAVE_ECC */
  45710. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  45711. return EXPECT_RESULT();
  45712. }
  45713. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  45714. {
  45715. EXPECT_DECLS;
  45716. #if !defined(NO_DH) && \
  45717. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45718. XFILE f = XBADFILE;
  45719. unsigned char buf[4096];
  45720. const unsigned char* pt = buf;
  45721. const char* params1 = "./certs/dh2048.der";
  45722. long len = 0;
  45723. WOLFSSL_DH* dh = NULL;
  45724. WOLFSSL_EVP_PKEY* pkey = NULL;
  45725. XMEMSET(buf, 0, sizeof(buf));
  45726. /* Load DH parameters DER. */
  45727. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  45728. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  45729. if (f != XBADFILE)
  45730. XFCLOSE(f);
  45731. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  45732. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  45733. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45734. /* Bad cases */
  45735. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  45736. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  45737. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  45738. /* Good case */
  45739. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  45740. if (EXPECT_FAIL()) {
  45741. wolfSSL_DH_free(dh);
  45742. }
  45743. EVP_PKEY_free(pkey);
  45744. #endif
  45745. return EXPECT_RESULT();
  45746. }
  45747. static int test_wolfSSL_EVP_PKEY_base_id(void)
  45748. {
  45749. EXPECT_DECLS;
  45750. WOLFSSL_EVP_PKEY* pkey = NULL;
  45751. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45752. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  45753. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  45754. EVP_PKEY_free(pkey);
  45755. return EXPECT_RESULT();
  45756. }
  45757. static int test_wolfSSL_EVP_PKEY_id(void)
  45758. {
  45759. EXPECT_DECLS;
  45760. WOLFSSL_EVP_PKEY* pkey = NULL;
  45761. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45762. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  45763. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  45764. EVP_PKEY_free(pkey);
  45765. return EXPECT_RESULT();
  45766. }
  45767. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  45768. {
  45769. EXPECT_DECLS;
  45770. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  45771. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  45772. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  45773. ECC_MIN_KEY_SZ <= 256)
  45774. EVP_PKEY_CTX* ctx = NULL;
  45775. EVP_PKEY* pkey = NULL;
  45776. /* Test error conditions. */
  45777. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  45778. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  45779. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  45780. #ifndef NO_RSA
  45781. EVP_PKEY_CTX_free(ctx);
  45782. /* Parameter generation for RSA not supported yet. */
  45783. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  45784. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  45785. #endif
  45786. #ifdef HAVE_ECC
  45787. EVP_PKEY_CTX_free(ctx);
  45788. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  45789. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  45790. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  45791. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  45792. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  45793. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  45794. WOLFSSL_SUCCESS);
  45795. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45796. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  45797. #endif
  45798. EVP_PKEY_CTX_free(ctx);
  45799. EVP_PKEY_free(pkey);
  45800. #endif
  45801. return EXPECT_RESULT();
  45802. }
  45803. static int test_wolfSSL_EVP_PKEY_keygen(void)
  45804. {
  45805. EXPECT_DECLS;
  45806. WOLFSSL_EVP_PKEY* pkey = NULL;
  45807. EVP_PKEY_CTX* ctx = NULL;
  45808. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  45809. WOLFSSL_EVP_PKEY* params = NULL;
  45810. DH* dh = NULL;
  45811. const BIGNUM* pubkey = NULL;
  45812. const BIGNUM* privkey = NULL;
  45813. ASN1_INTEGER* asn1int = NULL;
  45814. unsigned int length = 0;
  45815. byte* derBuffer = NULL;
  45816. #endif
  45817. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45818. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45819. /* Bad cases */
  45820. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  45821. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  45822. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  45823. /* Good case */
  45824. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  45825. EVP_PKEY_CTX_free(ctx);
  45826. ctx = NULL;
  45827. EVP_PKEY_free(pkey);
  45828. pkey = NULL;
  45829. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  45830. /* Test DH keygen */
  45831. {
  45832. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  45833. ExpectNotNull(dh = DH_get_2048_256());
  45834. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  45835. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  45836. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45837. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  45838. DH_free(dh);
  45839. dh = NULL;
  45840. EVP_PKEY_CTX_free(ctx);
  45841. EVP_PKEY_free(params);
  45842. /* try exporting generated key to DER, to verify */
  45843. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  45844. DH_get0_key(dh, &pubkey, &privkey);
  45845. ExpectNotNull(pubkey);
  45846. ExpectNotNull(privkey);
  45847. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  45848. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  45849. ASN1_INTEGER_free(asn1int);
  45850. DH_free(dh);
  45851. dh = NULL;
  45852. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45853. EVP_PKEY_free(pkey);
  45854. }
  45855. #endif
  45856. return EXPECT_RESULT();
  45857. }
  45858. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  45859. {
  45860. EXPECT_DECLS;
  45861. WOLFSSL_EVP_PKEY* pkey = NULL;
  45862. EVP_PKEY_CTX *ctx = NULL;
  45863. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45864. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45865. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  45866. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  45867. EVP_PKEY_CTX_free(ctx);
  45868. EVP_PKEY_free(pkey);
  45869. return EXPECT_RESULT();
  45870. }
  45871. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  45872. {
  45873. EXPECT_DECLS;
  45874. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  45875. WOLFSSL_EVP_PKEY* pkey = NULL;
  45876. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45877. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  45878. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  45879. EVP_PKEY_free(pkey);
  45880. #endif
  45881. return EXPECT_RESULT();
  45882. }
  45883. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  45884. {
  45885. EXPECT_DECLS;
  45886. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  45887. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  45888. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  45889. !defined(NO_FILESYSTEM)
  45890. WOLFSSL_EVP_PKEY* params = NULL;
  45891. WOLFSSL_EVP_PKEY* copy = NULL;
  45892. DH* dh = NULL;
  45893. BIGNUM* p1;
  45894. BIGNUM* g1;
  45895. BIGNUM* q1;
  45896. BIGNUM* p2;
  45897. BIGNUM* g2;
  45898. BIGNUM* q2;
  45899. /* create DH with DH_get_2048_256 params */
  45900. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  45901. ExpectNotNull(dh = DH_get_2048_256());
  45902. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  45903. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  45904. (const BIGNUM**)&q1,
  45905. (const BIGNUM**)&g1);
  45906. DH_free(dh);
  45907. dh = NULL;
  45908. /* create DH with random generated DH params */
  45909. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  45910. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  45911. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  45912. DH_free(dh);
  45913. dh = NULL;
  45914. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  45915. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  45916. ExpectNotNull(dh->p);
  45917. ExpectNotNull(dh->g);
  45918. ExpectNotNull(dh->q);
  45919. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  45920. (const BIGNUM**)&q2,
  45921. (const BIGNUM**)&g2);
  45922. ExpectIntEQ(BN_cmp(p1, p2), 0);
  45923. ExpectIntEQ(BN_cmp(q1, q2), 0);
  45924. ExpectIntEQ(BN_cmp(g1, g2), 0);
  45925. DH_free(dh);
  45926. dh = NULL;
  45927. EVP_PKEY_free(copy);
  45928. EVP_PKEY_free(params);
  45929. #endif
  45930. return EXPECT_RESULT();
  45931. }
  45932. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  45933. {
  45934. EXPECT_DECLS;
  45935. WOLFSSL_EVP_PKEY* pkey = NULL;
  45936. EVP_PKEY_CTX* ctx = NULL;
  45937. int bits = 2048;
  45938. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  45939. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45940. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  45941. WOLFSSL_SUCCESS);
  45942. EVP_PKEY_CTX_free(ctx);
  45943. EVP_PKEY_free(pkey);
  45944. return EXPECT_RESULT();
  45945. }
  45946. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  45947. {
  45948. EXPECT_DECLS;
  45949. /* This is large enough to be used for all key sizes */
  45950. byte key[AES_256_KEY_SIZE] = {0};
  45951. byte iv[AES_BLOCK_SIZE] = {0};
  45952. int i;
  45953. int nids[] = {
  45954. #ifdef HAVE_AES_CBC
  45955. NID_aes_128_cbc,
  45956. #endif
  45957. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  45958. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45959. #ifdef HAVE_AESGCM
  45960. NID_aes_128_gcm,
  45961. #endif
  45962. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45963. #ifdef WOLFSSL_AES_COUNTER
  45964. NID_aes_128_ctr,
  45965. #endif
  45966. #ifndef NO_DES3
  45967. NID_des_cbc,
  45968. NID_des_ede3_cbc,
  45969. #endif
  45970. };
  45971. int iv_lengths[] = {
  45972. #ifdef HAVE_AES_CBC
  45973. AES_BLOCK_SIZE,
  45974. #endif
  45975. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  45976. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  45977. #ifdef HAVE_AESGCM
  45978. GCM_NONCE_MID_SZ,
  45979. #endif
  45980. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  45981. #ifdef WOLFSSL_AES_COUNTER
  45982. AES_BLOCK_SIZE,
  45983. #endif
  45984. #ifndef NO_DES3
  45985. DES_BLOCK_SIZE,
  45986. DES_BLOCK_SIZE,
  45987. #endif
  45988. };
  45989. int nidsLen = (sizeof(nids)/sizeof(int));
  45990. for (i = 0; i < nidsLen; i++) {
  45991. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  45992. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  45993. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  45994. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  45995. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  45996. EVP_CIPHER_CTX_free(ctx);
  45997. }
  45998. return EXPECT_RESULT();
  45999. }
  46000. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  46001. {
  46002. EXPECT_DECLS;
  46003. byte key[AES_256_KEY_SIZE] = {0};
  46004. byte iv[AES_BLOCK_SIZE] = {0};
  46005. int i;
  46006. int nids[] = {
  46007. #ifdef HAVE_AES_CBC
  46008. NID_aes_128_cbc,
  46009. NID_aes_256_cbc,
  46010. #endif
  46011. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46012. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46013. #ifdef HAVE_AESGCM
  46014. NID_aes_128_gcm,
  46015. NID_aes_256_gcm,
  46016. #endif
  46017. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46018. #ifdef WOLFSSL_AES_COUNTER
  46019. NID_aes_128_ctr,
  46020. NID_aes_256_ctr,
  46021. #endif
  46022. #ifndef NO_DES3
  46023. NID_des_cbc,
  46024. NID_des_ede3_cbc,
  46025. #endif
  46026. };
  46027. int key_lengths[] = {
  46028. #ifdef HAVE_AES_CBC
  46029. AES_128_KEY_SIZE,
  46030. AES_256_KEY_SIZE,
  46031. #endif
  46032. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46033. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46034. #ifdef HAVE_AESGCM
  46035. AES_128_KEY_SIZE,
  46036. AES_256_KEY_SIZE,
  46037. #endif
  46038. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46039. #ifdef WOLFSSL_AES_COUNTER
  46040. AES_128_KEY_SIZE,
  46041. AES_256_KEY_SIZE,
  46042. #endif
  46043. #ifndef NO_DES3
  46044. DES_KEY_SIZE,
  46045. DES3_KEY_SIZE,
  46046. #endif
  46047. };
  46048. int nidsLen = (sizeof(nids)/sizeof(int));
  46049. for (i = 0; i < nidsLen; i++) {
  46050. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  46051. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  46052. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46053. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46054. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  46055. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  46056. WOLFSSL_SUCCESS);
  46057. EVP_CIPHER_CTX_free(ctx);
  46058. }
  46059. return EXPECT_RESULT();
  46060. }
  46061. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  46062. {
  46063. EXPECT_DECLS;
  46064. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  46065. int ivLen, keyLen;
  46066. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  46067. #ifdef HAVE_AESGCM
  46068. byte key[AES_128_KEY_SIZE] = {0};
  46069. byte iv[AES_BLOCK_SIZE] = {0};
  46070. const EVP_CIPHER *init = EVP_aes_128_gcm();
  46071. #else
  46072. byte key[DES3_KEY_SIZE] = {0};
  46073. byte iv[DES_BLOCK_SIZE] = {0};
  46074. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  46075. #endif
  46076. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46077. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46078. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  46079. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  46080. /* Bad cases */
  46081. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  46082. WOLFSSL_FAILURE);
  46083. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  46084. WOLFSSL_FAILURE);
  46085. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  46086. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  46087. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  46088. WOLFSSL_FAILURE);
  46089. /* Good case */
  46090. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  46091. EVP_CIPHER_CTX_free(ctx);
  46092. #endif
  46093. return EXPECT_RESULT();
  46094. }
  46095. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  46096. {
  46097. EXPECT_DECLS;
  46098. WOLFSSL_ENGINE* e = NULL;
  46099. int id = 0;
  46100. EVP_PKEY_CTX *ctx = NULL;
  46101. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  46102. EVP_PKEY_CTX_free(ctx);
  46103. return EXPECT_RESULT();
  46104. }
  46105. static int test_wolfSSL_EVP_rc4(void)
  46106. {
  46107. EXPECT_DECLS;
  46108. #if !defined(NO_RC4)
  46109. ExpectNotNull(wolfSSL_EVP_rc4());
  46110. #endif
  46111. return EXPECT_RESULT();
  46112. }
  46113. static int test_wolfSSL_EVP_enc_null(void)
  46114. {
  46115. EXPECT_DECLS;
  46116. ExpectNotNull(wolfSSL_EVP_enc_null());
  46117. return EXPECT_RESULT();
  46118. }
  46119. static int test_wolfSSL_EVP_rc2_cbc(void)
  46120. {
  46121. EXPECT_DECLS;
  46122. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  46123. ExpectNull(wolfSSL_EVP_rc2_cbc());
  46124. #endif
  46125. return EXPECT_RESULT();
  46126. }
  46127. static int test_wolfSSL_EVP_mdc2(void)
  46128. {
  46129. EXPECT_DECLS;
  46130. #if !defined(NO_WOLFSSL_STUB)
  46131. ExpectNull(wolfSSL_EVP_mdc2());
  46132. #endif
  46133. return EXPECT_RESULT();
  46134. }
  46135. static int test_wolfSSL_EVP_md4(void)
  46136. {
  46137. EXPECT_DECLS;
  46138. #if !defined(NO_MD4)
  46139. ExpectNotNull(wolfSSL_EVP_md4());
  46140. #endif
  46141. return EXPECT_RESULT();
  46142. }
  46143. static int test_wolfSSL_EVP_aes_256_gcm(void)
  46144. {
  46145. EXPECT_DECLS;
  46146. #ifdef HAVE_AESGCM
  46147. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  46148. #endif
  46149. return EXPECT_RESULT();
  46150. }
  46151. static int test_wolfSSL_EVP_aes_192_gcm(void)
  46152. {
  46153. EXPECT_DECLS;
  46154. #ifdef HAVE_AESGCM
  46155. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  46156. #endif
  46157. return EXPECT_RESULT();
  46158. }
  46159. static int test_wolfSSL_EVP_aes_256_ccm(void)
  46160. {
  46161. EXPECT_DECLS;
  46162. #ifdef HAVE_AESCCM
  46163. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  46164. #endif
  46165. return EXPECT_RESULT();
  46166. }
  46167. static int test_wolfSSL_EVP_aes_192_ccm(void)
  46168. {
  46169. EXPECT_DECLS;
  46170. #ifdef HAVE_AESCCM
  46171. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  46172. #endif
  46173. return EXPECT_RESULT();
  46174. }
  46175. static int test_wolfSSL_EVP_aes_128_ccm(void)
  46176. {
  46177. EXPECT_DECLS;
  46178. #ifdef HAVE_AESCCM
  46179. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  46180. #endif
  46181. return EXPECT_RESULT();
  46182. }
  46183. static int test_wolfSSL_EVP_ripemd160(void)
  46184. {
  46185. EXPECT_DECLS;
  46186. #if !defined(NO_WOLFSSL_STUB)
  46187. ExpectNull(wolfSSL_EVP_ripemd160());
  46188. #endif
  46189. return EXPECT_RESULT();
  46190. }
  46191. static int test_wolfSSL_EVP_get_digestbynid(void)
  46192. {
  46193. EXPECT_DECLS;
  46194. #ifndef NO_MD5
  46195. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  46196. #endif
  46197. #ifndef NO_SHA
  46198. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  46199. #endif
  46200. #ifndef NO_SHA256
  46201. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  46202. #endif
  46203. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  46204. return EXPECT_RESULT();
  46205. }
  46206. static int test_wolfSSL_EVP_MD_nid(void)
  46207. {
  46208. EXPECT_DECLS;
  46209. #ifndef NO_MD5
  46210. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  46211. #endif
  46212. #ifndef NO_SHA
  46213. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  46214. #endif
  46215. #ifndef NO_SHA256
  46216. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  46217. #endif
  46218. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  46219. return EXPECT_RESULT();
  46220. }
  46221. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  46222. {
  46223. EXPECT_DECLS;
  46224. #if defined(HAVE_ECC)
  46225. WOLFSSL_EVP_PKEY* pkey = NULL;
  46226. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  46227. ExpectNotNull(pkey = EVP_PKEY_new());
  46228. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  46229. EVP_PKEY_free(pkey);
  46230. #endif
  46231. return EXPECT_RESULT();
  46232. }
  46233. static int test_wolfSSL_EVP_X_STATE(void)
  46234. {
  46235. EXPECT_DECLS;
  46236. #if !defined(NO_DES3) && !defined(NO_RC4)
  46237. byte key[DES3_KEY_SIZE] = {0};
  46238. byte iv[DES_IV_SIZE] = {0};
  46239. EVP_CIPHER_CTX *ctx = NULL;
  46240. const EVP_CIPHER *init = NULL;
  46241. /* Bad test cases */
  46242. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46243. ExpectNotNull(init = EVP_des_ede3_cbc());
  46244. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46245. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46246. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  46247. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  46248. EVP_CIPHER_CTX_free(ctx);
  46249. ctx = NULL;
  46250. /* Good test case */
  46251. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46252. ExpectNotNull(init = wolfSSL_EVP_rc4());
  46253. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46254. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46255. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  46256. EVP_CIPHER_CTX_free(ctx);
  46257. #endif
  46258. return EXPECT_RESULT();
  46259. }
  46260. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  46261. {
  46262. EXPECT_DECLS;
  46263. #if !defined(NO_DES3) && !defined(NO_RC4)
  46264. byte key[DES3_KEY_SIZE] = {0};
  46265. byte iv[DES_IV_SIZE] = {0};
  46266. EVP_CIPHER_CTX *ctx = NULL;
  46267. const EVP_CIPHER *init = NULL;
  46268. /* Bad test cases */
  46269. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46270. ExpectNotNull(init = EVP_des_ede3_cbc());
  46271. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46272. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46273. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  46274. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  46275. EVP_CIPHER_CTX_free(ctx);
  46276. ctx = NULL;
  46277. /* Good test case */
  46278. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  46279. ExpectNotNull(init = wolfSSL_EVP_rc4());
  46280. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  46281. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  46282. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  46283. EVP_CIPHER_CTX_free(ctx);
  46284. #endif
  46285. return EXPECT_RESULT();
  46286. }
  46287. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  46288. {
  46289. EXPECT_DECLS;
  46290. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  46291. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  46292. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  46293. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  46294. #ifdef HAVE_AES_CBC
  46295. #ifdef WOLFSSL_AES_128
  46296. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  46297. #endif
  46298. #ifdef WOLFSSL_AES_192
  46299. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  46300. #endif
  46301. #ifdef WOLFSSL_AES_256
  46302. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  46303. #endif
  46304. #endif
  46305. #ifdef HAVE_AESGCM
  46306. #ifdef WOLFSSL_AES_128
  46307. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  46308. #endif
  46309. #ifdef WOLFSSL_AES_192
  46310. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  46311. #endif
  46312. #ifdef WOLFSSL_AES_256
  46313. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  46314. #endif
  46315. #endif
  46316. #ifdef HAVE_AESCCM
  46317. #ifdef WOLFSSL_AES_128
  46318. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  46319. #endif
  46320. #ifdef WOLFSSL_AES_192
  46321. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  46322. #endif
  46323. #ifdef WOLFSSL_AES_256
  46324. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  46325. #endif
  46326. #endif
  46327. #ifdef WOLFSSL_AES_COUNTER
  46328. #ifdef WOLFSSL_AES_128
  46329. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  46330. #endif
  46331. #ifdef WOLFSSL_AES_192
  46332. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  46333. #endif
  46334. #ifdef WOLFSSL_AES_256
  46335. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  46336. #endif
  46337. #endif
  46338. #ifdef HAVE_AES_ECB
  46339. #ifdef WOLFSSL_AES_128
  46340. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  46341. #endif
  46342. #ifdef WOLFSSL_AES_192
  46343. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  46344. #endif
  46345. #ifdef WOLFSSL_AES_256
  46346. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  46347. #endif
  46348. #endif
  46349. #ifdef WOLFSSL_AES_OFB
  46350. #ifdef WOLFSSL_AES_128
  46351. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  46352. #endif
  46353. #ifdef WOLFSSL_AES_192
  46354. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  46355. #endif
  46356. #ifdef WOLFSSL_AES_256
  46357. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  46358. #endif
  46359. #endif
  46360. #ifndef NO_RC4
  46361. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  46362. #endif
  46363. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46364. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  46365. #endif
  46366. #endif
  46367. #ifdef WOLFSSL_SM4_ECB
  46368. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  46369. #endif
  46370. #ifdef WOLFSSL_SM4_CBC
  46371. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  46372. #endif
  46373. #ifdef WOLFSSL_SM4_CTR
  46374. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  46375. #endif
  46376. #ifdef WOLFSSL_SM4_GCM
  46377. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  46378. #endif
  46379. #ifdef WOLFSSL_SM4_CCM
  46380. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  46381. #endif
  46382. return EXPECT_RESULT();
  46383. }
  46384. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  46385. {
  46386. EXPECT_DECLS;
  46387. int nids[] = {
  46388. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  46389. #ifdef WOLFSSL_AES_128
  46390. NID_aes_128_cbc,
  46391. #endif
  46392. #ifdef WOLFSSL_AES_192
  46393. NID_aes_192_cbc,
  46394. #endif
  46395. #ifdef WOLFSSL_AES_256
  46396. NID_aes_256_cbc,
  46397. #endif
  46398. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  46399. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46400. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46401. #ifdef HAVE_AESGCM
  46402. #ifdef WOLFSSL_AES_128
  46403. NID_aes_128_gcm,
  46404. #endif
  46405. #ifdef WOLFSSL_AES_192
  46406. NID_aes_192_gcm,
  46407. #endif
  46408. #ifdef WOLFSSL_AES_256
  46409. NID_aes_256_gcm,
  46410. #endif
  46411. #endif /* HAVE_AESGCM */
  46412. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46413. #ifdef WOLFSSL_AES_COUNTER
  46414. #ifdef WOLFSSL_AES_128
  46415. NID_aes_128_ctr,
  46416. #endif
  46417. #ifdef WOLFSSL_AES_192
  46418. NID_aes_192_ctr,
  46419. #endif
  46420. #ifdef WOLFSSL_AES_256
  46421. NID_aes_256_ctr,
  46422. #endif
  46423. #endif
  46424. #ifndef NO_DES3
  46425. NID_des_cbc,
  46426. NID_des_ede3_cbc,
  46427. #endif
  46428. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46429. NID_chacha20_poly1305,
  46430. #endif
  46431. };
  46432. int iv_lengths[] = {
  46433. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  46434. #ifdef WOLFSSL_AES_128
  46435. AES_BLOCK_SIZE,
  46436. #endif
  46437. #ifdef WOLFSSL_AES_192
  46438. AES_BLOCK_SIZE,
  46439. #endif
  46440. #ifdef WOLFSSL_AES_256
  46441. AES_BLOCK_SIZE,
  46442. #endif
  46443. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  46444. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  46445. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  46446. #ifdef HAVE_AESGCM
  46447. #ifdef WOLFSSL_AES_128
  46448. GCM_NONCE_MID_SZ,
  46449. #endif
  46450. #ifdef WOLFSSL_AES_192
  46451. GCM_NONCE_MID_SZ,
  46452. #endif
  46453. #ifdef WOLFSSL_AES_256
  46454. GCM_NONCE_MID_SZ,
  46455. #endif
  46456. #endif /* HAVE_AESGCM */
  46457. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  46458. #ifdef WOLFSSL_AES_COUNTER
  46459. #ifdef WOLFSSL_AES_128
  46460. AES_BLOCK_SIZE,
  46461. #endif
  46462. #ifdef WOLFSSL_AES_192
  46463. AES_BLOCK_SIZE,
  46464. #endif
  46465. #ifdef WOLFSSL_AES_256
  46466. AES_BLOCK_SIZE,
  46467. #endif
  46468. #endif
  46469. #ifndef NO_DES3
  46470. DES_BLOCK_SIZE,
  46471. DES_BLOCK_SIZE,
  46472. #endif
  46473. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  46474. CHACHA20_POLY1305_AEAD_IV_SIZE,
  46475. #endif
  46476. };
  46477. int i;
  46478. int nidsLen = (sizeof(nids)/sizeof(int));
  46479. for (i = 0; i < nidsLen; i++) {
  46480. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  46481. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  46482. }
  46483. return EXPECT_RESULT();
  46484. }
  46485. static int test_wolfSSL_EVP_SignInit_ex(void)
  46486. {
  46487. EXPECT_DECLS;
  46488. WOLFSSL_EVP_MD_CTX mdCtx;
  46489. WOLFSSL_ENGINE* e = 0;
  46490. const EVP_MD* md = EVP_sha256();
  46491. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46492. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  46493. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  46494. return EXPECT_RESULT();
  46495. }
  46496. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  46497. {
  46498. EXPECT_DECLS;
  46499. #if !defined(NO_SHA256)
  46500. WOLFSSL_EVP_MD_CTX mdCtx;
  46501. unsigned int s = 0;
  46502. unsigned char md[WC_SHA256_DIGEST_SIZE];
  46503. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  46504. /* Bad Case */
  46505. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  46506. (HAVE_FIPS_VERSION > 2))
  46507. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46508. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  46509. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  46510. #else
  46511. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46512. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  46513. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  46514. #endif
  46515. /* Good Case */
  46516. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  46517. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  46518. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  46519. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  46520. #endif
  46521. return EXPECT_RESULT();
  46522. }
  46523. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  46524. {
  46525. EXPECT_DECLS;
  46526. #if defined(OPENSSL_EXTRA)
  46527. EVP_PKEY* pkey = NULL;
  46528. EVP_PKEY_CTX* ctx = NULL;
  46529. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46530. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46531. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  46532. /* void */
  46533. EVP_PKEY_CTX_free(ctx);
  46534. #else
  46535. /* int */
  46536. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  46537. #endif
  46538. EVP_PKEY_free(pkey);
  46539. #endif
  46540. return EXPECT_RESULT();
  46541. }
  46542. static int test_wolfSSL_EVP_PKEY_param_check(void)
  46543. {
  46544. EXPECT_DECLS;
  46545. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  46546. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  46547. DH *dh = NULL;
  46548. DH *setDh = NULL;
  46549. EVP_PKEY *pkey = NULL;
  46550. EVP_PKEY_CTX* ctx = NULL;
  46551. FILE* f = NULL;
  46552. unsigned char buf[512];
  46553. const unsigned char* pt = buf;
  46554. const char* dh2048 = "./certs/dh2048.der";
  46555. long len = 0;
  46556. int code = -1;
  46557. XMEMSET(buf, 0, sizeof(buf));
  46558. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  46559. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  46560. if (f != XBADFILE)
  46561. XFCLOSE(f);
  46562. /* Load dh2048.der into DH with internal format */
  46563. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  46564. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  46565. ExpectIntEQ(code, 0);
  46566. code = -1;
  46567. pkey = wolfSSL_EVP_PKEY_new();
  46568. /* Set DH into PKEY */
  46569. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  46570. /* create ctx from pkey */
  46571. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46572. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  46573. /* TODO: more invalid cases */
  46574. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  46575. EVP_PKEY_CTX_free(ctx);
  46576. EVP_PKEY_free(pkey);
  46577. DH_free(setDh);
  46578. setDh = NULL;
  46579. DH_free(dh);
  46580. dh = NULL;
  46581. #endif
  46582. #endif
  46583. return EXPECT_RESULT();
  46584. }
  46585. static int test_wolfSSL_EVP_BytesToKey(void)
  46586. {
  46587. EXPECT_DECLS;
  46588. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  46589. byte key[AES_BLOCK_SIZE] = {0};
  46590. byte iv[AES_BLOCK_SIZE] = {0};
  46591. int count = 0;
  46592. const EVP_MD* md = EVP_sha256();
  46593. const EVP_CIPHER *type;
  46594. const unsigned char *salt = (unsigned char *)"salt1234";
  46595. int sz = 5;
  46596. const byte data[] = {
  46597. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  46598. 0x72,0x6c,0x64
  46599. };
  46600. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  46601. /* Bad cases */
  46602. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  46603. 0);
  46604. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  46605. 16);
  46606. md = "2";
  46607. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  46608. WOLFSSL_FAILURE);
  46609. /* Good case */
  46610. md = EVP_sha256();
  46611. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  46612. 16);
  46613. #endif
  46614. return EXPECT_RESULT();
  46615. }
  46616. static int test_evp_cipher_aes_gcm(void)
  46617. {
  46618. EXPECT_DECLS;
  46619. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  46620. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  46621. (HAVE_FIPS_VERSION >= 2)))
  46622. /*
  46623. * This test checks data at various points in the encrypt/decrypt process
  46624. * against known values produced using the same test with OpenSSL. This
  46625. * interop testing is critical for verifying the correctness of our
  46626. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  46627. * a flow supported by OpenSSL that uses the control command
  46628. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  46629. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  46630. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  46631. * wolfSSL OpenSSH clients because there was a bug in this flow that
  46632. * happened to "cancel out" if both sides of the connection had the bug.
  46633. */
  46634. enum {
  46635. NUM_ENCRYPTIONS = 3,
  46636. AAD_SIZE = 4
  46637. };
  46638. byte plainText1[] = {
  46639. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  46640. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  46641. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  46642. };
  46643. byte plainText2[] = {
  46644. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  46645. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  46646. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  46647. };
  46648. byte plainText3[] = {
  46649. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  46650. 0x5b, 0x57, 0x10
  46651. };
  46652. byte* plainTexts[NUM_ENCRYPTIONS] = {
  46653. plainText1,
  46654. plainText2,
  46655. plainText3
  46656. };
  46657. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  46658. sizeof(plainText1),
  46659. sizeof(plainText2),
  46660. sizeof(plainText3)
  46661. };
  46662. byte aad1[AAD_SIZE] = {
  46663. 0x00, 0x00, 0x00, 0x01
  46664. };
  46665. byte aad2[AAD_SIZE] = {
  46666. 0x00, 0x00, 0x00, 0x10
  46667. };
  46668. byte aad3[AAD_SIZE] = {
  46669. 0x00, 0x00, 0x01, 0x00
  46670. };
  46671. byte* aads[NUM_ENCRYPTIONS] = {
  46672. aad1,
  46673. aad2,
  46674. aad3
  46675. };
  46676. const byte iv[GCM_NONCE_MID_SZ] = {
  46677. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  46678. };
  46679. byte currentIv[GCM_NONCE_MID_SZ];
  46680. const byte key[] = {
  46681. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  46682. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  46683. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  46684. };
  46685. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  46686. {
  46687. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46688. 0xEF
  46689. },
  46690. {
  46691. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46692. 0xF0
  46693. },
  46694. {
  46695. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  46696. 0xF1
  46697. }
  46698. };
  46699. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  46700. {
  46701. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  46702. 0x3D, 0x32, 0x18, 0x34, 0xA9
  46703. },
  46704. {
  46705. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  46706. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  46707. },
  46708. {
  46709. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  46710. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  46711. }
  46712. };
  46713. const byte expCipherText1[] = {
  46714. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  46715. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  46716. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  46717. };
  46718. const byte expCipherText2[] = {
  46719. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  46720. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  46721. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  46722. };
  46723. const byte expCipherText3[] = {
  46724. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  46725. 0x80, 0x5E, 0x6B,
  46726. };
  46727. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  46728. expCipherText1,
  46729. expCipherText2,
  46730. expCipherText3
  46731. };
  46732. byte* cipherText = NULL;
  46733. byte* calcPlainText = NULL;
  46734. byte tag[AES_BLOCK_SIZE];
  46735. EVP_CIPHER_CTX* encCtx = NULL;
  46736. EVP_CIPHER_CTX* decCtx = NULL;
  46737. int i, j, outl;
  46738. /****************************************************/
  46739. for (i = 0; i < 3; ++i) {
  46740. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  46741. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  46742. /* First iteration, set key before IV. */
  46743. if (i == 0) {
  46744. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  46745. SSL_SUCCESS);
  46746. /*
  46747. * The call to EVP_CipherInit below (with NULL key) should clear the
  46748. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  46749. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  46750. * behavior.
  46751. */
  46752. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46753. (void*)iv), SSL_SUCCESS);
  46754. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  46755. SSL_SUCCESS);
  46756. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46757. currentIv), SSL_FAILURE);
  46758. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  46759. SSL_SUCCESS);
  46760. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  46761. SSL_SUCCESS);
  46762. }
  46763. /* Second iteration, IV before key. */
  46764. else {
  46765. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  46766. SSL_SUCCESS);
  46767. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  46768. SSL_SUCCESS);
  46769. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  46770. SSL_SUCCESS);
  46771. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  46772. SSL_SUCCESS);
  46773. }
  46774. /*
  46775. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  46776. * been issued first.
  46777. */
  46778. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46779. currentIv), SSL_FAILURE);
  46780. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46781. (void*)iv), SSL_SUCCESS);
  46782. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  46783. (void*)iv), SSL_SUCCESS);
  46784. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  46785. /*************** Encrypt ***************/
  46786. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46787. currentIv), SSL_SUCCESS);
  46788. /* Check current IV against expected. */
  46789. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  46790. /* Add AAD. */
  46791. if (i == 2) {
  46792. /* Test streaming API. */
  46793. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  46794. AAD_SIZE), SSL_SUCCESS);
  46795. }
  46796. else {
  46797. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  46798. AAD_SIZE);
  46799. }
  46800. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  46801. DYNAMIC_TYPE_TMP_BUFFER));
  46802. /* Encrypt plaintext. */
  46803. if (i == 2) {
  46804. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  46805. plainTexts[j], plainTextSzs[j]),
  46806. SSL_SUCCESS);
  46807. }
  46808. else {
  46809. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  46810. plainTextSzs[j]), plainTextSzs[j]);
  46811. }
  46812. if (i == 2) {
  46813. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  46814. SSL_SUCCESS);
  46815. }
  46816. else {
  46817. /*
  46818. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  46819. * akin to calling EVP_CipherFinal.
  46820. */
  46821. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  46822. }
  46823. /* Check ciphertext against expected. */
  46824. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  46825. 0);
  46826. /* Get and check tag against expected. */
  46827. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  46828. sizeof(tag), tag), SSL_SUCCESS);
  46829. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  46830. /*************** Decrypt ***************/
  46831. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  46832. currentIv), SSL_SUCCESS);
  46833. /* Check current IV against expected. */
  46834. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  46835. /* Add AAD. */
  46836. if (i == 2) {
  46837. /* Test streaming API. */
  46838. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  46839. AAD_SIZE), SSL_SUCCESS);
  46840. }
  46841. else {
  46842. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  46843. AAD_SIZE);
  46844. }
  46845. /* Set expected tag. */
  46846. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  46847. sizeof(tag), tag), SSL_SUCCESS);
  46848. /* Decrypt ciphertext. */
  46849. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  46850. DYNAMIC_TYPE_TMP_BUFFER));
  46851. if (i == 2) {
  46852. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  46853. cipherText, plainTextSzs[j]),
  46854. SSL_SUCCESS);
  46855. }
  46856. else {
  46857. /* This first EVP_Cipher call will check the tag, too. */
  46858. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  46859. plainTextSzs[j]), plainTextSzs[j]);
  46860. }
  46861. if (i == 2) {
  46862. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  46863. SSL_SUCCESS);
  46864. }
  46865. else {
  46866. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  46867. }
  46868. /* Check plaintext against expected. */
  46869. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  46870. 0);
  46871. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46872. cipherText = NULL;
  46873. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46874. calcPlainText = NULL;
  46875. }
  46876. EVP_CIPHER_CTX_free(encCtx);
  46877. encCtx = NULL;
  46878. EVP_CIPHER_CTX_free(decCtx);
  46879. decCtx = NULL;
  46880. }
  46881. #endif
  46882. return EXPECT_RESULT();
  46883. }
  46884. static int test_wolfSSL_OBJ_ln(void)
  46885. {
  46886. EXPECT_DECLS;
  46887. const int nid_set[] = {
  46888. NID_commonName,
  46889. NID_serialNumber,
  46890. NID_countryName,
  46891. NID_localityName,
  46892. NID_stateOrProvinceName,
  46893. NID_organizationName,
  46894. NID_organizationalUnitName,
  46895. NID_domainComponent,
  46896. NID_businessCategory,
  46897. NID_jurisdictionCountryName,
  46898. NID_jurisdictionStateOrProvinceName,
  46899. NID_emailAddress
  46900. };
  46901. const char* ln_set[] = {
  46902. "commonName",
  46903. "serialNumber",
  46904. "countryName",
  46905. "localityName",
  46906. "stateOrProvinceName",
  46907. "organizationName",
  46908. "organizationalUnitName",
  46909. "domainComponent",
  46910. "businessCategory",
  46911. "jurisdictionCountryName",
  46912. "jurisdictionStateOrProvinceName",
  46913. "emailAddress",
  46914. };
  46915. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  46916. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  46917. #ifdef HAVE_ECC
  46918. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46919. {
  46920. EC_builtin_curve r[27];
  46921. size_t nCurves = sizeof(r) / sizeof(r[0]);
  46922. nCurves = EC_get_builtin_curves(r, nCurves);
  46923. for (i = 0; i < nCurves; i++) {
  46924. /* skip ECC_CURVE_INVALID */
  46925. if (r[i].nid != ECC_CURVE_INVALID) {
  46926. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  46927. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  46928. }
  46929. }
  46930. }
  46931. #endif
  46932. #endif
  46933. for (i = 0; i < maxIdx; i++) {
  46934. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  46935. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  46936. }
  46937. return EXPECT_RESULT();
  46938. }
  46939. static int test_wolfSSL_OBJ_sn(void)
  46940. {
  46941. EXPECT_DECLS;
  46942. int i = 0, maxIdx = 7;
  46943. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  46944. NID_stateOrProvinceName,NID_organizationName,
  46945. NID_organizationalUnitName,NID_emailAddress};
  46946. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  46947. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  46948. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  46949. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  46950. WOLFSSL_EMAIL_ADDR};
  46951. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  46952. for (i = 0; i < maxIdx; i++) {
  46953. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  46954. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  46955. }
  46956. return EXPECT_RESULT();
  46957. }
  46958. #if !defined(NO_BIO)
  46959. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  46960. {
  46961. return lh_strhash(s[3]);
  46962. }
  46963. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  46964. {
  46965. return XSTRCMP(a[3], b[3]);
  46966. }
  46967. #endif
  46968. static int test_wolfSSL_TXT_DB(void)
  46969. {
  46970. EXPECT_DECLS;
  46971. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  46972. BIO *bio = NULL;
  46973. TXT_DB *db = NULL;
  46974. const int columns = 6;
  46975. const char *fields[6] = {
  46976. "V",
  46977. "320926161116Z",
  46978. "",
  46979. "12BD",
  46980. "unknown",
  46981. "/CN=rsa doe",
  46982. };
  46983. char** fields_copy = NULL;
  46984. /* Test read */
  46985. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  46986. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  46987. ExpectNotNull(db = TXT_DB_read(bio, columns));
  46988. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  46989. DYNAMIC_TYPE_OPENSSL));
  46990. if (fields_copy != NULL) {
  46991. XMEMCPY(fields_copy, fields, sizeof(fields));
  46992. }
  46993. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  46994. if (EXPECT_FAIL()) {
  46995. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  46996. }
  46997. BIO_free(bio);
  46998. bio = NULL;
  46999. /* Test write */
  47000. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47001. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  47002. BIO_free(bio);
  47003. /* Test index */
  47004. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  47005. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  47006. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47007. fields[3] = "12DA";
  47008. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47009. fields[3] = "FFFF";
  47010. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47011. fields[3] = "";
  47012. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  47013. TXT_DB_free(db);
  47014. #endif
  47015. return EXPECT_RESULT();
  47016. }
  47017. static int test_wolfSSL_NCONF(void)
  47018. {
  47019. EXPECT_DECLS;
  47020. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  47021. const char* confFile = "./tests/NCONF_test.cnf";
  47022. CONF* conf = NULL;
  47023. long eline = 0;
  47024. long num = 0;
  47025. ExpectNotNull(conf = NCONF_new(NULL));
  47026. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  47027. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  47028. ExpectIntEQ(num, 1234);
  47029. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  47030. ExpectIntEQ(num, 4321);
  47031. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  47032. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  47033. "./test-dir/file1");
  47034. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  47035. "./test-dir/file1:./test-dir/file2:./section1:file2");
  47036. NCONF_free(conf);
  47037. #endif
  47038. return EXPECT_RESULT();
  47039. }
  47040. #endif /* OPENSSL_ALL */
  47041. static int test_wolfSSL_X509V3_EXT_get(void) {
  47042. EXPECT_DECLS;
  47043. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47044. XFILE f = XBADFILE;
  47045. int numOfExt =0;
  47046. int extNid = 0;
  47047. int i = 0;
  47048. WOLFSSL_X509* x509 = NULL;
  47049. WOLFSSL_X509_EXTENSION* ext = NULL;
  47050. const WOLFSSL_v3_ext_method* method = NULL;
  47051. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47052. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47053. if (f != XBADFILE)
  47054. XFCLOSE(f);
  47055. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  47056. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  47057. for (i = 0; i < numOfExt; i++) {
  47058. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47059. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  47060. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47061. ExpectIntEQ(method->ext_nid, extNid);
  47062. }
  47063. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  47064. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  47065. wolfSSL_X509_free(x509);
  47066. #endif
  47067. return EXPECT_RESULT();
  47068. }
  47069. static int test_wolfSSL_X509V3_EXT_nconf(void)
  47070. {
  47071. EXPECT_DECLS;
  47072. #ifdef OPENSSL_ALL
  47073. const char *ext_names[] = {
  47074. "subjectKeyIdentifier",
  47075. "authorityKeyIdentifier",
  47076. "subjectAltName",
  47077. "keyUsage",
  47078. "extendedKeyUsage",
  47079. };
  47080. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  47081. int ext_nids[] = {
  47082. NID_subject_key_identifier,
  47083. NID_authority_key_identifier,
  47084. NID_subject_alt_name,
  47085. NID_key_usage,
  47086. NID_ext_key_usage,
  47087. };
  47088. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  47089. const char *ext_values[] = {
  47090. "hash",
  47091. "hash",
  47092. "DNS:example.com, IP:127.0.0.1",
  47093. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  47094. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  47095. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  47096. "OCSPSigning",
  47097. };
  47098. size_t i;
  47099. X509_EXTENSION* ext = NULL;
  47100. X509* x509 = NULL;
  47101. unsigned int keyUsageFlags;
  47102. unsigned int extKeyUsageFlags;
  47103. ExpectNotNull(x509 = X509_new());
  47104. /* keyUsage / extKeyUsage should match string above */
  47105. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47106. | KU_NON_REPUDIATION
  47107. | KU_KEY_ENCIPHERMENT
  47108. | KU_DATA_ENCIPHERMENT
  47109. | KU_KEY_AGREEMENT
  47110. | KU_KEY_CERT_SIGN
  47111. | KU_CRL_SIGN
  47112. | KU_ENCIPHER_ONLY
  47113. | KU_DECIPHER_ONLY;
  47114. extKeyUsageFlags = XKU_SSL_CLIENT
  47115. | XKU_SSL_SERVER
  47116. | XKU_CODE_SIGN
  47117. | XKU_SMIME
  47118. | XKU_TIMESTAMP
  47119. | XKU_OCSP_SIGN;
  47120. for (i = 0; i < ext_names_count; i++) {
  47121. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  47122. ext_values[i]));
  47123. X509_EXTENSION_free(ext);
  47124. ext = NULL;
  47125. }
  47126. for (i = 0; i < ext_nids_count; i++) {
  47127. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  47128. ext_values[i]));
  47129. X509_EXTENSION_free(ext);
  47130. ext = NULL;
  47131. }
  47132. /* Test adding extension to X509 */
  47133. for (i = 0; i < ext_nids_count; i++) {
  47134. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  47135. ext_values[i]));
  47136. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  47137. if (ext_nids[i] == NID_key_usage) {
  47138. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47139. }
  47140. else if (ext_nids[i] == NID_ext_key_usage) {
  47141. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  47142. }
  47143. X509_EXTENSION_free(ext);
  47144. ext = NULL;
  47145. }
  47146. X509_free(x509);
  47147. #endif
  47148. return EXPECT_RESULT();
  47149. }
  47150. static int test_wolfSSL_X509V3_EXT(void) {
  47151. EXPECT_DECLS;
  47152. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47153. XFILE f = XBADFILE;
  47154. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  47155. char* str = NULL;
  47156. unsigned char* data = NULL;
  47157. const WOLFSSL_v3_ext_method* method = NULL;
  47158. WOLFSSL_X509* x509 = NULL;
  47159. WOLFSSL_X509_EXTENSION* ext = NULL;
  47160. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  47161. WOLFSSL_ASN1_OBJECT *obj = NULL;
  47162. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  47163. WOLFSSL_ASN1_STRING* asn1str = NULL;
  47164. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  47165. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  47166. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  47167. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  47168. WOLFSSL_GENERAL_NAME* gn = NULL;
  47169. /* Check NULL argument */
  47170. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  47171. /* Using OCSP cert with X509V3 extensions */
  47172. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  47173. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47174. if (f != XBADFILE)
  47175. XFCLOSE(f);
  47176. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  47177. /* Basic Constraints */
  47178. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47179. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47180. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  47181. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  47182. ExpectIntEQ(bc->ca, 1);
  47183. ExpectNull(bc->pathlen);
  47184. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  47185. bc = NULL;
  47186. i++;
  47187. /* Subject Key Identifier */
  47188. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47189. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47190. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  47191. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  47192. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  47193. asn1str));
  47194. X509_EXTENSION_free(ext2);
  47195. ext2 = NULL;
  47196. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47197. ExpectNotNull(method->i2s);
  47198. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  47199. wolfSSL_ASN1_STRING_free(asn1str);
  47200. asn1str = NULL;
  47201. if (str != NULL) {
  47202. actual = strcmp(str,
  47203. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  47204. }
  47205. ExpectIntEQ(actual, 0);
  47206. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47207. str = NULL;
  47208. i++;
  47209. /* Authority Key Identifier */
  47210. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47211. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47212. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  47213. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  47214. ext));
  47215. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  47216. ExpectNotNull(asn1str = aKeyId->keyid);
  47217. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  47218. asn1str));
  47219. asn1str = NULL;
  47220. if (str != NULL) {
  47221. actual = strcmp(str,
  47222. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  47223. }
  47224. ExpectIntEQ(actual, 0);
  47225. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47226. str = NULL;
  47227. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  47228. aKeyId = NULL;
  47229. i++;
  47230. /* Key Usage */
  47231. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47232. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47233. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  47234. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  47235. #if defined(WOLFSSL_QT)
  47236. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  47237. #else
  47238. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  47239. #endif
  47240. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  47241. if (data != NULL) {
  47242. #ifdef BIG_ENDIAN_ORDER
  47243. actual = data[1];
  47244. #else
  47245. actual = data[0];
  47246. #endif
  47247. }
  47248. ExpectIntEQ(actual, expected);
  47249. wolfSSL_ASN1_STRING_free(asn1str);
  47250. asn1str = NULL;
  47251. #if 1
  47252. i++;
  47253. /* Authority Info Access */
  47254. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  47255. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  47256. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  47257. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  47258. ext));
  47259. #if defined(WOLFSSL_QT)
  47260. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  47261. #else
  47262. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  47263. #endif
  47264. /* URI entry is an ACCESS_DESCRIPTION type */
  47265. #if defined(WOLFSSL_QT)
  47266. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  47267. #else
  47268. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  47269. #endif
  47270. ExpectNotNull(adObj = ad->method);
  47271. /* Make sure nid is OCSP */
  47272. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  47273. /* GENERAL_NAME stores URI as an ASN1_STRING */
  47274. ExpectNotNull(gn = ad->location);
  47275. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  47276. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  47277. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  47278. #if defined(WOLFSSL_QT)
  47279. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  47280. #else
  47281. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  47282. #endif
  47283. if (str != NULL) {
  47284. actual = strcmp(str, "http://127.0.0.1:22220");
  47285. }
  47286. ExpectIntEQ(actual, 0);
  47287. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  47288. aia = NULL;
  47289. #else
  47290. (void) aia; (void) ad; (void) adObj; (void) gn;
  47291. #endif
  47292. wolfSSL_X509_free(x509);
  47293. #endif
  47294. return EXPECT_RESULT();
  47295. }
  47296. static int test_wolfSSL_X509_get_extension_flags(void)
  47297. {
  47298. EXPECT_DECLS;
  47299. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47300. XFILE f = XBADFILE;
  47301. X509* x509 = NULL;
  47302. unsigned int extFlags;
  47303. unsigned int keyUsageFlags;
  47304. unsigned int extKeyUsageFlags;
  47305. /* client-int-cert.pem has the following extension flags. */
  47306. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  47307. /* and the following key usage flags. */
  47308. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47309. | KU_NON_REPUDIATION
  47310. | KU_KEY_ENCIPHERMENT;
  47311. /* and the following extended key usage flags. */
  47312. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  47313. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  47314. XBADFILE);
  47315. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47316. if (f != XBADFILE) {
  47317. XFCLOSE(f);
  47318. f = XBADFILE;
  47319. }
  47320. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  47321. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47322. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  47323. X509_free(x509);
  47324. x509 = NULL;
  47325. /* client-cert-ext.pem has the following extension flags. */
  47326. extFlags = EXFLAG_KUSAGE;
  47327. /* and the following key usage flags. */
  47328. keyUsageFlags = KU_DIGITAL_SIGNATURE
  47329. | KU_KEY_CERT_SIGN
  47330. | KU_CRL_SIGN;
  47331. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  47332. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47333. if (f != XBADFILE)
  47334. XFCLOSE(f);
  47335. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  47336. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  47337. X509_free(x509);
  47338. #endif /* OPENSSL_ALL */
  47339. return EXPECT_RESULT();
  47340. }
  47341. static int test_wolfSSL_X509_get_ext(void)
  47342. {
  47343. EXPECT_DECLS;
  47344. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47345. int ret = 0;
  47346. XFILE f = XBADFILE;
  47347. WOLFSSL_X509* x509 = NULL;
  47348. WOLFSSL_X509_EXTENSION* foundExtension;
  47349. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47350. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47351. if (f != XBADFILE)
  47352. XFCLOSE(f);
  47353. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  47354. /* wolfSSL_X509_get_ext() valid input */
  47355. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  47356. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  47357. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  47358. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  47359. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  47360. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  47361. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  47362. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  47363. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  47364. wolfSSL_X509_free(x509);
  47365. #endif
  47366. return EXPECT_RESULT();
  47367. }
  47368. static int test_wolfSSL_X509_get_ext_by_NID(void)
  47369. {
  47370. EXPECT_DECLS;
  47371. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47372. int rc = 0;
  47373. XFILE f = XBADFILE;
  47374. WOLFSSL_X509* x509 = NULL;
  47375. ASN1_OBJECT* obj = NULL;
  47376. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47377. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47378. if (f != XBADFILE)
  47379. XFCLOSE(f);
  47380. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47381. -1), 0);
  47382. /* Start search from last location (should fail) */
  47383. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47384. rc), -1);
  47385. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  47386. -2), -1);
  47387. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  47388. -1), -1);
  47389. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  47390. /* NID_ext_key_usage, check also its nid and oid */
  47391. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  47392. -1);
  47393. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  47394. x509, rc)));
  47395. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  47396. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  47397. wolfSSL_X509_free(x509);
  47398. #endif
  47399. return EXPECT_RESULT();
  47400. }
  47401. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  47402. {
  47403. EXPECT_DECLS;
  47404. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47405. int rc = 0;
  47406. XFILE f = XBADFILE;
  47407. WOLFSSL_X509* x509 = NULL;
  47408. WOLFSSL_X509_EXTENSION* ext = NULL;
  47409. WOLFSSL_ASN1_STRING* sanString = NULL;
  47410. byte* sanDer = NULL;
  47411. const byte expectedDer[] = {
  47412. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  47413. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  47414. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47415. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  47416. if (f != XBADFILE)
  47417. XFCLOSE(f);
  47418. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  47419. ExpectNotNull(ext = X509_get_ext(x509, rc));
  47420. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  47421. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  47422. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  47423. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  47424. X509_free(x509);
  47425. #endif
  47426. return EXPECT_RESULT();
  47427. }
  47428. static int test_wolfSSL_X509_EXTENSION_new(void)
  47429. {
  47430. EXPECT_DECLS;
  47431. #if defined (OPENSSL_ALL)
  47432. WOLFSSL_X509_EXTENSION* ext = NULL;
  47433. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  47434. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  47435. wolfSSL_X509_EXTENSION_free(ext);
  47436. #endif
  47437. return EXPECT_RESULT();
  47438. }
  47439. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  47440. {
  47441. EXPECT_DECLS;
  47442. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47443. WOLFSSL_X509* x509 = NULL;
  47444. WOLFSSL_X509_EXTENSION* ext = NULL;
  47445. WOLFSSL_ASN1_OBJECT* o = NULL;
  47446. XFILE file = XBADFILE;
  47447. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47448. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47449. if (file != XBADFILE)
  47450. XFCLOSE(file);
  47451. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  47452. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47453. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  47454. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  47455. ExpectIntEQ(o->nid, 128);
  47456. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  47457. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  47458. wolfSSL_X509_free(x509);
  47459. #endif
  47460. return EXPECT_RESULT();
  47461. }
  47462. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  47463. {
  47464. EXPECT_DECLS;
  47465. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47466. WOLFSSL_X509* x509 = NULL;
  47467. WOLFSSL_X509_EXTENSION* ext = NULL;
  47468. WOLFSSL_ASN1_STRING* str = NULL;
  47469. XFILE file = XBADFILE;
  47470. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47471. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47472. if (file != XBADFILE)
  47473. XFCLOSE(file);
  47474. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47475. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  47476. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  47477. wolfSSL_X509_free(x509);
  47478. #endif
  47479. return EXPECT_RESULT();
  47480. }
  47481. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  47482. {
  47483. EXPECT_DECLS;
  47484. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  47485. WOLFSSL_X509* x509 = NULL;
  47486. WOLFSSL_X509_EXTENSION* ext = NULL;
  47487. XFILE file = XBADFILE;
  47488. int crit = 0;
  47489. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47490. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  47491. if (file != XBADFILE)
  47492. XFCLOSE(file);
  47493. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  47494. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  47495. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  47496. wolfSSL_X509_free(x509);
  47497. #endif
  47498. return EXPECT_RESULT();
  47499. }
  47500. static int test_wolfSSL_X509V3_EXT_print(void)
  47501. {
  47502. EXPECT_DECLS;
  47503. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  47504. !defined(NO_RSA)
  47505. {
  47506. XFILE f = XBADFILE;
  47507. WOLFSSL_X509* x509 = NULL;
  47508. X509_EXTENSION * ext = NULL;
  47509. int loc = 0;
  47510. BIO *bio = NULL;
  47511. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  47512. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  47513. if (f != XBADFILE)
  47514. fclose(f);
  47515. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  47516. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47517. NID_basic_constraints, -1), -1);
  47518. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47519. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47520. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47521. NID_subject_key_identifier, -1), -1);
  47522. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47523. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47524. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  47525. NID_authority_key_identifier, -1), -1);
  47526. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  47527. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  47528. wolfSSL_BIO_free(bio);
  47529. wolfSSL_X509_free(x509);
  47530. }
  47531. {
  47532. X509 *x509 = NULL;
  47533. BIO *bio = NULL;
  47534. X509_EXTENSION *ext = NULL;
  47535. unsigned int i = 0;
  47536. unsigned int idx = 0;
  47537. /* Some NIDs to test with */
  47538. int nids[] = {
  47539. /* NID_key_usage, currently X509_get_ext returns this as a bit
  47540. * string, which messes up X509V3_EXT_print */
  47541. /* NID_ext_key_usage, */
  47542. NID_subject_alt_name,
  47543. };
  47544. int* n = NULL;
  47545. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  47546. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  47547. WOLFSSL_FILETYPE_PEM));
  47548. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  47549. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  47550. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  47551. * update the index */
  47552. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  47553. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  47554. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  47555. ExpectIntGT(fprintf(stderr, "\n"), 0);
  47556. }
  47557. BIO_free(bio);
  47558. X509_free(x509);
  47559. }
  47560. #endif
  47561. return EXPECT_RESULT();
  47562. }
  47563. static int test_wolfSSL_X509_cmp(void)
  47564. {
  47565. EXPECT_DECLS;
  47566. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  47567. XFILE file1 = XBADFILE;
  47568. XFILE file2 = XBADFILE;
  47569. WOLFSSL_X509* cert1 = NULL;
  47570. WOLFSSL_X509* cert2 = NULL;
  47571. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  47572. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  47573. XBADFILE);
  47574. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  47575. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  47576. if (file1 != XBADFILE)
  47577. fclose(file1);
  47578. if (file2 != XBADFILE)
  47579. fclose(file2);
  47580. /* wolfSSL_X509_cmp() testing matching certs */
  47581. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  47582. /* wolfSSL_X509_cmp() testing mismatched certs */
  47583. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  47584. /* wolfSSL_X509_cmp() testing NULL, valid args */
  47585. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  47586. /* wolfSSL_X509_cmp() testing valid, NULL args */
  47587. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  47588. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  47589. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  47590. wolfSSL_X509_free(cert1);
  47591. wolfSSL_X509_free(cert2);
  47592. #endif
  47593. return EXPECT_RESULT();
  47594. }
  47595. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  47596. {
  47597. EXPECT_DECLS;
  47598. #if defined(OPENSSL_ALL)
  47599. EVP_PKEY* pkey;
  47600. pkey = EVP_PKEY_new();
  47601. ExpectNotNull(pkey);
  47602. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  47603. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  47604. EVP_PKEY_free(pkey);
  47605. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  47606. EVP_PKEY_free(pkey);
  47607. EVP_PKEY_free(pkey);
  47608. #endif
  47609. return EXPECT_RESULT();
  47610. }
  47611. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  47612. {
  47613. EXPECT_DECLS;
  47614. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47615. EVP_PKEY* pkey = NULL;
  47616. const unsigned char* p;
  47617. unsigned char *der = NULL;
  47618. unsigned char *tmp = NULL;
  47619. int derLen;
  47620. p = client_keypub_der_2048;
  47621. /* Check that key can be successfully decoded. */
  47622. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  47623. sizeof_client_keypub_der_2048));
  47624. /* Check that key can be successfully encoded. */
  47625. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  47626. /* Ensure that the encoded version matches the original. */
  47627. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  47628. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  47629. /* Do same test except with pre-allocated buffer to ensure the der pointer
  47630. * is advanced. */
  47631. tmp = der;
  47632. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  47633. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  47634. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  47635. ExpectTrue(der + derLen == tmp);
  47636. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47637. EVP_PKEY_free(pkey);
  47638. #endif
  47639. return EXPECT_RESULT();
  47640. }
  47641. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  47642. {
  47643. EXPECT_DECLS;
  47644. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  47645. !defined(NO_ASN) && !defined(NO_PWDBASED)
  47646. EVP_PKEY* pkey = NULL;
  47647. const unsigned char* p;
  47648. unsigned char *der = NULL;
  47649. unsigned char *tmp = NULL;
  47650. int derLen;
  47651. unsigned char pub_buf[65];
  47652. const int pub_len = 65;
  47653. BN_CTX* ctx;
  47654. EC_GROUP* curve = NULL;
  47655. EC_KEY* ephemeral_key = NULL;
  47656. const EC_POINT* h;
  47657. /* Generate an x963 key pair and get public part into pub_buf */
  47658. ExpectNotNull(ctx = BN_CTX_new());
  47659. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  47660. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  47661. NID_X9_62_prime256v1));
  47662. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  47663. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  47664. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  47665. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  47666. /* Prepare the EVP_PKEY */
  47667. ExpectNotNull(pkey = EVP_PKEY_new());
  47668. p = pub_buf;
  47669. /* Check that key can be successfully decoded. */
  47670. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  47671. pub_len));
  47672. /* Check that key can be successfully encoded. */
  47673. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  47674. /* Ensure that the encoded version matches the original. */
  47675. ExpectIntEQ(derLen, pub_len);
  47676. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  47677. /* Do same test except with pre-allocated buffer to ensure the der pointer
  47678. * is advanced. */
  47679. tmp = der;
  47680. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  47681. ExpectIntEQ(derLen, pub_len);
  47682. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  47683. ExpectTrue(der + derLen == tmp);
  47684. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47685. EVP_PKEY_free(pkey);
  47686. EC_KEY_free(ephemeral_key);
  47687. EC_GROUP_free(curve);
  47688. #endif
  47689. return EXPECT_RESULT();
  47690. }
  47691. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  47692. {
  47693. EXPECT_DECLS;
  47694. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  47695. DSA* dsa = NULL;
  47696. byte derIn[] = {
  47697. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  47698. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  47699. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  47700. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  47701. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  47702. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  47703. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  47704. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  47705. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  47706. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  47707. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  47708. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  47709. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  47710. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  47711. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  47712. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  47713. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  47714. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  47715. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  47716. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  47717. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  47718. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  47719. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  47720. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  47721. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  47722. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  47723. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  47724. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  47725. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  47726. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  47727. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  47728. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  47729. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  47730. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  47731. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  47732. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  47733. 0x1e, 0xde, 0xc4
  47734. };
  47735. int derInLen = sizeof(derIn);
  47736. byte* derOut = NULL;
  47737. int derOutLen;
  47738. byte* p = derIn;
  47739. /* Check that params can be successfully decoded. */
  47740. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  47741. /* Check that params can be successfully encoded. */
  47742. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  47743. /* Ensure that the encoded version matches the original. */
  47744. ExpectIntEQ(derInLen, derOutLen);
  47745. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  47746. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  47747. DSA_free(dsa);
  47748. #endif
  47749. return EXPECT_RESULT();
  47750. }
  47751. static int test_wolfSSL_i2d_PrivateKey(void)
  47752. {
  47753. EXPECT_DECLS;
  47754. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  47755. !defined(NO_ASN) && !defined(NO_PWDBASED)
  47756. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47757. {
  47758. EVP_PKEY* pkey = NULL;
  47759. const unsigned char* server_key =
  47760. (const unsigned char*)server_key_der_2048;
  47761. unsigned char buf[FOURK_BUF];
  47762. unsigned char* pt = NULL;
  47763. int bufSz = 0;
  47764. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  47765. (long)sizeof_server_key_der_2048));
  47766. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  47767. pt = buf;
  47768. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  47769. ExpectIntNE((pt - buf), 0);
  47770. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  47771. EVP_PKEY_free(pkey);
  47772. }
  47773. #endif
  47774. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47775. {
  47776. EVP_PKEY* pkey = NULL;
  47777. const unsigned char* client_key =
  47778. (const unsigned char*)ecc_clikey_der_256;
  47779. unsigned char buf[FOURK_BUF];
  47780. unsigned char* pt = NULL;
  47781. int bufSz = 0;
  47782. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  47783. (long)sizeof_ecc_clikey_der_256)));
  47784. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  47785. pt = buf;
  47786. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  47787. ExpectIntNE((pt - buf), 0);
  47788. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  47789. EVP_PKEY_free(pkey);
  47790. }
  47791. #endif
  47792. #endif
  47793. return EXPECT_RESULT();
  47794. }
  47795. static int test_wolfSSL_OCSP_id_get0_info(void)
  47796. {
  47797. EXPECT_DECLS;
  47798. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  47799. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47800. X509* cert = NULL;
  47801. X509* issuer = NULL;
  47802. OCSP_CERTID* id = NULL;
  47803. OCSP_CERTID* id2 = NULL;
  47804. ASN1_STRING* name = NULL;
  47805. ASN1_OBJECT* pmd = NULL;
  47806. ASN1_STRING* keyHash = NULL;
  47807. ASN1_INTEGER* serial = NULL;
  47808. ASN1_INTEGER* x509Int = NULL;
  47809. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  47810. SSL_FILETYPE_PEM));
  47811. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  47812. SSL_FILETYPE_PEM));
  47813. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  47814. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  47815. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  47816. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  47817. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  47818. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  47819. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  47820. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  47821. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  47822. ExpectNotNull(serial);
  47823. /* compare serial number to one in cert, should be equal */
  47824. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  47825. ExpectIntEQ(x509Int->length, serial->length);
  47826. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  47827. /* test OCSP_id_cmp */
  47828. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  47829. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  47830. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  47831. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  47832. if (id != NULL) {
  47833. id->issuerHash[0] = ~id->issuerHash[0];
  47834. }
  47835. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  47836. OCSP_CERTID_free(id);
  47837. OCSP_CERTID_free(id2);
  47838. X509_free(cert); /* free's x509Int */
  47839. X509_free(issuer);
  47840. #endif
  47841. return EXPECT_RESULT();
  47842. }
  47843. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  47844. {
  47845. EXPECT_DECLS;
  47846. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  47847. WOLFSSL_OCSP_CERTID certId;
  47848. byte* targetBuffer = NULL;
  47849. byte* p;
  47850. /* OCSP CertID bytes taken from PCAP */
  47851. byte rawCertId[] = {
  47852. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  47853. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  47854. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  47855. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  47856. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  47857. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  47858. 0xcf, 0xbc, 0x91
  47859. };
  47860. int ret = 0;
  47861. int i;
  47862. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  47863. certId.rawCertId = rawCertId;
  47864. certId.rawCertIdSize = sizeof(rawCertId);
  47865. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  47866. DYNAMIC_TYPE_TMP_BUFFER));
  47867. p = targetBuffer;
  47868. /* Function returns the size of the encoded data. */
  47869. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  47870. /* If target buffer is not null, function increments targetBuffer to point
  47871. * just past the end of the encoded data. */
  47872. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  47873. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  47874. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  47875. }
  47876. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47877. targetBuffer = NULL;
  47878. /* If target buffer is null, function allocates memory for a buffer and
  47879. * copies the encoded data into it. targetBuffer then points to the start of
  47880. * this newly allocate buffer. */
  47881. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  47882. sizeof(rawCertId));
  47883. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  47884. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  47885. }
  47886. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  47887. #endif
  47888. return EXPECT_RESULT();
  47889. }
  47890. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  47891. {
  47892. EXPECT_DECLS;
  47893. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  47894. WOLFSSL_OCSP_CERTID* certId;
  47895. WOLFSSL_OCSP_CERTID* certIdGood;
  47896. WOLFSSL_OCSP_CERTID* certIdBad;
  47897. const unsigned char* rawCertIdPtr;
  47898. const unsigned char rawCertId[] = {
  47899. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  47900. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  47901. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  47902. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  47903. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  47904. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  47905. 0xcf, 0xbc, 0x91
  47906. };
  47907. rawCertIdPtr = &rawCertId[0];
  47908. /* If the cert ID is NULL the function should allocate it and copy the
  47909. * data to it. */
  47910. certId = NULL;
  47911. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  47912. sizeof(rawCertId)));
  47913. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  47914. if (certId != NULL) {
  47915. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  47916. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  47917. }
  47918. /* If the cert ID is not NULL the function will just copy the data to it. */
  47919. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  47920. DYNAMIC_TYPE_TMP_BUFFER));
  47921. ExpectNotNull(certId);
  47922. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  47923. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  47924. rawCertIdPtr = &rawCertId[0];
  47925. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  47926. sizeof(rawCertId)));
  47927. ExpectPtrEq(certIdGood, certId);
  47928. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  47929. if (certId != NULL) {
  47930. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  47931. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47932. certId = NULL;
  47933. }
  47934. /* The below tests should fail when passed bad parameters. NULL should
  47935. * always be returned. */
  47936. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  47937. sizeof(rawCertId)));
  47938. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  47939. sizeof(rawCertId)));
  47940. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  47941. #endif
  47942. return EXPECT_RESULT();
  47943. }
  47944. static int test_wolfSSL_OCSP_id_cmp(void)
  47945. {
  47946. EXPECT_DECLS;
  47947. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  47948. OCSP_CERTID id1;
  47949. OCSP_CERTID id2;
  47950. XMEMSET(&id1, 0, sizeof(id1));
  47951. XMEMSET(&id2, 0, sizeof(id2));
  47952. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  47953. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  47954. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  47955. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  47956. #endif
  47957. return EXPECT_RESULT();
  47958. }
  47959. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  47960. {
  47961. EXPECT_DECLS;
  47962. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  47963. WOLFSSL_OCSP_SINGLERESP single;
  47964. const WOLFSSL_OCSP_CERTID* certId;
  47965. XMEMSET(&single, 0, sizeof(single));
  47966. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  47967. ExpectPtrEq(&single, certId);
  47968. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  47969. #endif
  47970. return EXPECT_RESULT();
  47971. }
  47972. static int test_wolfSSL_OCSP_single_get0_status(void)
  47973. {
  47974. EXPECT_DECLS;
  47975. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  47976. WOLFSSL_OCSP_SINGLERESP single;
  47977. CertStatus certStatus;
  47978. WOLFSSL_ASN1_TIME* thisDate;
  47979. WOLFSSL_ASN1_TIME* nextDate;
  47980. int ret, i;
  47981. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  47982. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  47983. /* Fill the date fields with some dummy data. */
  47984. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  47985. certStatus.thisDateParsed.data[i] = i;
  47986. certStatus.nextDateParsed.data[i] = i;
  47987. }
  47988. certStatus.status = CERT_GOOD;
  47989. single.status = &certStatus;
  47990. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  47991. &nextDate);
  47992. ExpectIntEQ(ret, CERT_GOOD);
  47993. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  47994. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  47995. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  47996. CERT_GOOD);
  47997. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  47998. NULL), CERT_GOOD);
  47999. #endif
  48000. return EXPECT_RESULT();
  48001. }
  48002. static int test_wolfSSL_OCSP_resp_count(void)
  48003. {
  48004. EXPECT_DECLS;
  48005. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48006. WOLFSSL_OCSP_BASICRESP basicResp;
  48007. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  48008. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  48009. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  48010. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48011. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48012. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  48013. basicResp.single = &singleRespOne;
  48014. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  48015. singleRespOne.next = &singleRespTwo;
  48016. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  48017. #endif
  48018. return EXPECT_RESULT();
  48019. }
  48020. static int test_wolfSSL_OCSP_resp_get0(void)
  48021. {
  48022. EXPECT_DECLS;
  48023. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  48024. WOLFSSL_OCSP_BASICRESP basicResp;
  48025. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  48026. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  48027. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  48028. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48029. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  48030. basicResp.single = &singleRespOne;
  48031. singleRespOne.next = &singleRespTwo;
  48032. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  48033. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  48034. #endif
  48035. return EXPECT_RESULT();
  48036. }
  48037. static int test_wolfSSL_EVP_PKEY_derive(void)
  48038. {
  48039. EXPECT_DECLS;
  48040. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  48041. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  48042. EVP_PKEY_CTX *ctx = NULL;
  48043. unsigned char *skey = NULL;
  48044. size_t skeylen;
  48045. EVP_PKEY *pkey = NULL;
  48046. EVP_PKEY *peerkey = NULL;
  48047. const unsigned char* key;
  48048. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  48049. /* DH */
  48050. key = dh_key_der_2048;
  48051. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  48052. sizeof_dh_key_der_2048)));
  48053. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  48054. key = dh_key_der_2048;
  48055. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  48056. sizeof_dh_key_der_2048)));
  48057. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  48058. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48059. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  48060. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  48061. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  48062. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  48063. DYNAMIC_TYPE_OPENSSL));
  48064. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  48065. EVP_PKEY_CTX_free(ctx);
  48066. ctx = NULL;
  48067. EVP_PKEY_free(peerkey);
  48068. peerkey = NULL;
  48069. EVP_PKEY_free(pkey);
  48070. pkey = NULL;
  48071. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  48072. skey = NULL;
  48073. #endif
  48074. #ifdef HAVE_ECC
  48075. /* ECDH */
  48076. key = ecc_clikey_der_256;
  48077. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  48078. sizeof_ecc_clikey_der_256)));
  48079. key = ecc_clikeypub_der_256;
  48080. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  48081. sizeof_ecc_clikeypub_der_256)));
  48082. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48083. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  48084. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  48085. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  48086. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  48087. DYNAMIC_TYPE_OPENSSL));
  48088. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  48089. EVP_PKEY_CTX_free(ctx);
  48090. EVP_PKEY_free(peerkey);
  48091. EVP_PKEY_free(pkey);
  48092. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  48093. #endif /* HAVE_ECC */
  48094. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  48095. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  48096. return EXPECT_RESULT();
  48097. }
  48098. static int test_wolfSSL_EVP_PBE_scrypt(void)
  48099. {
  48100. EXPECT_DECLS;
  48101. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  48102. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  48103. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  48104. int ret;
  48105. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  48106. int pwdlen = sizeof(pwd);
  48107. const byte salt[] = {'N','a','C','l'};
  48108. int saltlen = sizeof(salt);
  48109. byte key[80];
  48110. word64 numOvr32 = (word64)INT32_MAX + 1;
  48111. /* expected derived key for N:16, r:1, p:1 */
  48112. const byte expectedKey[] = {
  48113. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  48114. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  48115. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  48116. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  48117. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  48118. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  48119. 0xE7, 0xE9, 0xC0, 0x9A};
  48120. /* N r p mx key keylen */
  48121. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  48122. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  48123. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  48124. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  48125. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  48126. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  48127. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  48128. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  48129. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  48130. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  48131. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  48132. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  48133. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  48134. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  48135. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  48136. ExpectIntEQ(ret, 1); /* should succeed */
  48137. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  48138. key, 64);
  48139. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  48140. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  48141. key, 64);
  48142. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  48143. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  48144. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  48145. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  48146. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  48147. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  48148. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  48149. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  48150. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  48151. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  48152. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  48153. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  48154. ExpectIntEQ(ret, 1);
  48155. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  48156. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  48157. #endif /* !NO_PWDBASED && !NO_SHA256 */
  48158. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  48159. return EXPECT_RESULT();
  48160. }
  48161. static int test_no_op_functions(void)
  48162. {
  48163. EXPECT_DECLS;
  48164. #if defined(OPENSSL_EXTRA)
  48165. /* this makes sure wolfSSL can compile and run these no-op functions */
  48166. SSL_load_error_strings();
  48167. ENGINE_load_builtin_engines();
  48168. OpenSSL_add_all_ciphers();
  48169. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  48170. #endif
  48171. return EXPECT_RESULT();
  48172. }
  48173. static int test_wolfSSL_CRYPTO_memcmp(void)
  48174. {
  48175. EXPECT_DECLS;
  48176. #ifdef OPENSSL_EXTRA
  48177. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48178. "implementation of TLS/SSL for embedded devices to the cloud.";
  48179. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48180. "implementation of TLS/SSL for embedded devices to the cloud.";
  48181. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  48182. "implementation of TLS/SSL for embedded devices to the cloud!";
  48183. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  48184. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  48185. #endif
  48186. return EXPECT_RESULT();
  48187. }
  48188. /*----------------------------------------------------------------------------*
  48189. | wolfCrypt ASN
  48190. *----------------------------------------------------------------------------*/
  48191. static int test_wc_CreateEncryptedPKCS8Key(void)
  48192. {
  48193. EXPECT_DECLS;
  48194. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  48195. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  48196. WC_RNG rng;
  48197. byte* encKey = NULL;
  48198. word32 encKeySz = 0;
  48199. word32 decKeySz = 0;
  48200. const char password[] = "Lorem ipsum dolor sit amet";
  48201. word32 passwordSz = (word32)XSTRLEN(password);
  48202. word32 tradIdx = 0;
  48203. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48204. ExpectIntEQ(wc_InitRng(&rng), 0);
  48205. PRIVATE_KEY_UNLOCK();
  48206. /* Call with NULL for out buffer to get necessary length. */
  48207. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  48208. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  48209. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  48210. LENGTH_ONLY_E);
  48211. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  48212. DYNAMIC_TYPE_TMP_BUFFER));
  48213. /* Call with the allocated out buffer. */
  48214. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  48215. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  48216. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  48217. 0);
  48218. /* Decrypt the encrypted PKCS8 key we just made. */
  48219. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  48220. (int)passwordSz)), 0);
  48221. /* encKey now holds the decrypted key (decrypted in place). */
  48222. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  48223. /* Check that the decrypted key matches the key prior to encryption. */
  48224. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  48225. sizeof_server_key_der_2048), 0);
  48226. PRIVATE_KEY_LOCK();
  48227. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48228. wc_FreeRng(&rng);
  48229. #endif
  48230. return EXPECT_RESULT();
  48231. }
  48232. static int test_wc_GetPkcs8TraditionalOffset(void)
  48233. {
  48234. EXPECT_DECLS;
  48235. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  48236. int length;
  48237. int derSz = 0;
  48238. word32 inOutIdx;
  48239. const char* path = "./certs/server-keyPkcs8.der";
  48240. XFILE file = XBADFILE;
  48241. byte der[2048];
  48242. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  48243. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  48244. if (file != XBADFILE)
  48245. XFCLOSE(file);
  48246. /* valid case */
  48247. inOutIdx = 0;
  48248. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48249. 0);
  48250. /* inOutIdx > sz */
  48251. inOutIdx = 4000;
  48252. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48253. BAD_FUNC_ARG);
  48254. /* null input */
  48255. inOutIdx = 0;
  48256. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  48257. BAD_FUNC_ARG);
  48258. /* invalid input, fill buffer with 1's */
  48259. XMEMSET(der, 1, sizeof(der));
  48260. inOutIdx = 0;
  48261. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  48262. ASN_PARSE_E);
  48263. #endif /* NO_ASN */
  48264. return EXPECT_RESULT();
  48265. }
  48266. static int test_wc_SetSubjectRaw(void)
  48267. {
  48268. EXPECT_DECLS;
  48269. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48270. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48271. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48272. WOLFSSL_X509* x509 = NULL;
  48273. int peerCertSz;
  48274. const byte* peerCertBuf = NULL;
  48275. Cert forgedCert;
  48276. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48277. WOLFSSL_FILETYPE_ASN1));
  48278. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48279. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48280. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  48281. wolfSSL_FreeX509(x509);
  48282. #endif
  48283. return EXPECT_RESULT();
  48284. }
  48285. static int test_wc_GetSubjectRaw(void)
  48286. {
  48287. EXPECT_DECLS;
  48288. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48289. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  48290. Cert cert;
  48291. byte *subjectRaw;
  48292. ExpectIntEQ(0, wc_InitCert(&cert));
  48293. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  48294. #endif
  48295. return EXPECT_RESULT();
  48296. }
  48297. static int test_wc_SetIssuerRaw(void)
  48298. {
  48299. EXPECT_DECLS;
  48300. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48301. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48302. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48303. WOLFSSL_X509* x509 = NULL;
  48304. int peerCertSz;
  48305. const byte* peerCertBuf;
  48306. Cert forgedCert;
  48307. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48308. WOLFSSL_FILETYPE_ASN1));
  48309. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48310. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48311. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  48312. wolfSSL_FreeX509(x509);
  48313. #endif
  48314. return EXPECT_RESULT();
  48315. }
  48316. static int test_wc_SetIssueBuffer(void)
  48317. {
  48318. EXPECT_DECLS;
  48319. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48320. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  48321. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  48322. WOLFSSL_X509* x509 = NULL;
  48323. int peerCertSz;
  48324. const byte* peerCertBuf;
  48325. Cert forgedCert;
  48326. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  48327. WOLFSSL_FILETYPE_ASN1));
  48328. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  48329. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  48330. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  48331. wolfSSL_FreeX509(x509);
  48332. #endif
  48333. return EXPECT_RESULT();
  48334. }
  48335. /*
  48336. * Testing wc_SetSubjectKeyId
  48337. */
  48338. static int test_wc_SetSubjectKeyId(void)
  48339. {
  48340. EXPECT_DECLS;
  48341. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48342. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  48343. Cert cert;
  48344. const char* file = "certs/ecc-client-keyPub.pem";
  48345. ExpectIntEQ(0, wc_InitCert(&cert));
  48346. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  48347. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  48348. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  48349. #endif
  48350. return EXPECT_RESULT();
  48351. } /* END test_wc_SetSubjectKeyId */
  48352. /*
  48353. * Testing wc_SetSubject
  48354. */
  48355. static int test_wc_SetSubject(void)
  48356. {
  48357. EXPECT_DECLS;
  48358. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  48359. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  48360. Cert cert;
  48361. const char* file = "./certs/ca-ecc-cert.pem";
  48362. ExpectIntEQ(0, wc_InitCert(&cert));
  48363. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  48364. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  48365. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  48366. #endif
  48367. return EXPECT_RESULT();
  48368. } /* END test_wc_SetSubject */
  48369. static int test_CheckCertSignature(void)
  48370. {
  48371. EXPECT_DECLS;
  48372. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  48373. WOLFSSL_CERT_MANAGER* cm = NULL;
  48374. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  48375. XFILE fp = XBADFILE;
  48376. byte cert[4096];
  48377. int certSz;
  48378. #endif
  48379. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, NULL));
  48380. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  48381. ExpectIntEQ(BAD_FUNC_ARG, wc_CheckCertSignature(NULL, 0, NULL, cm));
  48382. #ifndef NO_RSA
  48383. #ifdef USE_CERT_BUFFERS_1024
  48384. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_1024,
  48385. sizeof_server_cert_der_1024, NULL, cm));
  48386. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48387. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  48388. WOLFSSL_FILETYPE_ASN1));
  48389. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_1024,
  48390. sizeof_server_cert_der_1024, NULL, cm));
  48391. #elif defined(USE_CERT_BUFFERS_2048)
  48392. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(server_cert_der_2048,
  48393. sizeof_server_cert_der_2048, NULL, cm));
  48394. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48395. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  48396. WOLFSSL_FILETYPE_ASN1));
  48397. ExpectIntEQ(0, wc_CheckCertSignature(server_cert_der_2048,
  48398. sizeof_server_cert_der_2048, NULL, cm));
  48399. #endif
  48400. #endif
  48401. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  48402. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(serv_ecc_der_256,
  48403. sizeof_serv_ecc_der_256, NULL, cm));
  48404. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  48405. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  48406. WOLFSSL_FILETYPE_ASN1));
  48407. ExpectIntEQ(0, wc_CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  48408. NULL, cm));
  48409. #endif
  48410. #if !defined(NO_FILESYSTEM)
  48411. wolfSSL_CertManagerFree(cm);
  48412. cm = NULL;
  48413. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  48414. #ifndef NO_RSA
  48415. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  48416. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  48417. if (fp != XBADFILE) {
  48418. XFCLOSE(fp);
  48419. fp = XBADFILE;
  48420. }
  48421. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48422. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  48423. "./certs/ca-cert.pem", NULL));
  48424. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48425. #endif
  48426. #ifdef HAVE_ECC
  48427. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  48428. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  48429. if (fp != XBADFILE) {
  48430. XFCLOSE(fp);
  48431. fp = XBADFILE;
  48432. }
  48433. ExpectIntEQ(ASN_NO_SIGNER_E, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48434. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  48435. "./certs/ca-ecc-cert.pem", NULL));
  48436. ExpectIntEQ(0, wc_CheckCertSignature(cert, certSz, NULL, cm));
  48437. #endif
  48438. #endif
  48439. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  48440. (void)fp;
  48441. (void)cert;
  48442. (void)certSz;
  48443. #endif
  48444. wolfSSL_CertManagerFree(cm);
  48445. #endif
  48446. return EXPECT_RESULT();
  48447. }
  48448. static int test_wc_ParseCert(void)
  48449. {
  48450. EXPECT_DECLS;
  48451. #if !defined(NO_CERTS) && !defined(NO_RSA)
  48452. DecodedCert decodedCert;
  48453. const byte* rawCert = client_cert_der_2048;
  48454. const int rawCertSize = sizeof_client_cert_der_2048;
  48455. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  48456. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48457. #ifndef IGNORE_NAME_CONSTRAINTS
  48458. /* check that the subjects emailAddress was not put in the alt name list */
  48459. ExpectNotNull(decodedCert.subjectEmail);
  48460. ExpectNull(decodedCert.altEmailNames);
  48461. #endif
  48462. wc_FreeDecodedCert(&decodedCert);
  48463. #endif
  48464. return EXPECT_RESULT();
  48465. }
  48466. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  48467. * the API safely errors out on badly-formed ASN input.
  48468. * NOTE: Test not compatible with released FIPS implementations!
  48469. */
  48470. static int test_wc_ParseCert_Error(void)
  48471. {
  48472. EXPECT_DECLS;
  48473. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  48474. (!defined(HAVE_FIPS) || \
  48475. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  48476. DecodedCert decodedCert;
  48477. int i;
  48478. /* Certificate data */
  48479. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  48480. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  48481. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  48482. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  48483. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  48484. /* Test data */
  48485. const struct testStruct {
  48486. const byte* c;
  48487. const int cSz;
  48488. const int expRet;
  48489. } t[] = {
  48490. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  48491. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  48492. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  48493. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  48494. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  48495. };
  48496. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  48497. for (i = 0; i < tSz; i++) {
  48498. WOLFSSL_MSG_EX("i == %d", i);
  48499. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  48500. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  48501. wc_FreeDecodedCert(&decodedCert);
  48502. }
  48503. #endif
  48504. return EXPECT_RESULT();
  48505. }
  48506. static int test_MakeCertWithPathLen(void)
  48507. {
  48508. EXPECT_DECLS;
  48509. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  48510. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  48511. const byte expectedPathLen = 7;
  48512. Cert cert;
  48513. DecodedCert decodedCert;
  48514. byte der[FOURK_BUF];
  48515. int derSize = 0;
  48516. WC_RNG rng;
  48517. ecc_key key;
  48518. int ret;
  48519. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48520. XMEMSET(&key, 0, sizeof(ecc_key));
  48521. XMEMSET(&cert, 0, sizeof(Cert));
  48522. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48523. ExpectIntEQ(wc_InitRng(&rng), 0);
  48524. ExpectIntEQ(wc_ecc_init(&key), 0);
  48525. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48526. ExpectIntEQ(wc_InitCert(&cert), 0);
  48527. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48528. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48529. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48530. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48531. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48532. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48533. CTC_NAME_SIZE);
  48534. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48535. CTC_NAME_SIZE);
  48536. cert.selfSigned = 1;
  48537. cert.isCA = 1;
  48538. cert.pathLen = expectedPathLen;
  48539. cert.pathLenSet = 1;
  48540. cert.sigType = CTC_SHA256wECDSA;
  48541. #ifdef WOLFSSL_CERT_EXT
  48542. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  48543. #endif
  48544. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48545. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48546. FOURK_BUF, NULL, &key, &rng), 0);
  48547. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  48548. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48549. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  48550. wc_FreeDecodedCert(&decodedCert);
  48551. ret = wc_ecc_free(&key);
  48552. ExpectIntEQ(ret, 0);
  48553. ret = wc_FreeRng(&rng);
  48554. ExpectIntEQ(ret, 0);
  48555. #endif
  48556. return EXPECT_RESULT();
  48557. }
  48558. static int test_MakeCertWith0Ser(void)
  48559. {
  48560. EXPECT_DECLS;
  48561. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  48562. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC) && \
  48563. defined(WOLFSSL_ASN_TEMPLATE)
  48564. Cert cert;
  48565. DecodedCert decodedCert;
  48566. byte der[FOURK_BUF];
  48567. int derSize = 0;
  48568. WC_RNG rng;
  48569. ecc_key key;
  48570. int ret;
  48571. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48572. XMEMSET(&key, 0, sizeof(ecc_key));
  48573. XMEMSET(&cert, 0, sizeof(Cert));
  48574. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48575. ExpectIntEQ(wc_InitRng(&rng), 0);
  48576. ExpectIntEQ(wc_ecc_init(&key), 0);
  48577. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48578. ExpectIntEQ(wc_InitCert(&cert), 0);
  48579. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48580. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48581. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48582. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48583. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48584. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48585. CTC_NAME_SIZE);
  48586. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48587. CTC_NAME_SIZE);
  48588. cert.selfSigned = 1;
  48589. cert.isCA = 1;
  48590. cert.sigType = CTC_SHA256wECDSA;
  48591. #ifdef WOLFSSL_CERT_EXT
  48592. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  48593. #endif
  48594. /* set serial number to 0 */
  48595. cert.serialSz = 1;
  48596. cert.serial[0] = 0;
  48597. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48598. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48599. FOURK_BUF, NULL, &key, &rng), 0);
  48600. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  48601. #if !defined(WOLFSSL_NO_ASN_STRICT) && !defined(WOLFSSL_PYTHON)
  48602. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL),
  48603. ASN_PARSE_E);
  48604. #else
  48605. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48606. #endif
  48607. wc_FreeDecodedCert(&decodedCert);
  48608. ret = wc_ecc_free(&key);
  48609. ExpectIntEQ(ret, 0);
  48610. ret = wc_FreeRng(&rng);
  48611. ExpectIntEQ(ret, 0);
  48612. #endif
  48613. return EXPECT_RESULT();
  48614. }
  48615. static int test_MakeCertWithCaFalse(void)
  48616. {
  48617. EXPECT_DECLS;
  48618. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  48619. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  48620. const byte expectedIsCa = 0;
  48621. Cert cert;
  48622. DecodedCert decodedCert;
  48623. byte der[FOURK_BUF];
  48624. int derSize = 0;
  48625. WC_RNG rng;
  48626. ecc_key key;
  48627. int ret;
  48628. XMEMSET(&rng, 0, sizeof(WC_RNG));
  48629. XMEMSET(&key, 0, sizeof(ecc_key));
  48630. XMEMSET(&cert, 0, sizeof(Cert));
  48631. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  48632. ExpectIntEQ(wc_InitRng(&rng), 0);
  48633. ExpectIntEQ(wc_ecc_init(&key), 0);
  48634. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  48635. ExpectIntEQ(wc_InitCert(&cert), 0);
  48636. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  48637. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  48638. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  48639. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  48640. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  48641. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  48642. CTC_NAME_SIZE);
  48643. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  48644. CTC_NAME_SIZE);
  48645. cert.selfSigned = 1;
  48646. cert.isCA = expectedIsCa;
  48647. cert.isCaSet = 1;
  48648. cert.sigType = CTC_SHA256wECDSA;
  48649. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  48650. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  48651. FOURK_BUF, NULL, &key, &rng), 0);
  48652. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  48653. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  48654. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  48655. wc_FreeDecodedCert(&decodedCert);
  48656. ret = wc_ecc_free(&key);
  48657. ExpectIntEQ(ret, 0);
  48658. ret = wc_FreeRng(&rng);
  48659. ExpectIntEQ(ret, 0);
  48660. #endif
  48661. return EXPECT_RESULT();
  48662. }
  48663. /*----------------------------------------------------------------------------*
  48664. | wolfCrypt ECC
  48665. *----------------------------------------------------------------------------*/
  48666. static int test_wc_ecc_get_curve_size_from_name(void)
  48667. {
  48668. EXPECT_DECLS;
  48669. #ifdef HAVE_ECC
  48670. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48671. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  48672. #endif
  48673. /* invalid case */
  48674. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  48675. /* NULL input */
  48676. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  48677. #endif /* HAVE_ECC */
  48678. return EXPECT_RESULT();
  48679. }
  48680. static int test_wc_ecc_get_curve_id_from_name(void)
  48681. {
  48682. EXPECT_DECLS;
  48683. #ifdef HAVE_ECC
  48684. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48685. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  48686. ECC_SECP256R1);
  48687. #endif
  48688. /* invalid case */
  48689. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  48690. /* NULL input */
  48691. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  48692. #endif /* HAVE_ECC */
  48693. return EXPECT_RESULT();
  48694. }
  48695. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  48696. !defined(HAVE_SELFTEST) && \
  48697. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  48698. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  48699. {
  48700. EXPECT_DECLS;
  48701. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48702. ecc_key* key;
  48703. const ecc_set_type* params = NULL;
  48704. int ret;
  48705. #endif
  48706. WOLFSSL_EC_KEY *ecKey = NULL;
  48707. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48708. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  48709. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  48710. if (EXPECT_SUCCESS()) {
  48711. ret = EC_KEY_generate_key(ecKey);
  48712. } else
  48713. ret = 0;
  48714. if (ret == 1) {
  48715. /* normal test */
  48716. key = (ecc_key*)ecKey->internal;
  48717. if (key != NULL) {
  48718. params = key->dp;
  48719. }
  48720. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  48721. ECC_SECP256R1);
  48722. }
  48723. #endif
  48724. /* invalid case, NULL input*/
  48725. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  48726. wolfSSL_EC_KEY_free(ecKey);
  48727. return EXPECT_RESULT();
  48728. }
  48729. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48730. static int test_wc_ecc_get_curve_id_from_params(void)
  48731. {
  48732. EXPECT_DECLS;
  48733. #ifdef HAVE_ECC
  48734. const byte prime[] =
  48735. {
  48736. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48737. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48738. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48739. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  48740. };
  48741. const byte primeInvalid[] =
  48742. {
  48743. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48744. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48745. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48746. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  48747. };
  48748. const byte Af[] =
  48749. {
  48750. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  48751. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  48752. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  48753. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  48754. };
  48755. const byte Bf[] =
  48756. {
  48757. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  48758. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  48759. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  48760. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  48761. };
  48762. const byte order[] =
  48763. {
  48764. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  48765. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48766. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  48767. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  48768. };
  48769. const byte Gx[] =
  48770. {
  48771. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  48772. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  48773. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  48774. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  48775. };
  48776. const byte Gy[] =
  48777. {
  48778. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  48779. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  48780. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  48781. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  48782. };
  48783. int cofactor = 1;
  48784. int fieldSize = 256;
  48785. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  48786. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  48787. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  48788. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  48789. ECC_SECP256R1);
  48790. #endif
  48791. /* invalid case, fieldSize = 0 */
  48792. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  48793. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48794. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  48795. /* invalid case, NULL prime */
  48796. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  48797. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48798. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  48799. /* invalid case, invalid prime */
  48800. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  48801. primeInvalid, sizeof(primeInvalid),
  48802. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  48803. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  48804. #endif
  48805. return EXPECT_RESULT();
  48806. }
  48807. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  48808. {
  48809. EXPECT_DECLS;
  48810. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  48811. WOLFSSL_RSA* rsa = NULL;
  48812. WOLFSSL_EVP_PKEY* pkey = NULL;
  48813. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  48814. const char* in = "What is easy to do is easy not to do.";
  48815. size_t inlen = XSTRLEN(in);
  48816. size_t outEncLen = 0;
  48817. byte* outEnc = NULL;
  48818. byte* outDec = NULL;
  48819. size_t outDecLen = 0;
  48820. size_t rsaKeySz = 2048/8; /* Bytes */
  48821. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48822. byte* inTmp = NULL;
  48823. byte* outEncTmp = NULL;
  48824. byte* outDecTmp = NULL;
  48825. #endif
  48826. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48827. DYNAMIC_TYPE_TMP_BUFFER));
  48828. if (outEnc != NULL) {
  48829. XMEMSET(outEnc, 0, rsaKeySz);
  48830. }
  48831. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48832. DYNAMIC_TYPE_TMP_BUFFER));
  48833. if (outDec != NULL) {
  48834. XMEMSET(outDec, 0, rsaKeySz);
  48835. }
  48836. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48837. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  48838. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  48839. if (EXPECT_FAIL()) {
  48840. RSA_free(rsa);
  48841. }
  48842. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  48843. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  48844. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  48845. WOLFSSL_SUCCESS);
  48846. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  48847. since ctx uses it.*/
  48848. ExpectIntEQ(pkey->ref.count, 2);
  48849. EVP_PKEY_free(pkey);
  48850. ExpectIntEQ(pkey->ref.count, 1);
  48851. /* Encrypt data */
  48852. /* Check that we can get the required output buffer length by passing in a
  48853. * NULL output buffer. */
  48854. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  48855. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  48856. ExpectIntEQ(rsaKeySz, outEncLen);
  48857. /* Now do the actual encryption. */
  48858. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  48859. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  48860. /* Decrypt data */
  48861. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  48862. /* Check that we can get the required output buffer length by passing in a
  48863. * NULL output buffer. */
  48864. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  48865. WOLFSSL_SUCCESS);
  48866. ExpectIntEQ(rsaKeySz, outDecLen);
  48867. /* Now do the actual decryption. */
  48868. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  48869. WOLFSSL_SUCCESS);
  48870. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  48871. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48872. /* The input length must be the same size as the RSA key.*/
  48873. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48874. DYNAMIC_TYPE_TMP_BUFFER));
  48875. if (inTmp != NULL) {
  48876. XMEMSET(inTmp, 9, rsaKeySz);
  48877. }
  48878. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48879. DYNAMIC_TYPE_TMP_BUFFER));
  48880. if (outEncTmp != NULL) {
  48881. XMEMSET(outEncTmp, 0, rsaKeySz);
  48882. }
  48883. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  48884. DYNAMIC_TYPE_TMP_BUFFER));
  48885. if (outDecTmp != NULL) {
  48886. XMEMSET(outDecTmp, 0, rsaKeySz);
  48887. }
  48888. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  48889. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  48890. WOLFSSL_SUCCESS);
  48891. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  48892. WOLFSSL_SUCCESS);
  48893. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  48894. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  48895. outEncLen), WOLFSSL_SUCCESS);
  48896. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  48897. #endif
  48898. EVP_PKEY_CTX_free(ctx);
  48899. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48900. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48901. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  48902. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48903. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48904. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  48905. #endif
  48906. #endif
  48907. return EXPECT_RESULT();
  48908. }
  48909. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48910. !defined(HAVE_SELFTEST)
  48911. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48912. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48913. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48914. #endif
  48915. #endif
  48916. #endif
  48917. #if defined(OPENSSL_EXTRA)
  48918. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  48919. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48920. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48921. #endif
  48922. #endif
  48923. #endif
  48924. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48925. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48926. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48927. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48928. #endif
  48929. #endif
  48930. #endif
  48931. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  48932. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  48933. {
  48934. EXPECT_DECLS;
  48935. #if defined(OPENSSL_EXTRA)
  48936. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48937. !defined(HAVE_SELFTEST)
  48938. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48939. WOLFSSL_RSA* rsa = NULL;
  48940. #endif
  48941. #endif
  48942. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  48943. WOLFSSL_DSA* dsa = NULL;
  48944. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  48945. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48946. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48947. WOLFSSL_EC_KEY* ecKey = NULL;
  48948. #endif
  48949. #endif
  48950. WOLFSSL_EVP_PKEY* pkey = NULL;
  48951. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  48952. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  48953. const char* in = "What is easy to do is easy not to do.";
  48954. size_t inlen = XSTRLEN(in);
  48955. byte hash[SHA256_DIGEST_LENGTH] = {0};
  48956. byte zero[SHA256_DIGEST_LENGTH] = {0};
  48957. SHA256_CTX c;
  48958. byte* sig = NULL;
  48959. byte* sigVerify = NULL;
  48960. size_t siglen;
  48961. size_t siglenOnlyLen;
  48962. size_t keySz = 2048/8; /* Bytes */
  48963. ExpectNotNull(sig =
  48964. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  48965. ExpectNotNull(sigVerify =
  48966. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  48967. siglen = keySz;
  48968. ExpectNotNull(XMEMSET(sig, 0, keySz));
  48969. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  48970. /* Generate hash */
  48971. SHA256_Init(&c);
  48972. SHA256_Update(&c, in, inlen);
  48973. SHA256_Final(hash, &c);
  48974. #ifdef WOLFSSL_SMALL_STACK_CACHE
  48975. /* workaround for small stack cache case */
  48976. wc_Sha256Free((wc_Sha256*)&c);
  48977. #endif
  48978. /* Generate key */
  48979. ExpectNotNull(pkey = EVP_PKEY_new());
  48980. switch (keyType) {
  48981. case EVP_PKEY_RSA:
  48982. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  48983. !defined(HAVE_SELFTEST)
  48984. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  48985. {
  48986. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  48987. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  48988. }
  48989. #endif
  48990. #endif
  48991. break;
  48992. case EVP_PKEY_DSA:
  48993. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  48994. ExpectNotNull(dsa = DSA_new());
  48995. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  48996. NULL, 0, NULL, NULL, NULL), 1);
  48997. ExpectIntEQ(DSA_generate_key(dsa), 1);
  48998. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  48999. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  49000. break;
  49001. case EVP_PKEY_EC:
  49002. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49003. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49004. {
  49005. ExpectNotNull(ecKey = EC_KEY_new());
  49006. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  49007. ExpectIntEQ(
  49008. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  49009. if (EXPECT_FAIL()) {
  49010. EC_KEY_free(ecKey);
  49011. }
  49012. }
  49013. #endif
  49014. #endif
  49015. break;
  49016. }
  49017. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  49018. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49019. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49020. !defined(HAVE_SELFTEST)
  49021. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49022. if (keyType == EVP_PKEY_RSA)
  49023. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  49024. WOLFSSL_SUCCESS);
  49025. #endif
  49026. #endif
  49027. /* Check returning only length */
  49028. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  49029. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  49030. ExpectIntGT(siglenOnlyLen, 0);
  49031. /* Sign data */
  49032. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  49033. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  49034. ExpectIntGE(siglenOnlyLen, siglen);
  49035. /* Verify signature */
  49036. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  49037. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49038. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49039. !defined(HAVE_SELFTEST)
  49040. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49041. if (keyType == EVP_PKEY_RSA)
  49042. ExpectIntEQ(
  49043. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  49044. WOLFSSL_SUCCESS);
  49045. #endif
  49046. #endif
  49047. ExpectIntEQ(EVP_PKEY_verify(
  49048. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  49049. WOLFSSL_SUCCESS);
  49050. ExpectIntEQ(EVP_PKEY_verify(
  49051. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  49052. WOLFSSL_FAILURE);
  49053. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49054. !defined(HAVE_SELFTEST)
  49055. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49056. if (keyType == EVP_PKEY_RSA) {
  49057. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  49058. /* Try RSA sign/verify with no padding. */
  49059. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49060. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  49061. WOLFSSL_SUCCESS);
  49062. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  49063. siglen), WOLFSSL_SUCCESS);
  49064. ExpectIntGE(siglenOnlyLen, siglen);
  49065. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49066. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49067. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  49068. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  49069. siglen), WOLFSSL_SUCCESS);
  49070. #endif
  49071. /* Wrong padding schemes. */
  49072. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49073. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  49074. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  49075. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  49076. siglen), WOLFSSL_SUCCESS);
  49077. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  49078. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49079. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  49080. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  49081. siglen), WOLFSSL_SUCCESS);
  49082. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  49083. WOLFSSL_SUCCESS);
  49084. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  49085. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  49086. }
  49087. #endif
  49088. #endif
  49089. /* error cases */
  49090. siglen = keySz; /* Reset because sig size may vary slightly */
  49091. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  49092. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  49093. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  49094. WOLFSSL_SUCCESS);
  49095. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  49096. WOLFSSL_SUCCESS);
  49097. EVP_PKEY_free(pkey);
  49098. pkey = NULL;
  49099. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  49100. DSA_free(dsa);
  49101. dsa = NULL;
  49102. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  49103. EVP_PKEY_CTX_free(ctx_verify);
  49104. ctx_verify = NULL;
  49105. EVP_PKEY_CTX_free(ctx);
  49106. ctx = NULL;
  49107. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49108. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  49109. #endif /* OPENSSL_EXTRA */
  49110. return EXPECT_RESULT();
  49111. }
  49112. #endif
  49113. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  49114. {
  49115. EXPECT_DECLS;
  49116. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  49117. !defined(HAVE_SELFTEST)
  49118. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49119. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  49120. #endif
  49121. #endif
  49122. return EXPECT_RESULT();
  49123. }
  49124. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  49125. {
  49126. EXPECT_DECLS;
  49127. #if defined(OPENSSL_EXTRA)
  49128. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  49129. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  49130. #endif
  49131. #endif
  49132. return EXPECT_RESULT();
  49133. }
  49134. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  49135. {
  49136. EXPECT_DECLS;
  49137. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49138. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49139. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  49140. #endif
  49141. #endif
  49142. return EXPECT_RESULT();
  49143. }
  49144. static int test_EVP_PKEY_rsa(void)
  49145. {
  49146. EXPECT_DECLS;
  49147. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  49148. WOLFSSL_RSA* rsa = NULL;
  49149. WOLFSSL_EVP_PKEY* pkey = NULL;
  49150. ExpectNotNull(rsa = wolfSSL_RSA_new());
  49151. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  49152. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  49153. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  49154. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  49155. if (EXPECT_FAIL()) {
  49156. wolfSSL_RSA_free(rsa);
  49157. }
  49158. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  49159. wolfSSL_EVP_PKEY_free(pkey);
  49160. #endif
  49161. return EXPECT_RESULT();
  49162. }
  49163. static int test_EVP_PKEY_ec(void)
  49164. {
  49165. EXPECT_DECLS;
  49166. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49167. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  49168. WOLFSSL_EC_KEY* ecKey = NULL;
  49169. WOLFSSL_EVP_PKEY* pkey = NULL;
  49170. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  49171. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  49172. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  49173. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  49174. /* Should fail since ecKey is empty */
  49175. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  49176. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  49177. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  49178. if (EXPECT_FAIL()) {
  49179. wolfSSL_EC_KEY_free(ecKey);
  49180. }
  49181. wolfSSL_EVP_PKEY_free(pkey);
  49182. #endif
  49183. #endif
  49184. return EXPECT_RESULT();
  49185. }
  49186. static int test_EVP_PKEY_cmp(void)
  49187. {
  49188. EXPECT_DECLS;
  49189. #if defined(OPENSSL_EXTRA)
  49190. EVP_PKEY *a = NULL;
  49191. EVP_PKEY *b = NULL;
  49192. const unsigned char *in;
  49193. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  49194. in = client_key_der_2048;
  49195. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49196. &in, (long)sizeof_client_key_der_2048));
  49197. in = client_key_der_2048;
  49198. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49199. &in, (long)sizeof_client_key_der_2048));
  49200. /* Test success case RSA */
  49201. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49202. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  49203. #else
  49204. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49205. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49206. EVP_PKEY_free(b);
  49207. b = NULL;
  49208. EVP_PKEY_free(a);
  49209. a = NULL;
  49210. #endif
  49211. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  49212. in = ecc_clikey_der_256;
  49213. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49214. &in, (long)sizeof_ecc_clikey_der_256));
  49215. in = ecc_clikey_der_256;
  49216. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49217. &in, (long)sizeof_ecc_clikey_der_256));
  49218. /* Test success case ECC */
  49219. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49220. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  49221. #else
  49222. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49223. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49224. EVP_PKEY_free(b);
  49225. b = NULL;
  49226. EVP_PKEY_free(a);
  49227. a = NULL;
  49228. #endif
  49229. /* Test failure cases */
  49230. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  49231. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  49232. in = client_key_der_2048;
  49233. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  49234. &in, (long)sizeof_client_key_der_2048));
  49235. in = ecc_clikey_der_256;
  49236. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  49237. &in, (long)sizeof_ecc_clikey_der_256));
  49238. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49239. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  49240. #else
  49241. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  49242. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  49243. EVP_PKEY_free(b);
  49244. b = NULL;
  49245. EVP_PKEY_free(a);
  49246. a = NULL;
  49247. #endif
  49248. /* invalid or empty failure cases */
  49249. a = EVP_PKEY_new();
  49250. b = EVP_PKEY_new();
  49251. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49252. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  49253. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  49254. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  49255. #ifdef NO_RSA
  49256. /* Type check will fail since RSA is the default EVP key type */
  49257. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  49258. #else
  49259. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  49260. #endif
  49261. #else
  49262. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  49263. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  49264. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  49265. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  49266. #endif
  49267. EVP_PKEY_free(b);
  49268. EVP_PKEY_free(a);
  49269. (void)in;
  49270. #endif
  49271. return EXPECT_RESULT();
  49272. }
  49273. static int test_ERR_load_crypto_strings(void)
  49274. {
  49275. #if defined(OPENSSL_ALL)
  49276. ERR_load_crypto_strings();
  49277. return TEST_SUCCESS;
  49278. #else
  49279. return TEST_SKIPPED;
  49280. #endif
  49281. }
  49282. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49283. static void free_x509(X509* x)
  49284. {
  49285. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  49286. }
  49287. #endif
  49288. static int test_sk_X509(void)
  49289. {
  49290. EXPECT_DECLS;
  49291. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  49292. {
  49293. STACK_OF(X509)* s = NULL;
  49294. ExpectNotNull(s = sk_X509_new_null());
  49295. ExpectIntEQ(sk_X509_num(s), 0);
  49296. sk_X509_pop_free(s, NULL);
  49297. ExpectNotNull(s = sk_X509_new_null());
  49298. ExpectIntEQ(sk_X509_num(s), 0);
  49299. sk_X509_pop_free(s, NULL);
  49300. ExpectNotNull(s = sk_X509_new_null());
  49301. sk_X509_push(s, (X509*)1);
  49302. ExpectIntEQ(sk_X509_num(s), 1);
  49303. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  49304. sk_X509_push(s, (X509*)2);
  49305. ExpectIntEQ(sk_X509_num(s), 2);
  49306. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  49307. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  49308. sk_X509_push(s, (X509*)2);
  49309. sk_X509_pop_free(s, free_x509);
  49310. }
  49311. {
  49312. /* Push a list of 10 X509s onto stack, then verify that
  49313. * value(), push(), shift(), and pop() behave as expected. */
  49314. STACK_OF(X509)* s = NULL;
  49315. X509* xList[10];
  49316. int i = 0;
  49317. const int len = (sizeof(xList) / sizeof(xList[0]));
  49318. for (i = 0; i < len; ++i) {
  49319. xList[i] = NULL;
  49320. ExpectNotNull(xList[i] = X509_new());
  49321. }
  49322. /* test push, pop, and free */
  49323. ExpectNotNull(s = sk_X509_new_null());
  49324. for (i = 0; i < len; ++i) {
  49325. sk_X509_push(s, xList[i]);
  49326. ExpectIntEQ(sk_X509_num(s), i + 1);
  49327. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  49328. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  49329. }
  49330. /* pop returns and removes last pushed on stack, which is index 0
  49331. * in sk_x509_value */
  49332. for (i = 0; i < len; ++i) {
  49333. X509 * x = sk_X509_value(s, 0);
  49334. X509 * y = sk_X509_pop(s);
  49335. X509 * z = xList[len - 1 - i];
  49336. ExpectIntEQ((x == y), 1);
  49337. ExpectIntEQ((x == z), 1);
  49338. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  49339. }
  49340. sk_free(s);
  49341. s = NULL;
  49342. /* test push, shift, and free */
  49343. ExpectNotNull(s = sk_X509_new_null());
  49344. for (i = 0; i < len; ++i) {
  49345. sk_X509_push(s, xList[i]);
  49346. ExpectIntEQ(sk_X509_num(s), i + 1);
  49347. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  49348. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  49349. }
  49350. /* shift returns and removes first pushed on stack, which is index i
  49351. * in sk_x509_value() */
  49352. for (i = 0; i < len; ++i) {
  49353. X509 * x = sk_X509_value(s, len - 1 - i);
  49354. X509 * y = sk_X509_shift(s);
  49355. X509 * z = xList[i];
  49356. ExpectIntEQ((x == y), 1);
  49357. ExpectIntEQ((x == z), 1);
  49358. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  49359. }
  49360. sk_free(s);
  49361. for (i = 0; i < len; ++i)
  49362. X509_free(xList[i]);
  49363. }
  49364. #endif
  49365. return EXPECT_RESULT();
  49366. }
  49367. static int test_sk_X509_CRL(void)
  49368. {
  49369. EXPECT_DECLS;
  49370. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  49371. X509_CRL* crl = NULL;
  49372. XFILE fp = XBADFILE;
  49373. STACK_OF(X509_CRL)* s = NULL;
  49374. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  49375. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49376. NULL, NULL));
  49377. if (fp != XBADFILE)
  49378. XFCLOSE(fp);
  49379. ExpectNotNull(s = sk_X509_CRL_new());
  49380. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  49381. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  49382. if (EXPECT_FAIL()) {
  49383. X509_CRL_free(crl);
  49384. }
  49385. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  49386. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  49387. sk_X509_CRL_free(s);
  49388. #endif
  49389. return EXPECT_RESULT();
  49390. }
  49391. static int test_X509_get_signature_nid(void)
  49392. {
  49393. EXPECT_DECLS;
  49394. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49395. X509* x509 = NULL;
  49396. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  49397. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  49398. SSL_FILETYPE_PEM));
  49399. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  49400. X509_free(x509);
  49401. #endif
  49402. return EXPECT_RESULT();
  49403. }
  49404. static int test_X509_REQ(void)
  49405. {
  49406. EXPECT_DECLS;
  49407. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  49408. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  49409. X509_NAME* name = NULL;
  49410. #ifndef NO_RSA
  49411. X509_NAME* subject = NULL;
  49412. #endif
  49413. #if !defined(NO_RSA) || defined(HAVE_ECC)
  49414. X509_REQ* req = NULL;
  49415. EVP_PKEY* priv = NULL;
  49416. EVP_PKEY* pub = NULL;
  49417. unsigned char* der = NULL;
  49418. int len;
  49419. #endif
  49420. #ifndef NO_RSA
  49421. EVP_MD_CTX *mctx = NULL;
  49422. EVP_PKEY_CTX *pkctx = NULL;
  49423. #ifdef USE_CERT_BUFFERS_1024
  49424. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  49425. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  49426. #elif defined(USE_CERT_BUFFERS_2048)
  49427. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  49428. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  49429. #endif
  49430. #endif
  49431. #ifdef HAVE_ECC
  49432. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  49433. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  49434. #endif
  49435. ExpectNotNull(name = X509_NAME_new());
  49436. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  49437. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  49438. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  49439. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  49440. #ifndef NO_RSA
  49441. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  49442. (long)sizeof_client_key_der_2048));
  49443. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  49444. (long)sizeof_client_keypub_der_2048));
  49445. ExpectNotNull(req = X509_REQ_new());
  49446. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  49447. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  49448. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  49449. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  49450. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  49451. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49452. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  49453. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  49454. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  49455. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49456. len = i2d_X509_REQ(req, &der);
  49457. DEBUG_WRITE_DER(der, len, "req.der");
  49458. #ifdef USE_CERT_BUFFERS_1024
  49459. ExpectIntEQ(len, 381);
  49460. #else
  49461. ExpectIntEQ(len, 643);
  49462. #endif
  49463. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49464. der = NULL;
  49465. mctx = EVP_MD_CTX_new();
  49466. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  49467. WOLFSSL_SUCCESS);
  49468. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  49469. EVP_MD_CTX_free(mctx);
  49470. mctx = NULL;
  49471. X509_REQ_free(NULL);
  49472. X509_REQ_free(req);
  49473. req = NULL;
  49474. /* Test getting the subject from a newly created X509_REQ */
  49475. ExpectNotNull(req = X509_REQ_new());
  49476. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  49477. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  49478. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  49479. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  49480. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  49481. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  49482. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  49483. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  49484. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  49485. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  49486. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  49487. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  49488. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  49489. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49490. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49491. len = i2d_X509_REQ(req, &der);
  49492. DEBUG_WRITE_DER(der, len, "req2.der");
  49493. #ifdef USE_CERT_BUFFERS_1024
  49494. ExpectIntEQ(len, 435);
  49495. #else
  49496. ExpectIntEQ(len, 696);
  49497. #endif
  49498. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49499. der = NULL;
  49500. EVP_PKEY_free(pub);
  49501. pub = NULL;
  49502. EVP_PKEY_free(priv);
  49503. priv = NULL;
  49504. X509_REQ_free(req);
  49505. req = NULL;
  49506. #endif
  49507. #ifdef HAVE_ECC
  49508. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  49509. sizeof_ecc_clikey_der_256));
  49510. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  49511. sizeof_ecc_clikeypub_der_256));
  49512. ExpectNotNull(req = X509_REQ_new());
  49513. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  49514. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  49515. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  49516. /* Signature is random and may be shorter or longer. */
  49517. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  49518. ExpectIntLE(len, 253);
  49519. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  49520. X509_REQ_free(req);
  49521. EVP_PKEY_free(pub);
  49522. EVP_PKEY_free(priv);
  49523. #ifdef FP_ECC
  49524. wc_ecc_fp_free();
  49525. #endif
  49526. #endif /* HAVE_ECC */
  49527. X509_NAME_free(name);
  49528. #endif
  49529. return EXPECT_RESULT();
  49530. }
  49531. static int test_wolfssl_PKCS7(void)
  49532. {
  49533. EXPECT_DECLS;
  49534. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  49535. !defined(NO_RSA)
  49536. PKCS7* pkcs7 = NULL;
  49537. byte data[FOURK_BUF];
  49538. word32 len = sizeof(data);
  49539. const byte* p = data;
  49540. byte content[] = "Test data to encode.";
  49541. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  49542. BIO* bio = NULL;
  49543. byte key[sizeof(client_key_der_2048)];
  49544. word32 keySz = (word32)sizeof(key);
  49545. byte* out = NULL;
  49546. #endif
  49547. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  49548. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  49549. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  49550. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  49551. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49552. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  49553. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  49554. PKCS7_free(pkcs7);
  49555. pkcs7 = NULL;
  49556. /* fail case, without PKCS7_NOVERIFY */
  49557. p = data;
  49558. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49559. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  49560. 0), WOLFSSL_FAILURE);
  49561. PKCS7_free(pkcs7);
  49562. pkcs7 = NULL;
  49563. /* success case, with PKCS7_NOVERIFY */
  49564. p = data;
  49565. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  49566. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  49567. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  49568. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  49569. /* test i2d */
  49570. XMEMCPY(key, client_key_der_2048, keySz);
  49571. if (pkcs7 != NULL) {
  49572. pkcs7->privateKey = key;
  49573. pkcs7->privateKeySz = (word32)sizeof(key);
  49574. pkcs7->encryptOID = RSAk;
  49575. #ifdef NO_SHA
  49576. pkcs7->hashOID = SHA256h;
  49577. #else
  49578. pkcs7->hashOID = SHAh;
  49579. #endif
  49580. }
  49581. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49582. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  49583. #ifndef NO_ASN_TIME
  49584. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  49585. #else
  49586. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  49587. #endif
  49588. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49589. BIO_free(bio);
  49590. #endif
  49591. PKCS7_free(NULL);
  49592. PKCS7_free(pkcs7);
  49593. #endif
  49594. return EXPECT_RESULT();
  49595. }
  49596. static int test_wolfSSL_PKCS7_sign(void)
  49597. {
  49598. EXPECT_DECLS;
  49599. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  49600. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  49601. PKCS7* p7 = NULL;
  49602. PKCS7* p7Ver = NULL;
  49603. byte* out = NULL;
  49604. byte* tmpPtr = NULL;
  49605. int outLen = 0;
  49606. int flags = 0;
  49607. byte data[] = "Test data to encode.";
  49608. const char* cert = "./certs/server-cert.pem";
  49609. const char* key = "./certs/server-key.pem";
  49610. const char* ca = "./certs/ca-cert.pem";
  49611. WOLFSSL_BIO* certBio = NULL;
  49612. WOLFSSL_BIO* keyBio = NULL;
  49613. WOLFSSL_BIO* caBio = NULL;
  49614. WOLFSSL_BIO* inBio = NULL;
  49615. X509* signCert = NULL;
  49616. EVP_PKEY* signKey = NULL;
  49617. X509* caCert = NULL;
  49618. X509_STORE* store = NULL;
  49619. #ifndef NO_PKCS7_STREAM
  49620. int z;
  49621. int ret;
  49622. #endif /* !NO_PKCS7_STREAM */
  49623. /* read signer cert/key into BIO */
  49624. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  49625. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  49626. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  49627. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  49628. /* read CA cert into store (for verify) */
  49629. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  49630. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  49631. ExpectNotNull(store = X509_STORE_new());
  49632. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  49633. /* data to be signed into BIO */
  49634. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49635. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49636. /* PKCS7_sign, bad args: signer NULL */
  49637. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  49638. /* PKCS7_sign, bad args: signer key NULL */
  49639. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  49640. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  49641. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  49642. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  49643. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  49644. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  49645. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  49646. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  49647. {
  49648. flags = PKCS7_BINARY;
  49649. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49650. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49651. /* verify with d2i_PKCS7 */
  49652. tmpPtr = out;
  49653. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49654. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  49655. PKCS7_free(p7Ver);
  49656. p7Ver = NULL;
  49657. /* verify with wc_PKCS7_VerifySignedData */
  49658. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49659. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  49660. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49661. #ifndef NO_PKCS7_STREAM
  49662. /* verify with wc_PKCS7_VerifySignedData streaming */
  49663. wc_PKCS7_Free(p7Ver);
  49664. p7Ver = NULL;
  49665. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49666. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  49667. /* test for streaming */
  49668. ret = -1;
  49669. for (z = 0; z < outLen && ret != 0; z++) {
  49670. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49671. if (ret < 0){
  49672. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49673. }
  49674. }
  49675. ExpectIntEQ(ret, 0);
  49676. #endif /* !NO_PKCS7_STREAM */
  49677. /* compare the signer found to expected signer */
  49678. ExpectIntNE(p7Ver->verifyCertSz, 0);
  49679. tmpPtr = NULL;
  49680. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  49681. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  49682. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  49683. tmpPtr = NULL;
  49684. wc_PKCS7_Free(p7Ver);
  49685. p7Ver = NULL;
  49686. ExpectNotNull(out);
  49687. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49688. out = NULL;
  49689. PKCS7_free(p7);
  49690. p7 = NULL;
  49691. }
  49692. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  49693. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  49694. {
  49695. /* re-populate input BIO, may have been consumed */
  49696. BIO_free(inBio);
  49697. inBio = NULL;
  49698. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49699. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49700. flags = PKCS7_BINARY | PKCS7_STREAM;
  49701. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49702. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  49703. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49704. /* PKCS7_final, bad args: PKCS7 null */
  49705. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  49706. /* PKCS7_final, bad args: PKCS7 null */
  49707. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  49708. tmpPtr = out;
  49709. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49710. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  49711. PKCS7_free(p7Ver);
  49712. p7Ver = NULL;
  49713. ExpectNotNull(out);
  49714. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49715. out = NULL;
  49716. PKCS7_free(p7);
  49717. p7 = NULL;
  49718. }
  49719. /* TEST SUCCESS: Detached, not streaming, not MIME */
  49720. {
  49721. /* re-populate input BIO, may have been consumed */
  49722. BIO_free(inBio);
  49723. inBio = NULL;
  49724. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49725. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49726. flags = PKCS7_BINARY | PKCS7_DETACHED;
  49727. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49728. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49729. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  49730. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49731. if (p7Ver != NULL) {
  49732. p7Ver->content = data;
  49733. p7Ver->contentSz = sizeof(data);
  49734. }
  49735. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49736. wc_PKCS7_Free(p7Ver);
  49737. p7Ver = NULL;
  49738. #ifndef NO_PKCS7_STREAM
  49739. /* verify with wc_PKCS7_VerifySignedData streaming */
  49740. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49741. if (p7Ver != NULL) {
  49742. p7Ver->content = data;
  49743. p7Ver->contentSz = sizeof(data);
  49744. }
  49745. /* test for streaming */
  49746. ret = -1;
  49747. for (z = 0; z < outLen && ret != 0; z++) {
  49748. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49749. if (ret < 0){
  49750. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49751. }
  49752. }
  49753. ExpectIntEQ(ret, 0);
  49754. wc_PKCS7_Free(p7Ver);
  49755. p7Ver = NULL;
  49756. #endif /* !NO_PKCS7_STREAM */
  49757. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  49758. * yet support detached content */
  49759. tmpPtr = out;
  49760. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  49761. PKCS7_free(p7Ver);
  49762. p7Ver = NULL;
  49763. ExpectNotNull(out);
  49764. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49765. out = NULL;
  49766. PKCS7_free(p7);
  49767. p7 = NULL;
  49768. }
  49769. /* TEST SUCCESS: Detached, streaming, not MIME */
  49770. {
  49771. /* re-populate input BIO, may have been consumed */
  49772. BIO_free(inBio);
  49773. inBio = NULL;
  49774. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  49775. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  49776. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  49777. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  49778. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  49779. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  49780. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  49781. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49782. if (p7Ver != NULL) {
  49783. p7Ver->content = data;
  49784. p7Ver->contentSz = sizeof(data);
  49785. }
  49786. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  49787. wc_PKCS7_Free(p7Ver);
  49788. p7Ver = NULL;
  49789. ExpectNotNull(out);
  49790. #ifndef NO_PKCS7_STREAM
  49791. /* verify with wc_PKCS7_VerifySignedData streaming */
  49792. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  49793. if (p7Ver != NULL) {
  49794. p7Ver->content = data;
  49795. p7Ver->contentSz = sizeof(data);
  49796. }
  49797. /* test for streaming */
  49798. ret = -1;
  49799. for (z = 0; z < outLen && ret != 0; z++) {
  49800. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  49801. if (ret < 0){
  49802. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  49803. }
  49804. }
  49805. ExpectIntEQ(ret, 0);
  49806. ExpectNotNull(out);
  49807. wc_PKCS7_Free(p7Ver);
  49808. p7Ver = NULL;
  49809. #endif /* !NO_PKCS7_STREAM */
  49810. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49811. PKCS7_free(p7);
  49812. p7 = NULL;
  49813. }
  49814. X509_STORE_free(store);
  49815. X509_free(caCert);
  49816. X509_free(signCert);
  49817. EVP_PKEY_free(signKey);
  49818. BIO_free(inBio);
  49819. BIO_free(keyBio);
  49820. BIO_free(certBio);
  49821. BIO_free(caBio);
  49822. #endif
  49823. return EXPECT_RESULT();
  49824. }
  49825. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  49826. {
  49827. EXPECT_DECLS;
  49828. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  49829. PKCS7_SIGNED* pkcs7 = NULL;
  49830. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  49831. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  49832. PKCS7_SIGNED_free(pkcs7);
  49833. #endif
  49834. return EXPECT_RESULT();
  49835. }
  49836. #ifndef NO_BIO
  49837. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  49838. {
  49839. EXPECT_DECLS;
  49840. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  49841. PKCS7* pkcs7 = NULL;
  49842. BIO* bio = NULL;
  49843. const byte* cert_buf = NULL;
  49844. int ret = 0;
  49845. WC_RNG rng;
  49846. const byte data[] = { /* Hello World */
  49847. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  49848. 0x72,0x6c,0x64
  49849. };
  49850. #ifndef NO_RSA
  49851. #if defined(USE_CERT_BUFFERS_2048)
  49852. byte key[sizeof(client_key_der_2048)];
  49853. byte cert[sizeof(client_cert_der_2048)];
  49854. word32 keySz = (word32)sizeof(key);
  49855. word32 certSz = (word32)sizeof(cert);
  49856. XMEMSET(key, 0, keySz);
  49857. XMEMSET(cert, 0, certSz);
  49858. XMEMCPY(key, client_key_der_2048, keySz);
  49859. XMEMCPY(cert, client_cert_der_2048, certSz);
  49860. #elif defined(USE_CERT_BUFFERS_1024)
  49861. byte key[sizeof_client_key_der_1024];
  49862. byte cert[sizeof(sizeof_client_cert_der_1024)];
  49863. word32 keySz = (word32)sizeof(key);
  49864. word32 certSz = (word32)sizeof(cert);
  49865. XMEMSET(key, 0, keySz);
  49866. XMEMSET(cert, 0, certSz);
  49867. XMEMCPY(key, client_key_der_1024, keySz);
  49868. XMEMCPY(cert, client_cert_der_1024, certSz);
  49869. #else
  49870. unsigned char cert[ONEK_BUF];
  49871. unsigned char key[ONEK_BUF];
  49872. XFILE fp = XBADFILE;
  49873. int certSz;
  49874. int keySz;
  49875. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  49876. XBADFILE);
  49877. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  49878. fp), 0);
  49879. if (fp != XBADFILE) {
  49880. XFCLOSE(fp);
  49881. fp = XBADFILE;
  49882. }
  49883. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  49884. XBADFILE);
  49885. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  49886. 0);
  49887. if (fp != XBADFILE) {
  49888. XFCLOSE(fp);
  49889. fp = XBADFILE;
  49890. }
  49891. #endif
  49892. #elif defined(HAVE_ECC)
  49893. #if defined(USE_CERT_BUFFERS_256)
  49894. unsigned char cert[sizeof(cliecc_cert_der_256)];
  49895. unsigned char key[sizeof(ecc_clikey_der_256)];
  49896. int certSz = (int)sizeof(cert);
  49897. int keySz = (int)sizeof(key);
  49898. XMEMSET(cert, 0, certSz);
  49899. XMEMSET(key, 0, keySz);
  49900. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  49901. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  49902. #else
  49903. unsigned char cert[ONEK_BUF];
  49904. unsigned char key[ONEK_BUF];
  49905. XFILE fp = XBADFILE;
  49906. int certSz, keySz;
  49907. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  49908. XBADFILE);
  49909. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  49910. fp), 0);
  49911. if (fp != XBADFILE) {
  49912. XFCLOSE(fp);
  49913. fp = XBADFILE;
  49914. }
  49915. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  49916. XBADFILE);
  49917. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  49918. 0);
  49919. if (fp != XBADFILE) {
  49920. XFCLOSE(fp);
  49921. fp = XBADFILE;
  49922. }
  49923. #endif
  49924. #else
  49925. #error PKCS7 requires ECC or RSA
  49926. #endif
  49927. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  49928. /* initialize with DER encoded cert */
  49929. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  49930. /* init rng */
  49931. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49932. ExpectIntEQ(wc_InitRng(&rng), 0);
  49933. if (pkcs7 != NULL) {
  49934. pkcs7->rng = &rng;
  49935. pkcs7->content = (byte*)data; /* not used for ex */
  49936. pkcs7->contentSz = (word32)sizeof(data);
  49937. pkcs7->contentOID = SIGNED_DATA;
  49938. pkcs7->privateKey = key;
  49939. pkcs7->privateKeySz = (word32)sizeof(key);
  49940. pkcs7->encryptOID = RSAk;
  49941. #ifdef NO_SHA
  49942. pkcs7->hashOID = SHA256h;
  49943. #else
  49944. pkcs7->hashOID = SHAh;
  49945. #endif
  49946. pkcs7->signedAttribs = NULL;
  49947. pkcs7->signedAttribsSz = 0;
  49948. }
  49949. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  49950. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  49951. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  49952. /* Read PKCS#7 PEM from BIO */
  49953. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  49954. ExpectIntGE(ret, 0);
  49955. BIO_free(bio);
  49956. wc_PKCS7_Free(pkcs7);
  49957. wc_FreeRng(&rng);
  49958. #endif
  49959. return EXPECT_RESULT();
  49960. }
  49961. #ifdef HAVE_SMIME
  49962. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  49963. static int test_wolfSSL_SMIME_read_PKCS7(void)
  49964. {
  49965. EXPECT_DECLS;
  49966. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  49967. !defined(NO_RSA)
  49968. PKCS7* pkcs7 = NULL;
  49969. BIO* bio = NULL;
  49970. BIO* bcont = NULL;
  49971. BIO* out = NULL;
  49972. const byte* outBuf = NULL;
  49973. int outBufLen = 0;
  49974. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  49975. XFILE smimeTestFile = XBADFILE;
  49976. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  49977. XBADFILE);
  49978. /* smime-test.p7s */
  49979. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  49980. ExpectNotNull(bio);
  49981. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  49982. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  49983. ExpectNotNull(pkcs7);
  49984. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  49985. PKCS7_NOVERIFY), SSL_SUCCESS);
  49986. if (smimeTestFile != XBADFILE) {
  49987. XFCLOSE(smimeTestFile);
  49988. smimeTestFile = XBADFILE;
  49989. }
  49990. if (bcont) BIO_free(bcont);
  49991. bcont = NULL;
  49992. wolfSSL_PKCS7_free(pkcs7);
  49993. pkcs7 = NULL;
  49994. /* smime-test-multipart.p7s */
  49995. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  49996. ExpectFalse(smimeTestFile == XBADFILE);
  49997. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  49998. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  49999. ExpectNotNull(pkcs7);
  50000. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50001. PKCS7_NOVERIFY), SSL_SUCCESS);
  50002. if (smimeTestFile != XBADFILE) {
  50003. XFCLOSE(smimeTestFile);
  50004. smimeTestFile = XBADFILE;
  50005. }
  50006. if (bcont) BIO_free(bcont);
  50007. bcont = NULL;
  50008. wolfSSL_PKCS7_free(pkcs7);
  50009. pkcs7 = NULL;
  50010. /* smime-test-multipart-badsig.p7s */
  50011. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  50012. ExpectFalse(smimeTestFile == XBADFILE);
  50013. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50014. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50015. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  50016. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50017. PKCS7_NOVERIFY), SSL_FAILURE);
  50018. if (smimeTestFile != XBADFILE) {
  50019. XFCLOSE(smimeTestFile);
  50020. smimeTestFile = XBADFILE;
  50021. }
  50022. if (bcont) BIO_free(bcont);
  50023. bcont = NULL;
  50024. wolfSSL_PKCS7_free(pkcs7);
  50025. pkcs7 = NULL;
  50026. /* smime-test-canon.p7s */
  50027. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  50028. ExpectFalse(smimeTestFile == XBADFILE);
  50029. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50030. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50031. ExpectNotNull(pkcs7);
  50032. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  50033. PKCS7_NOVERIFY), SSL_SUCCESS);
  50034. if (smimeTestFile != XBADFILE) {
  50035. XFCLOSE(smimeTestFile);
  50036. smimeTestFile = XBADFILE;
  50037. }
  50038. if (bcont) BIO_free(bcont);
  50039. bcont = NULL;
  50040. wolfSSL_PKCS7_free(pkcs7);
  50041. pkcs7 = NULL;
  50042. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  50043. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  50044. ExpectFalse(smimeTestFile == XBADFILE);
  50045. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  50046. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  50047. ExpectNotNull(pkcs7);
  50048. out = wolfSSL_BIO_new(BIO_s_mem());
  50049. ExpectNotNull(out);
  50050. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  50051. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  50052. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  50053. /* Content-Type should not show up at beginning of output buffer */
  50054. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  50055. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  50056. BIO_free(out);
  50057. BIO_free(bio);
  50058. if (bcont) BIO_free(bcont);
  50059. wolfSSL_PKCS7_free(pkcs7);
  50060. #endif
  50061. return EXPECT_RESULT();
  50062. }
  50063. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  50064. static int test_wolfSSL_SMIME_write_PKCS7(void)
  50065. {
  50066. EXPECT_DECLS;
  50067. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  50068. PKCS7* p7 = NULL;
  50069. PKCS7* p7Ver = NULL;
  50070. int flags = 0;
  50071. byte data[] = "Test data to encode.";
  50072. const char* cert = "./certs/server-cert.pem";
  50073. const char* key = "./certs/server-key.pem";
  50074. const char* ca = "./certs/ca-cert.pem";
  50075. WOLFSSL_BIO* certBio = NULL;
  50076. WOLFSSL_BIO* keyBio = NULL;
  50077. WOLFSSL_BIO* caBio = NULL;
  50078. WOLFSSL_BIO* inBio = NULL;
  50079. WOLFSSL_BIO* outBio = NULL;
  50080. WOLFSSL_BIO* content = NULL;
  50081. X509* signCert = NULL;
  50082. EVP_PKEY* signKey = NULL;
  50083. X509* caCert = NULL;
  50084. X509_STORE* store = NULL;
  50085. /* read signer cert/key into BIO */
  50086. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  50087. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  50088. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  50089. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  50090. /* read CA cert into store (for verify) */
  50091. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  50092. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  50093. ExpectNotNull(store = X509_STORE_new());
  50094. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  50095. /* generate and verify SMIME: not detached */
  50096. {
  50097. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50098. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50099. flags = PKCS7_STREAM;
  50100. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50101. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50102. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50103. /* bad arg: out NULL */
  50104. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  50105. /* bad arg: pkcs7 NULL */
  50106. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  50107. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50108. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  50109. BIO_free(content);
  50110. content = NULL;
  50111. BIO_free(inBio);
  50112. inBio = NULL;
  50113. BIO_free(outBio);
  50114. outBio = NULL;
  50115. PKCS7_free(p7Ver);
  50116. p7Ver = NULL;
  50117. PKCS7_free(p7);
  50118. p7 = NULL;
  50119. }
  50120. /* generate and verify SMIME: not detached, add Content-Type */
  50121. {
  50122. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50123. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50124. flags = PKCS7_STREAM | PKCS7_TEXT;
  50125. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50126. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50127. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50128. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50129. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  50130. BIO_free(content);
  50131. content = NULL;
  50132. BIO_free(inBio);
  50133. inBio = NULL;
  50134. BIO_free(outBio);
  50135. outBio = NULL;
  50136. PKCS7_free(p7Ver);
  50137. p7Ver = NULL;
  50138. PKCS7_free(p7);
  50139. p7 = NULL;
  50140. }
  50141. /* generate and verify SMIME: detached */
  50142. {
  50143. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50144. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50145. flags = PKCS7_DETACHED | PKCS7_STREAM;
  50146. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50147. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50148. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50149. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50150. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  50151. BIO_free(content);
  50152. content = NULL;
  50153. BIO_free(inBio);
  50154. inBio = NULL;
  50155. BIO_free(outBio);
  50156. outBio = NULL;
  50157. PKCS7_free(p7Ver);
  50158. p7Ver = NULL;
  50159. PKCS7_free(p7);
  50160. p7 = NULL;
  50161. }
  50162. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  50163. {
  50164. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  50165. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  50166. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  50167. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  50168. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  50169. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  50170. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  50171. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  50172. BIO_free(content);
  50173. content = NULL;
  50174. BIO_free(inBio);
  50175. inBio = NULL;
  50176. BIO_free(outBio);
  50177. outBio = NULL;
  50178. PKCS7_free(p7Ver);
  50179. p7Ver = NULL;
  50180. PKCS7_free(p7);
  50181. p7 = NULL;
  50182. }
  50183. X509_STORE_free(store);
  50184. X509_free(caCert);
  50185. X509_free(signCert);
  50186. EVP_PKEY_free(signKey);
  50187. BIO_free(keyBio);
  50188. BIO_free(certBio);
  50189. BIO_free(caBio);
  50190. #endif
  50191. return EXPECT_RESULT();
  50192. }
  50193. #endif /* HAVE_SMIME */
  50194. #endif /* !NO_BIO */
  50195. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  50196. * returns 0) */
  50197. static int test_X509_STORE_No_SSL_CTX(void)
  50198. {
  50199. EXPECT_DECLS;
  50200. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  50201. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  50202. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  50203. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  50204. defined(HAVE_CRL) && !defined(NO_RSA)
  50205. X509_STORE * store = NULL;
  50206. X509_STORE_CTX * storeCtx = NULL;
  50207. X509_CRL * crl = NULL;
  50208. X509 * ca = NULL;
  50209. X509 * cert = NULL;
  50210. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  50211. const char srvCert[] = "./certs/server-cert.pem";
  50212. const char caCert[] = "./certs/ca-cert.pem";
  50213. const char caDir[] = "./certs/crl/hash_pem";
  50214. XFILE fp = XBADFILE;
  50215. X509_LOOKUP * lookup = NULL;
  50216. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50217. /* Set up store with CA */
  50218. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50219. SSL_FILETYPE_PEM)));
  50220. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50221. /* Add CRL lookup directory to store
  50222. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  50223. * of crl.pem */
  50224. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50225. X509_LOOKUP_hash_dir())));
  50226. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  50227. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  50228. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50229. SSL_SUCCESS);
  50230. /* Add CRL to store NOT containing the verified certificate, which
  50231. * forces use of the CRL lookup directory */
  50232. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50233. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50234. NULL, NULL));
  50235. if (fp != XBADFILE)
  50236. XFCLOSE(fp);
  50237. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50238. /* Create verification context outside of an SSL session */
  50239. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50240. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50241. SSL_FILETYPE_PEM)));
  50242. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50243. /* Perform verification, which should NOT indicate CRL missing due to the
  50244. * store CM's X509 store pointer being NULL */
  50245. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  50246. X509_CRL_free(crl);
  50247. X509_STORE_free(store);
  50248. X509_STORE_CTX_free(storeCtx);
  50249. X509_free(cert);
  50250. X509_free(ca);
  50251. #endif
  50252. return EXPECT_RESULT();
  50253. }
  50254. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  50255. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  50256. static int test_X509_LOOKUP_add_dir(void)
  50257. {
  50258. EXPECT_DECLS;
  50259. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  50260. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  50261. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  50262. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  50263. defined(HAVE_CRL) && !defined(NO_RSA)
  50264. X509_STORE * store = NULL;
  50265. X509_STORE_CTX * storeCtx = NULL;
  50266. X509_CRL * crl = NULL;
  50267. X509 * ca = NULL;
  50268. X509 * cert = NULL;
  50269. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  50270. const char srvCert[] = "./certs/server-cert.pem";
  50271. const char caCert[] = "./certs/ca-cert.pem";
  50272. const char caDir[] = "./certs/crl/hash_der";
  50273. XFILE fp = XBADFILE;
  50274. X509_LOOKUP * lookup = NULL;
  50275. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50276. /* Set up store with CA */
  50277. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50278. SSL_FILETYPE_PEM)));
  50279. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50280. /* Add CRL lookup directory to store.
  50281. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  50282. * of crl.der */
  50283. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50284. X509_LOOKUP_hash_dir())));
  50285. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  50286. SSL_SUCCESS);
  50287. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50288. SSL_SUCCESS);
  50289. /* Add CRL to store NOT containing the verified certificate, which
  50290. * forces use of the CRL lookup directory */
  50291. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50292. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50293. NULL, NULL));
  50294. if (fp != XBADFILE) {
  50295. XFCLOSE(fp);
  50296. fp = XBADFILE;
  50297. }
  50298. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50299. /* Create verification context outside of an SSL session */
  50300. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50301. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50302. SSL_FILETYPE_PEM)));
  50303. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50304. /* Perform verification, which should NOT return CRL missing */
  50305. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  50306. X509_CRL_free(crl);
  50307. crl = NULL;
  50308. X509_STORE_free(store);
  50309. store = NULL;
  50310. X509_STORE_CTX_free(storeCtx);
  50311. storeCtx = NULL;
  50312. X509_free(cert);
  50313. cert = NULL;
  50314. X509_free(ca);
  50315. ca = NULL;
  50316. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  50317. * We should get CRL_MISSING at the end, because the lookup
  50318. * dir has only ASN1 CRLs. */
  50319. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  50320. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  50321. SSL_FILETYPE_PEM)));
  50322. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  50323. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  50324. X509_LOOKUP_hash_dir())));
  50325. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  50326. SSL_SUCCESS);
  50327. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  50328. SSL_SUCCESS);
  50329. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  50330. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  50331. NULL, NULL));
  50332. if (fp != XBADFILE) {
  50333. XFCLOSE(fp);
  50334. fp = XBADFILE;
  50335. }
  50336. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  50337. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  50338. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  50339. SSL_FILETYPE_PEM)));
  50340. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  50341. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  50342. * in dir containing only ASN1/DER. */
  50343. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  50344. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  50345. X509_V_ERR_UNABLE_TO_GET_CRL);
  50346. X509_CRL_free(crl);
  50347. X509_STORE_free(store);
  50348. X509_STORE_CTX_free(storeCtx);
  50349. X509_free(cert);
  50350. X509_free(ca);
  50351. #endif
  50352. return EXPECT_RESULT();
  50353. }
  50354. /*----------------------------------------------------------------------------*
  50355. | Certificate Failure Checks
  50356. *----------------------------------------------------------------------------*/
  50357. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  50358. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  50359. #if !defined(NO_RSA) || defined(HAVE_ECC)
  50360. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  50361. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  50362. int type)
  50363. {
  50364. int ret;
  50365. WOLFSSL_CERT_MANAGER* cm = NULL;
  50366. switch (type) {
  50367. case TESTING_RSA:
  50368. #ifdef NO_RSA
  50369. fprintf(stderr, "RSA disabled, skipping test\n");
  50370. return ASN_SIG_CONFIRM_E;
  50371. #else
  50372. break;
  50373. #endif
  50374. case TESTING_ECC:
  50375. #ifndef HAVE_ECC
  50376. fprintf(stderr, "ECC disabled, skipping test\n");
  50377. return ASN_SIG_CONFIRM_E;
  50378. #else
  50379. break;
  50380. #endif
  50381. default:
  50382. fprintf(stderr, "Bad function argument\n");
  50383. return BAD_FUNC_ARG;
  50384. }
  50385. cm = wolfSSL_CertManagerNew();
  50386. if (cm == NULL) {
  50387. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  50388. return -1;
  50389. }
  50390. #ifndef NO_FILESYSTEM
  50391. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  50392. if (ret != WOLFSSL_SUCCESS) {
  50393. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  50394. wolfSSL_CertManagerFree(cm);
  50395. return ret;
  50396. }
  50397. #else
  50398. (void)ca;
  50399. #endif
  50400. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  50401. WOLFSSL_FILETYPE_ASN1);
  50402. /* Let ExpectIntEQ handle return code */
  50403. wolfSSL_CertManagerFree(cm);
  50404. return ret;
  50405. }
  50406. #endif
  50407. #if !defined(NO_FILESYSTEM)
  50408. static int test_RsaSigFailure_cm(void)
  50409. {
  50410. EXPECT_DECLS;
  50411. #ifndef NO_RSA
  50412. const char* ca_cert = "./certs/ca-cert.pem";
  50413. const char* server_cert = "./certs/server-cert.der";
  50414. byte* cert_buf = NULL;
  50415. size_t cert_sz = 0;
  50416. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  50417. if (cert_buf != NULL) {
  50418. /* corrupt DER - invert last byte, which is signature */
  50419. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  50420. /* test bad cert */
  50421. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  50422. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  50423. WOLFSSL_FATAL_ERROR);
  50424. #else
  50425. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  50426. ASN_SIG_CONFIRM_E);
  50427. #endif
  50428. }
  50429. /* load_file() uses malloc. */
  50430. if (cert_buf != NULL) {
  50431. free(cert_buf);
  50432. }
  50433. #endif /* !NO_RSA */
  50434. return EXPECT_RESULT();
  50435. }
  50436. static int test_EccSigFailure_cm(void)
  50437. {
  50438. EXPECT_DECLS;
  50439. #ifdef HAVE_ECC
  50440. /* self-signed ECC cert, so use server cert as CA */
  50441. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  50442. const char* server_cert = "./certs/server-ecc.der";
  50443. byte* cert_buf = NULL;
  50444. size_t cert_sz = 0;
  50445. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  50446. if (cert_buf != NULL) {
  50447. /* corrupt DER - invert last byte, which is signature */
  50448. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  50449. /* test bad cert */
  50450. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  50451. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  50452. WOLFSSL_FATAL_ERROR);
  50453. #else
  50454. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  50455. ASN_SIG_CONFIRM_E);
  50456. #endif
  50457. }
  50458. /* load_file() uses malloc. */
  50459. if (cert_buf != NULL) {
  50460. free(cert_buf);
  50461. }
  50462. #ifdef FP_ECC
  50463. wc_ecc_fp_free();
  50464. #endif
  50465. #endif /* HAVE_ECC */
  50466. return EXPECT_RESULT();
  50467. }
  50468. #endif /* !NO_FILESYSTEM */
  50469. #endif /* NO_CERTS */
  50470. #ifdef WOLFSSL_TLS13
  50471. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  50472. #ifdef WC_SHA384_DIGEST_SIZE
  50473. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  50474. #else
  50475. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  50476. #endif
  50477. #endif
  50478. #ifdef WOLFSSL_EARLY_DATA
  50479. static const char earlyData[] = "Early Data";
  50480. static char earlyDataBuffer[1];
  50481. #endif
  50482. static int test_tls13_apis(void)
  50483. {
  50484. EXPECT_DECLS;
  50485. int ret;
  50486. #ifndef WOLFSSL_NO_TLS12
  50487. #ifndef NO_WOLFSSL_CLIENT
  50488. WOLFSSL_CTX* clientTls12Ctx = NULL;
  50489. WOLFSSL* clientTls12Ssl = NULL;
  50490. #endif
  50491. #ifndef NO_WOLFSSL_SERVER
  50492. WOLFSSL_CTX* serverTls12Ctx = NULL;
  50493. WOLFSSL* serverTls12Ssl = NULL;
  50494. #endif
  50495. #endif
  50496. #ifndef NO_WOLFSSL_CLIENT
  50497. WOLFSSL_CTX* clientCtx = NULL;
  50498. WOLFSSL* clientSsl = NULL;
  50499. #endif
  50500. #ifndef NO_WOLFSSL_SERVER
  50501. WOLFSSL_CTX* serverCtx = NULL;
  50502. WOLFSSL* serverSsl = NULL;
  50503. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50504. const char* ourCert = svrCertFile;
  50505. const char* ourKey = svrKeyFile;
  50506. #endif
  50507. #endif
  50508. int required;
  50509. #ifdef WOLFSSL_EARLY_DATA
  50510. int outSz;
  50511. #endif
  50512. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  50513. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  50514. #ifdef WOLFSSL_HAVE_KYBER
  50515. WOLFSSL_KYBER_LEVEL1
  50516. #else
  50517. WOLFSSL_ECC_SECP256R1
  50518. #endif
  50519. };
  50520. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  50521. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  50522. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  50523. int numGroups = 2;
  50524. #endif
  50525. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  50526. char groupList[] =
  50527. #ifdef HAVE_CURVE25519
  50528. "X25519:"
  50529. #endif
  50530. #ifdef HAVE_CURVE448
  50531. "X448:"
  50532. #endif
  50533. #ifndef NO_ECC_SECP
  50534. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  50535. "P-521:secp521r1:"
  50536. #endif
  50537. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  50538. "P-384:secp384r1:"
  50539. #endif
  50540. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  50541. "P-256:secp256r1"
  50542. #if defined(WOLFSSL_HAVE_KYBER)
  50543. ":P256_KYBER_LEVEL1"
  50544. #endif
  50545. #endif
  50546. #endif /* !defined(NO_ECC_SECP) */
  50547. #if defined(WOLFSSL_HAVE_KYBER)
  50548. ":KYBER_LEVEL1"
  50549. #endif
  50550. "";
  50551. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  50552. (void)ret;
  50553. #ifndef WOLFSSL_NO_TLS12
  50554. #ifndef NO_WOLFSSL_CLIENT
  50555. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  50556. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  50557. #endif
  50558. #ifndef NO_WOLFSSL_SERVER
  50559. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  50560. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50561. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  50562. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  50563. WOLFSSL_FILETYPE_PEM);
  50564. #endif
  50565. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  50566. #endif
  50567. #endif
  50568. #ifndef NO_WOLFSSL_CLIENT
  50569. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  50570. clientSsl = wolfSSL_new(clientCtx);
  50571. #endif
  50572. #ifndef NO_WOLFSSL_SERVER
  50573. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  50574. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  50575. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  50576. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  50577. #endif
  50578. serverSsl = wolfSSL_new(serverCtx);
  50579. ExpectNotNull(serverSsl);
  50580. #endif
  50581. #ifdef WOLFSSL_SEND_HRR_COOKIE
  50582. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  50583. #ifndef NO_WOLFSSL_CLIENT
  50584. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  50585. #endif
  50586. #ifndef NO_WOLFSSL_SERVER
  50587. #ifndef WOLFSSL_NO_TLS12
  50588. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  50589. BAD_FUNC_ARG);
  50590. #endif
  50591. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  50592. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  50593. WOLFSSL_SUCCESS);
  50594. #endif
  50595. #endif
  50596. #ifdef HAVE_SUPPORTED_CURVES
  50597. #ifdef HAVE_ECC
  50598. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  50599. BAD_FUNC_ARG);
  50600. #ifndef NO_WOLFSSL_SERVER
  50601. do {
  50602. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  50603. #ifdef WOLFSSL_ASYNC_CRYPT
  50604. if (ret == WC_PENDING_E)
  50605. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  50606. #endif
  50607. }
  50608. while (ret == WC_PENDING_E);
  50609. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50610. #endif
  50611. #ifndef NO_WOLFSSL_CLIENT
  50612. #ifndef WOLFSSL_NO_TLS12
  50613. do {
  50614. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  50615. #ifdef WOLFSSL_ASYNC_CRYPT
  50616. if (ret == WC_PENDING_E)
  50617. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  50618. #endif
  50619. }
  50620. while (ret == WC_PENDING_E);
  50621. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50622. #endif
  50623. do {
  50624. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  50625. #ifdef WOLFSSL_ASYNC_CRYPT
  50626. if (ret == WC_PENDING_E)
  50627. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  50628. #endif
  50629. }
  50630. while (ret == WC_PENDING_E);
  50631. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  50632. #endif
  50633. #elif defined(HAVE_CURVE25519)
  50634. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  50635. #ifndef NO_WOLFSSL_SERVER
  50636. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  50637. WOLFSSL_SUCCESS);
  50638. #endif
  50639. #ifndef NO_WOLFSSL_CLIENT
  50640. #ifndef WOLFSSL_NO_TLS12
  50641. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  50642. WOLFSSL_SUCCESS);
  50643. #endif
  50644. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  50645. WOLFSSL_SUCCESS);
  50646. #endif
  50647. #elif defined(HAVE_CURVE448)
  50648. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  50649. #ifndef NO_WOLFSSL_SERVER
  50650. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  50651. WOLFSSL_SUCCESS);
  50652. #endif
  50653. #ifndef NO_WOLFSSL_CLIENT
  50654. #ifndef WOLFSSL_NO_TLS12
  50655. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  50656. WOLFSSL_SUCCESS);
  50657. #endif
  50658. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  50659. WOLFSSL_SUCCESS);
  50660. #endif
  50661. #else
  50662. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  50663. BAD_FUNC_ARG);
  50664. #ifndef NO_WOLFSSL_CLIENT
  50665. #ifndef WOLFSSL_NO_TLS12
  50666. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  50667. NOT_COMPILED_IN);
  50668. #endif
  50669. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  50670. NOT_COMPILED_IN);
  50671. #endif
  50672. #endif
  50673. #if defined(WOLFSSL_HAVE_KYBER)
  50674. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  50675. #ifndef NO_WOLFSSL_SERVER
  50676. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  50677. WOLFSSL_SUCCESS);
  50678. #endif
  50679. #ifndef NO_WOLFSSL_CLIENT
  50680. #ifndef WOLFSSL_NO_TLS12
  50681. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  50682. BAD_FUNC_ARG);
  50683. #endif
  50684. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  50685. WOLFSSL_SUCCESS);
  50686. #endif
  50687. #endif
  50688. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  50689. #ifndef NO_WOLFSSL_SERVER
  50690. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  50691. #endif
  50692. #ifndef NO_WOLFSSL_CLIENT
  50693. #ifndef WOLFSSL_NO_TLS12
  50694. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  50695. #endif
  50696. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  50697. #endif
  50698. #endif /* HAVE_SUPPORTED_CURVES */
  50699. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  50700. #ifndef NO_WOLFSSL_CLIENT
  50701. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  50702. #endif
  50703. #ifndef NO_WOLFSSL_SERVER
  50704. #ifndef WOLFSSL_NO_TLS12
  50705. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  50706. #endif
  50707. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  50708. #endif
  50709. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  50710. #ifndef NO_WOLFSSL_CLIENT
  50711. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  50712. #endif
  50713. #ifndef NO_WOLFSSL_SERVER
  50714. #ifndef WOLFSSL_NO_TLS12
  50715. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  50716. #endif
  50717. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  50718. #endif
  50719. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  50720. #ifndef NO_WOLFSSL_CLIENT
  50721. #ifndef WOLFSSL_NO_TLS12
  50722. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  50723. #endif
  50724. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  50725. #endif
  50726. #ifndef NO_WOLFSSL_SERVER
  50727. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  50728. #endif
  50729. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  50730. #ifndef NO_WOLFSSL_CLIENT
  50731. #ifndef WOLFSSL_NO_TLS12
  50732. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  50733. #endif
  50734. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  50735. #endif
  50736. #ifndef NO_WOLFSSL_SERVER
  50737. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  50738. #endif
  50739. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  50740. #ifndef NO_WOLFSSL_CLIENT
  50741. #ifndef WOLFSSL_NO_TLS12
  50742. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  50743. #endif
  50744. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  50745. #endif
  50746. #ifndef NO_WOLFSSL_SERVER
  50747. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  50748. #endif
  50749. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  50750. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  50751. #ifndef NO_WOLFSSL_CLIENT
  50752. #ifndef WOLFSSL_NO_TLS12
  50753. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  50754. BAD_FUNC_ARG);
  50755. #endif
  50756. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  50757. #endif
  50758. #ifndef NO_WOLFSSL_SERVER
  50759. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  50760. #endif
  50761. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  50762. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  50763. #ifndef NO_WOLFSSL_SERVER
  50764. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  50765. #endif
  50766. #ifndef NO_WOLFSSL_CLIENT
  50767. #ifndef WOLFSSL_NO_TLS12
  50768. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  50769. BAD_FUNC_ARG);
  50770. #endif
  50771. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  50772. #endif
  50773. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  50774. #ifndef NO_WOLFSSL_SERVER
  50775. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  50776. #endif
  50777. #ifndef NO_WOLFSSL_CLIENT
  50778. #ifndef WOLFSSL_NO_TLS12
  50779. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  50780. BAD_FUNC_ARG);
  50781. #endif
  50782. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  50783. #endif
  50784. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  50785. #ifndef NO_WOLFSSL_CLIENT
  50786. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  50787. #endif
  50788. #ifndef NO_WOLFSSL_SERVER
  50789. #ifndef WOLFSSL_NO_TLS12
  50790. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  50791. BAD_FUNC_ARG);
  50792. #endif
  50793. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  50794. #endif
  50795. #endif
  50796. #ifdef HAVE_ECC
  50797. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  50798. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  50799. #ifndef NO_WOLFSSL_SERVER
  50800. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  50801. #endif
  50802. #ifndef NO_WOLFSSL_CLIENT
  50803. #ifndef WOLFSSL_NO_TLS12
  50804. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  50805. #endif
  50806. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  50807. #endif
  50808. #endif
  50809. #ifdef HAVE_SUPPORTED_CURVES
  50810. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  50811. #ifndef NO_WOLFSSL_CLIENT
  50812. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  50813. #endif
  50814. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  50815. #ifndef NO_WOLFSSL_CLIENT
  50816. #ifndef WOLFSSL_NO_TLS12
  50817. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  50818. WOLFSSL_SUCCESS);
  50819. #endif
  50820. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  50821. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  50822. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  50823. WOLFSSL_SUCCESS);
  50824. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  50825. BAD_FUNC_ARG);
  50826. #endif
  50827. #ifndef NO_WOLFSSL_SERVER
  50828. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  50829. WOLFSSL_SUCCESS);
  50830. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  50831. BAD_FUNC_ARG);
  50832. #endif
  50833. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  50834. #ifndef NO_WOLFSSL_CLIENT
  50835. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  50836. #endif
  50837. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  50838. #ifndef NO_WOLFSSL_CLIENT
  50839. #ifndef WOLFSSL_NO_TLS12
  50840. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  50841. WOLFSSL_SUCCESS);
  50842. #endif
  50843. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  50844. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  50845. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  50846. WOLFSSL_SUCCESS);
  50847. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  50848. BAD_FUNC_ARG);
  50849. #endif
  50850. #ifndef NO_WOLFSSL_SERVER
  50851. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  50852. WOLFSSL_SUCCESS);
  50853. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  50854. BAD_FUNC_ARG);
  50855. #endif
  50856. #ifdef OPENSSL_EXTRA
  50857. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  50858. #ifndef NO_WOLFSSL_CLIENT
  50859. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  50860. WOLFSSL_FAILURE);
  50861. #endif
  50862. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  50863. WOLFSSL_FAILURE);
  50864. #ifndef NO_WOLFSSL_CLIENT
  50865. #ifndef WOLFSSL_NO_TLS12
  50866. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  50867. WOLFSSL_SUCCESS);
  50868. #endif
  50869. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  50870. WOLFSSL_SUCCESS);
  50871. #endif
  50872. #ifndef NO_WOLFSSL_SERVER
  50873. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  50874. WOLFSSL_SUCCESS);
  50875. #endif
  50876. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  50877. #ifndef NO_WOLFSSL_CLIENT
  50878. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  50879. #endif
  50880. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  50881. #ifndef NO_WOLFSSL_CLIENT
  50882. #ifndef WOLFSSL_NO_TLS12
  50883. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  50884. WOLFSSL_SUCCESS);
  50885. #endif
  50886. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  50887. WOLFSSL_SUCCESS);
  50888. #endif
  50889. #ifndef NO_WOLFSSL_SERVER
  50890. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  50891. WOLFSSL_SUCCESS);
  50892. #endif
  50893. #endif /* OPENSSL_EXTRA */
  50894. #endif /* HAVE_SUPPORTED_CURVES */
  50895. #endif /* HAVE_ECC */
  50896. #ifdef WOLFSSL_EARLY_DATA
  50897. #ifndef OPENSSL_EXTRA
  50898. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50899. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  50900. #else
  50901. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50902. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  50903. #endif
  50904. #ifndef NO_WOLFSSL_CLIENT
  50905. #ifndef OPENSSL_EXTRA
  50906. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  50907. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  50908. #else
  50909. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  50910. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  50911. #endif
  50912. #endif
  50913. #ifndef NO_WOLFSSL_SERVER
  50914. #ifndef WOLFSSL_NO_TLS12
  50915. #ifndef OPENSSL_EXTRA
  50916. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  50917. BAD_FUNC_ARG);
  50918. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  50919. #else
  50920. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  50921. BAD_FUNC_ARG);
  50922. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  50923. #endif
  50924. #endif
  50925. #ifndef OPENSSL_EXTRA
  50926. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  50927. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  50928. WOLFSSL_SUCCESS);
  50929. #else
  50930. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  50931. #endif
  50932. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  50933. #else
  50934. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  50935. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  50936. #endif
  50937. #endif
  50938. #ifndef OPENSSL_EXTRA
  50939. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50940. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  50941. #else
  50942. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  50943. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  50944. #endif
  50945. #ifndef NO_WOLFSSL_CLIENT
  50946. #ifndef OPENSSL_EXTRA
  50947. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  50948. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  50949. #else
  50950. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  50951. #endif
  50952. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  50953. #else
  50954. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  50955. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  50956. #endif
  50957. #endif
  50958. #ifndef NO_WOLFSSL_SERVER
  50959. #ifndef WOLFSSL_NO_TLS12
  50960. #ifndef OPENSSL_EXTRA
  50961. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  50962. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  50963. #else
  50964. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  50965. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  50966. #endif
  50967. #endif
  50968. #ifndef OPENSSL_EXTRA
  50969. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  50970. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  50971. #else
  50972. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  50973. #endif
  50974. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  50975. #else
  50976. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  50977. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  50978. #endif
  50979. #endif
  50980. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  50981. &outSz), BAD_FUNC_ARG);
  50982. #ifndef NO_WOLFSSL_CLIENT
  50983. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  50984. &outSz), BAD_FUNC_ARG);
  50985. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  50986. BAD_FUNC_ARG);
  50987. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  50988. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  50989. #endif
  50990. #ifndef NO_WOLFSSL_SERVER
  50991. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  50992. sizeof(earlyData), &outSz), SIDE_ERROR);
  50993. #endif
  50994. #ifndef NO_WOLFSSL_CLIENT
  50995. #ifndef WOLFSSL_NO_TLS12
  50996. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  50997. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  50998. #endif
  50999. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  51000. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  51001. #endif
  51002. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  51003. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51004. #ifndef NO_WOLFSSL_SERVER
  51005. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  51006. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51007. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  51008. &outSz), BAD_FUNC_ARG);
  51009. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  51010. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  51011. #endif
  51012. #ifndef NO_WOLFSSL_CLIENT
  51013. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  51014. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  51015. #endif
  51016. #ifndef NO_WOLFSSL_SERVER
  51017. #ifndef WOLFSSL_NO_TLS12
  51018. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  51019. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  51020. #endif
  51021. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  51022. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  51023. #endif
  51024. #endif
  51025. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  51026. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  51027. #endif
  51028. #ifndef NO_WOLFSSL_SERVER
  51029. wolfSSL_free(serverSsl);
  51030. wolfSSL_CTX_free(serverCtx);
  51031. #endif
  51032. #ifndef NO_WOLFSSL_CLIENT
  51033. wolfSSL_free(clientSsl);
  51034. wolfSSL_CTX_free(clientCtx);
  51035. #endif
  51036. #ifndef WOLFSSL_NO_TLS12
  51037. #ifndef NO_WOLFSSL_SERVER
  51038. wolfSSL_free(serverTls12Ssl);
  51039. wolfSSL_CTX_free(serverTls12Ctx);
  51040. #endif
  51041. #ifndef NO_WOLFSSL_CLIENT
  51042. wolfSSL_free(clientTls12Ssl);
  51043. wolfSSL_CTX_free(clientTls12Ctx);
  51044. #endif
  51045. #endif
  51046. return EXPECT_RESULT();
  51047. }
  51048. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  51049. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  51050. defined(BUILD_TLS_AES_256_GCM_SHA384)
  51051. /* Called when writing. */
  51052. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  51053. {
  51054. (void)ssl;
  51055. (void)buf;
  51056. (void)sz;
  51057. (void)ctx;
  51058. /* Force error return from wolfSSL_accept_TLSv13(). */
  51059. return WANT_WRITE;
  51060. }
  51061. /* Called when reading. */
  51062. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  51063. {
  51064. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  51065. int len = (int)msg->length;
  51066. (void)ssl;
  51067. (void)sz;
  51068. /* Pass back as much of message as will fit in buffer. */
  51069. if (len > sz)
  51070. len = sz;
  51071. XMEMCPY(buf, msg->buffer, len);
  51072. /* Move over returned data. */
  51073. msg->buffer += len;
  51074. msg->length -= len;
  51075. /* Amount actually copied. */
  51076. return len;
  51077. }
  51078. #endif
  51079. static int test_tls13_cipher_suites(void)
  51080. {
  51081. EXPECT_DECLS;
  51082. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  51083. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  51084. defined(BUILD_TLS_AES_256_GCM_SHA384)
  51085. WOLFSSL_CTX* ctx = NULL;
  51086. WOLFSSL *ssl = NULL;
  51087. int i;
  51088. byte clientHello[] = {
  51089. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  51090. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  51091. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  51092. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  51093. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  51094. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  51095. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  51096. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  51097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51098. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  51099. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  51100. 0x13, 0x01,
  51101. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  51102. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  51103. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  51104. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  51105. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  51106. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  51107. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  51108. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  51109. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  51110. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  51111. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  51112. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  51113. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  51114. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  51115. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  51116. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  51117. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  51118. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  51119. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  51120. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  51121. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  51122. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  51123. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  51124. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  51125. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  51126. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  51127. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  51128. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  51129. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  51130. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  51131. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  51132. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  51133. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  51134. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  51135. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  51136. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51137. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51138. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  51139. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  51140. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  51141. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  51142. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  51143. };
  51144. WOLFSSL_BUFFER_INFO msg;
  51145. /* Offset into ClientHello message data of first cipher suite. */
  51146. const int csOff = 78;
  51147. /* Server cipher list. */
  51148. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  51149. /* Suite list with duplicates. */
  51150. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  51151. "TLS13-AES128-GCM-SHA256:"
  51152. "TLS13-AES256-GCM-SHA384:"
  51153. "TLS13-AES256-GCM-SHA384:"
  51154. "TLS13-AES128-GCM-SHA256";
  51155. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  51156. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  51157. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  51158. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  51159. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  51160. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  51161. #endif
  51162. /* Set up wolfSSL context. */
  51163. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  51164. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  51165. WOLFSSL_FILETYPE_PEM));
  51166. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  51167. WOLFSSL_FILETYPE_PEM));
  51168. /* Read from 'msg'. */
  51169. wolfSSL_SetIORecv(ctx, CsRecv);
  51170. /* No where to send to - dummy sender. */
  51171. wolfSSL_SetIOSend(ctx, CsSend);
  51172. /* Test cipher suite list with many copies of a cipher suite. */
  51173. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51174. msg.buffer = clientHello;
  51175. msg.length = (unsigned int)sizeof(clientHello);
  51176. wolfSSL_SetIOReadCtx(ssl, &msg);
  51177. /* Force server to have as many occurrences of same cipher suite as
  51178. * possible. */
  51179. if (ssl != NULL) {
  51180. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  51181. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  51182. for (i = 0; i < suites->suiteSz; i += 2) {
  51183. suites->suites[i + 0] = TLS13_BYTE;
  51184. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  51185. }
  51186. }
  51187. /* Test multiple occurrences of same cipher suite. */
  51188. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51189. wolfSSL_free(ssl);
  51190. ssl = NULL;
  51191. /* Set client order opposite to server order:
  51192. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  51193. clientHello[csOff + 0] = TLS13_BYTE;
  51194. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  51195. clientHello[csOff + 2] = TLS13_BYTE;
  51196. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  51197. /* Test server order negotiation. */
  51198. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51199. msg.buffer = clientHello;
  51200. msg.length = (unsigned int)sizeof(clientHello);
  51201. wolfSSL_SetIOReadCtx(ssl, &msg);
  51202. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  51203. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  51204. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  51205. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51206. /* Check refined order - server order. */
  51207. ExpectIntEQ(ssl->suites->suiteSz, 4);
  51208. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  51209. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  51210. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  51211. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  51212. wolfSSL_free(ssl);
  51213. ssl = NULL;
  51214. /* Test client order negotiation. */
  51215. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51216. msg.buffer = clientHello;
  51217. msg.length = (unsigned int)sizeof(clientHello);
  51218. wolfSSL_SetIOReadCtx(ssl, &msg);
  51219. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  51220. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  51221. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  51222. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  51223. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  51224. /* Check refined order - client order. */
  51225. ExpectIntEQ(ssl->suites->suiteSz, 4);
  51226. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  51227. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  51228. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  51229. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  51230. wolfSSL_free(ssl);
  51231. ssl = NULL;
  51232. /* Check duplicate detection is working. */
  51233. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  51234. ExpectIntEQ(ctx->suites->suiteSz, 4);
  51235. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  51236. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  51237. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  51238. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  51239. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  51240. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  51241. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  51242. ExpectIntEQ(ctx->suites->suiteSz, 4);
  51243. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  51244. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  51245. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  51246. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  51247. #endif
  51248. wolfSSL_CTX_free(ctx);
  51249. #endif
  51250. return EXPECT_RESULT();
  51251. }
  51252. #endif
  51253. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  51254. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  51255. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  51256. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51257. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  51258. const unsigned char* priv, unsigned int privSz,
  51259. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  51260. unsigned char* out, unsigned int* outlen,
  51261. void* ctx)
  51262. {
  51263. int result;
  51264. /* Test fail when context associated with WOLFSSL is NULL */
  51265. if (ctx == NULL) {
  51266. return -1;
  51267. }
  51268. (void)ssl;
  51269. /* return 0 on success */
  51270. PRIVATE_KEY_UNLOCK();
  51271. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  51272. PRIVATE_KEY_LOCK();
  51273. return result;
  51274. }
  51275. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  51276. EXPECT_DECLS;
  51277. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  51278. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  51279. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  51280. WOLFSSL_SUCCESS);
  51281. #endif
  51282. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  51283. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  51284. WOLFSSL_SUCCESS);
  51285. #endif
  51286. return EXPECT_RESULT();
  51287. }
  51288. static int test_dh_ssl_setup(WOLFSSL* ssl)
  51289. {
  51290. EXPECT_DECLS;
  51291. static int dh_test_ctx = 1;
  51292. int ret;
  51293. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  51294. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  51295. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  51296. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  51297. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51298. }
  51299. return EXPECT_RESULT();
  51300. }
  51301. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  51302. {
  51303. EXPECT_DECLS;
  51304. int ret;
  51305. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  51306. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  51307. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  51308. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  51309. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  51310. }
  51311. return EXPECT_RESULT();
  51312. }
  51313. #endif
  51314. static int test_DhCallbacks(void)
  51315. {
  51316. EXPECT_DECLS;
  51317. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  51318. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  51319. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  51320. WOLFSSL_CTX *ctx = NULL;
  51321. WOLFSSL *ssl = NULL;
  51322. int test;
  51323. test_ssl_cbf func_cb_client;
  51324. test_ssl_cbf func_cb_server;
  51325. /* Test that DH callback APIs work. */
  51326. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  51327. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  51328. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  51329. /* load client ca cert */
  51330. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  51331. WOLFSSL_SUCCESS);
  51332. /* test with NULL arguments */
  51333. wolfSSL_SetDhAgreeCtx(NULL, &test);
  51334. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  51335. /* test success case */
  51336. test = 1;
  51337. ExpectNotNull(ssl = wolfSSL_new(ctx));
  51338. wolfSSL_SetDhAgreeCtx(ssl, &test);
  51339. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  51340. wolfSSL_free(ssl);
  51341. wolfSSL_CTX_free(ctx);
  51342. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51343. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51344. /* set callbacks to use DH functions */
  51345. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  51346. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  51347. func_cb_client.method = wolfTLSv1_2_client_method;
  51348. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  51349. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  51350. func_cb_server.method = wolfTLSv1_2_server_method;
  51351. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51352. &func_cb_server, NULL), TEST_SUCCESS);
  51353. /* Test fail */
  51354. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51355. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51356. /* set callbacks to use DH functions */
  51357. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  51358. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  51359. func_cb_client.method = wolfTLSv1_2_client_method;
  51360. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  51361. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  51362. func_cb_server.method = wolfTLSv1_2_server_method;
  51363. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51364. &func_cb_server, NULL), TEST_FAIL);
  51365. #endif
  51366. return EXPECT_RESULT();
  51367. }
  51368. #endif /* HAVE_PK_CALLBACKS */
  51369. #ifdef HAVE_HASHDRBG
  51370. #ifdef TEST_RESEED_INTERVAL
  51371. static int test_wc_RNG_GenerateBlock_Reseed(void)
  51372. {
  51373. EXPECT_DECLS;
  51374. int i;
  51375. WC_RNG rng;
  51376. byte key[32];
  51377. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51378. ExpectIntEQ(wc_InitRng(&rng), 0);
  51379. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  51380. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  51381. }
  51382. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51383. return EXPECT_RESULT();
  51384. }
  51385. #endif /* TEST_RESEED_INTERVAL */
  51386. static int test_wc_RNG_GenerateBlock(void)
  51387. {
  51388. EXPECT_DECLS;
  51389. int i;
  51390. WC_RNG rng;
  51391. byte key[32];
  51392. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51393. ExpectIntEQ(wc_InitRng(&rng), 0);
  51394. for (i = 0; i < 10; i++) {
  51395. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  51396. }
  51397. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51398. return EXPECT_RESULT();
  51399. }
  51400. #endif /* HAVE_HASHDRBG */
  51401. /*
  51402. * Testing get_rand_digit
  51403. */
  51404. static int test_get_rand_digit(void)
  51405. {
  51406. EXPECT_DECLS;
  51407. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  51408. WC_RNG rng;
  51409. mp_digit d;
  51410. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51411. ExpectIntEQ(wc_InitRng(&rng), 0);
  51412. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  51413. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  51414. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  51415. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  51416. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51417. #endif
  51418. return EXPECT_RESULT();
  51419. } /* End test_get_rand_digit*/
  51420. /*
  51421. * Testing get_digit_count
  51422. */
  51423. static int test_get_digit_count(void)
  51424. {
  51425. EXPECT_DECLS;
  51426. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  51427. mp_int a;
  51428. XMEMSET(&a, 0, sizeof(mp_int));
  51429. ExpectIntEQ(mp_init(&a), 0);
  51430. ExpectIntEQ(get_digit_count(NULL), 0);
  51431. ExpectIntEQ(get_digit_count(&a), 0);
  51432. mp_clear(&a);
  51433. #endif
  51434. return EXPECT_RESULT();
  51435. } /* End test_get_digit_count*/
  51436. /*
  51437. * Testing mp_cond_copy
  51438. */
  51439. static int test_mp_cond_copy(void)
  51440. {
  51441. EXPECT_DECLS;
  51442. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  51443. defined(WOLFSSL_PUBLIC_MP)
  51444. mp_int a;
  51445. mp_int b;
  51446. int copy = 0;
  51447. XMEMSET(&a, 0, sizeof(mp_int));
  51448. XMEMSET(&b, 0, sizeof(mp_int));
  51449. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51450. ExpectIntEQ(mp_init(&b), MP_OKAY);
  51451. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  51452. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  51453. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  51454. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  51455. mp_clear(&a);
  51456. mp_clear(&b);
  51457. #endif
  51458. return EXPECT_RESULT();
  51459. } /* End test_mp_cond_copy*/
  51460. /*
  51461. * Testing mp_rand
  51462. */
  51463. static int test_mp_rand(void)
  51464. {
  51465. EXPECT_DECLS;
  51466. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  51467. mp_int a;
  51468. WC_RNG rng;
  51469. int digits = 1;
  51470. XMEMSET(&a, 0, sizeof(mp_int));
  51471. XMEMSET(&rng, 0, sizeof(WC_RNG));
  51472. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51473. ExpectIntEQ(wc_InitRng(&rng), 0);
  51474. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  51475. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  51476. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  51477. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  51478. mp_clear(&a);
  51479. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  51480. #endif
  51481. return EXPECT_RESULT();
  51482. } /* End test_mp_rand*/
  51483. /*
  51484. * Testing get_digit
  51485. */
  51486. static int test_get_digit(void)
  51487. {
  51488. EXPECT_DECLS;
  51489. #if defined(WOLFSSL_PUBLIC_MP)
  51490. mp_int a;
  51491. int n = 0;
  51492. XMEMSET(&a, 0, sizeof(mp_int));
  51493. ExpectIntEQ(mp_init(&a), MP_OKAY);
  51494. ExpectIntEQ(get_digit(NULL, n), 0);
  51495. ExpectIntEQ(get_digit(&a, n), 0);
  51496. mp_clear(&a);
  51497. #endif
  51498. return EXPECT_RESULT();
  51499. } /* End test_get_digit*/
  51500. /*
  51501. * Testing wc_export_int
  51502. */
  51503. static int test_wc_export_int(void)
  51504. {
  51505. EXPECT_DECLS;
  51506. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  51507. defined(WOLFSSL_PUBLIC_MP)
  51508. mp_int mp;
  51509. byte buf[32];
  51510. word32 keySz = (word32)sizeof(buf);
  51511. word32 len = (word32)sizeof(buf);
  51512. XMEMSET(&mp, 0, sizeof(mp_int));
  51513. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  51514. ExpectIntEQ(mp_set(&mp, 1234), 0);
  51515. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  51516. BAD_FUNC_ARG);
  51517. len = sizeof(buf)-1;
  51518. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  51519. BUFFER_E);
  51520. len = sizeof(buf);
  51521. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  51522. len = 4; /* test input too small */
  51523. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  51524. len = sizeof(buf);
  51525. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  51526. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  51527. ExpectIntEQ(len, 5);
  51528. mp_clear(&mp);
  51529. #endif
  51530. return EXPECT_RESULT();
  51531. } /* End test_wc_export_int*/
  51532. static int test_wc_InitRngNonce(void)
  51533. {
  51534. EXPECT_DECLS;
  51535. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  51536. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  51537. HAVE_FIPS_VERSION >= 2))
  51538. WC_RNG rng;
  51539. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  51540. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  51541. word32 nonceSz = sizeof(nonce);
  51542. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  51543. ExpectIntEQ(wc_FreeRng(&rng), 0);
  51544. #endif
  51545. return EXPECT_RESULT();
  51546. } /* End test_wc_InitRngNonce*/
  51547. /*
  51548. * Testing wc_InitRngNonce_ex
  51549. */
  51550. static int test_wc_InitRngNonce_ex(void)
  51551. {
  51552. EXPECT_DECLS;
  51553. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  51554. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  51555. HAVE_FIPS_VERSION >= 2))
  51556. WC_RNG rng;
  51557. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  51558. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  51559. word32 nonceSz = sizeof(nonce);
  51560. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  51561. 0);
  51562. ExpectIntEQ(wc_FreeRng(&rng), 0);
  51563. #endif
  51564. return EXPECT_RESULT();
  51565. } /* End test_wc_InitRngNonce_ex */
  51566. static int test_wolfSSL_X509_CRL(void)
  51567. {
  51568. EXPECT_DECLS;
  51569. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  51570. X509_CRL *crl = NULL;
  51571. char pem[][100] = {
  51572. "./certs/crl/crl.pem",
  51573. "./certs/crl/crl2.pem",
  51574. "./certs/crl/caEccCrl.pem",
  51575. "./certs/crl/eccCliCRL.pem",
  51576. "./certs/crl/eccSrvCRL.pem",
  51577. ""
  51578. };
  51579. #ifndef NO_BIO
  51580. BIO *bio = NULL;
  51581. #endif
  51582. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  51583. char der[][100] = {
  51584. "./certs/crl/crl.der",
  51585. "./certs/crl/crl2.der",
  51586. ""};
  51587. #endif
  51588. XFILE fp = XBADFILE;
  51589. int i;
  51590. for (i = 0; pem[i][0] != '\0'; i++)
  51591. {
  51592. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  51593. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51594. NULL, NULL));
  51595. ExpectNotNull(crl);
  51596. X509_CRL_free(crl);
  51597. if (fp != XBADFILE) {
  51598. XFCLOSE(fp);
  51599. fp = XBADFILE;
  51600. }
  51601. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  51602. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  51603. NULL));
  51604. if (EXPECT_FAIL()) {
  51605. crl = NULL;
  51606. }
  51607. ExpectNotNull(crl);
  51608. X509_CRL_free(crl);
  51609. crl = NULL;
  51610. if (fp != XBADFILE) {
  51611. XFCLOSE(fp);
  51612. fp = XBADFILE;
  51613. }
  51614. }
  51615. #ifndef NO_BIO
  51616. for (i = 0; pem[i][0] != '\0'; i++)
  51617. {
  51618. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  51619. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  51620. X509_CRL_free(crl);
  51621. crl = NULL;
  51622. BIO_free(bio);
  51623. bio = NULL;
  51624. }
  51625. #endif
  51626. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  51627. for (i = 0; der[i][0] != '\0'; i++) {
  51628. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  51629. ExpectTrue((fp != XBADFILE));
  51630. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  51631. ExpectNotNull(crl);
  51632. X509_CRL_free(crl);
  51633. if (fp != XBADFILE) {
  51634. XFCLOSE(fp);
  51635. fp = XBADFILE;
  51636. }
  51637. fp = XFOPEN(der[i], "rb");
  51638. ExpectTrue((fp != XBADFILE));
  51639. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  51640. if (EXPECT_FAIL()) {
  51641. crl = NULL;
  51642. }
  51643. ExpectNotNull(crl);
  51644. X509_CRL_free(crl);
  51645. crl = NULL;
  51646. if (fp != XBADFILE) {
  51647. XFCLOSE(fp);
  51648. fp = XBADFILE;
  51649. }
  51650. }
  51651. #endif
  51652. #endif
  51653. return EXPECT_RESULT();
  51654. }
  51655. static int test_wolfSSL_X509_load_crl_file(void)
  51656. {
  51657. EXPECT_DECLS;
  51658. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  51659. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  51660. int i;
  51661. char pem[][100] = {
  51662. "./certs/crl/crl.pem",
  51663. "./certs/crl/crl2.pem",
  51664. "./certs/crl/caEccCrl.pem",
  51665. "./certs/crl/eccCliCRL.pem",
  51666. "./certs/crl/eccSrvCRL.pem",
  51667. #ifdef WC_RSA_PSS
  51668. "./certs/crl/crl_rsapss.pem",
  51669. #endif
  51670. ""
  51671. };
  51672. char der[][100] = {
  51673. "./certs/crl/crl.der",
  51674. "./certs/crl/crl2.der",
  51675. ""
  51676. };
  51677. WOLFSSL_X509_STORE* store = NULL;
  51678. WOLFSSL_X509_LOOKUP* lookup = NULL;
  51679. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  51680. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  51681. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  51682. X509_FILETYPE_PEM), 1);
  51683. #ifdef WC_RSA_PSS
  51684. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  51685. X509_FILETYPE_PEM), 1);
  51686. #endif
  51687. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  51688. X509_FILETYPE_PEM), 1);
  51689. if (store) {
  51690. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  51691. WOLFSSL_FILETYPE_PEM), 1);
  51692. /* since store hasn't yet known the revoked cert*/
  51693. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51694. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  51695. }
  51696. for (i = 0; pem[i][0] != '\0'; i++) {
  51697. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  51698. 1);
  51699. }
  51700. if (store) {
  51701. /* since store knows crl list */
  51702. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51703. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  51704. CRL_CERT_REVOKED);
  51705. #ifdef WC_RSA_PSS
  51706. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51707. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  51708. CRL_CERT_REVOKED);
  51709. #endif
  51710. }
  51711. /* once feeing store */
  51712. X509_STORE_free(store);
  51713. store = NULL;
  51714. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  51715. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  51716. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  51717. X509_FILETYPE_PEM), 1);
  51718. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  51719. X509_FILETYPE_PEM), 1);
  51720. if (store) {
  51721. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  51722. WOLFSSL_FILETYPE_PEM), 1);
  51723. /* since store hasn't yet known the revoked cert*/
  51724. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51725. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  51726. }
  51727. for (i = 0; der[i][0] != '\0'; i++) {
  51728. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  51729. 1);
  51730. }
  51731. if (store) {
  51732. /* since store knows crl list */
  51733. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  51734. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  51735. CRL_CERT_REVOKED);
  51736. }
  51737. /* test for incorrect parameter */
  51738. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  51739. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  51740. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  51741. X509_STORE_free(store);
  51742. store = NULL;
  51743. #endif
  51744. return EXPECT_RESULT();
  51745. }
  51746. static int test_wolfSSL_i2d_X509(void)
  51747. {
  51748. EXPECT_DECLS;
  51749. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  51750. const unsigned char* cert_buf = server_cert_der_2048;
  51751. unsigned char* out = NULL;
  51752. unsigned char* tmp = NULL;
  51753. X509* cert = NULL;
  51754. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  51755. /* Pointer should be advanced */
  51756. ExpectPtrGT(cert_buf, server_cert_der_2048);
  51757. ExpectIntGT(i2d_X509(cert, &out), 0);
  51758. ExpectNotNull(out);
  51759. tmp = out;
  51760. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  51761. ExpectPtrGT(tmp, out);
  51762. if (out != NULL)
  51763. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  51764. X509_free(cert);
  51765. #endif
  51766. return EXPECT_RESULT();
  51767. }
  51768. static int test_wolfSSL_d2i_X509_REQ(void)
  51769. {
  51770. EXPECT_DECLS;
  51771. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  51772. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  51773. !defined(WOLFSSL_SP_MATH)
  51774. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  51775. * were generated by libest
  51776. * ./certs/csr.attr.der contains sample attributes
  51777. * ./certs/csr.ext.der contains sample extensions */
  51778. const char* csrFile = "./certs/csr.signed.der";
  51779. const char* csrPopFile = "./certs/csr.attr.der";
  51780. const char* csrExtFile = "./certs/csr.ext.der";
  51781. /* ./certs/csr.dsa.pem is generated using
  51782. * openssl req -newkey dsa:certs/dsaparams.pem \
  51783. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  51784. * -outform PEM
  51785. * with the passphrase "wolfSSL"
  51786. */
  51787. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  51788. const char* csrDsaFile = "./certs/csr.dsa.pem";
  51789. XFILE f = XBADFILE;
  51790. #endif
  51791. BIO* bio = NULL;
  51792. X509* req = NULL;
  51793. EVP_PKEY *pub_key = NULL;
  51794. {
  51795. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  51796. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51797. /*
  51798. * Extract the public key from the CSR
  51799. */
  51800. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51801. /*
  51802. * Verify the signature in the CSR
  51803. */
  51804. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51805. X509_free(req);
  51806. req = NULL;
  51807. BIO_free(bio);
  51808. bio = NULL;
  51809. EVP_PKEY_free(pub_key);
  51810. pub_key = NULL;
  51811. }
  51812. {
  51813. #ifdef OPENSSL_ALL
  51814. X509_ATTRIBUTE* attr = NULL;
  51815. ASN1_TYPE *at = NULL;
  51816. #endif
  51817. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  51818. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51819. /*
  51820. * Extract the public key from the CSR
  51821. */
  51822. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51823. /*
  51824. * Verify the signature in the CSR
  51825. */
  51826. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51827. #ifdef OPENSSL_ALL
  51828. /*
  51829. * Obtain the challenge password from the CSR
  51830. */
  51831. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  51832. -1), 1);
  51833. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  51834. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  51835. ExpectNotNull(at->value.asn1_string);
  51836. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  51837. "2xIE+qqp/rhyTXP+");
  51838. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  51839. #endif
  51840. X509_free(req);
  51841. req = NULL;
  51842. BIO_free(bio);
  51843. bio = NULL;
  51844. EVP_PKEY_free(pub_key);
  51845. pub_key = NULL;
  51846. }
  51847. {
  51848. #ifdef OPENSSL_ALL
  51849. X509_ATTRIBUTE* attr = NULL;
  51850. ASN1_TYPE *at = NULL;
  51851. STACK_OF(X509_EXTENSION) *exts = NULL;
  51852. #endif
  51853. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  51854. /* This CSR contains an Extension Request attribute so
  51855. * we test extension parsing in a CSR attribute here. */
  51856. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  51857. /*
  51858. * Extract the public key from the CSR
  51859. */
  51860. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51861. /*
  51862. * Verify the signature in the CSR
  51863. */
  51864. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51865. #ifdef OPENSSL_ALL
  51866. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  51867. req));
  51868. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  51869. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  51870. /*
  51871. * Obtain the challenge password from the CSR
  51872. */
  51873. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  51874. -1), 0);
  51875. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  51876. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  51877. ExpectNotNull(at->value.asn1_string);
  51878. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  51879. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  51880. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  51881. #endif
  51882. X509_free(req);
  51883. req = NULL;
  51884. BIO_free(bio);
  51885. bio = NULL;
  51886. EVP_PKEY_free(pub_key);
  51887. pub_key = NULL;
  51888. }
  51889. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  51890. {
  51891. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  51892. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  51893. /*
  51894. * Extract the public key from the CSR
  51895. */
  51896. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  51897. /*
  51898. * Verify the signature in the CSR
  51899. */
  51900. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51901. X509_free(req);
  51902. req = NULL;
  51903. BIO_free(bio);
  51904. /* Run the same test, but with a file pointer instead of a BIO.
  51905. * (PEM_read_X509_REQ)*/
  51906. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  51907. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  51908. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  51909. X509_free(req);
  51910. EVP_PKEY_free(pub_key);
  51911. }
  51912. #endif /* !NO_DSA && !HAVE_SELFTEST */
  51913. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  51914. return EXPECT_RESULT();
  51915. }
  51916. static int test_wolfSSL_PEM_read_X509(void)
  51917. {
  51918. EXPECT_DECLS;
  51919. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  51920. !defined(NO_RSA)
  51921. X509 *x509 = NULL;
  51922. XFILE fp = XBADFILE;
  51923. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  51924. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  51925. X509_free(x509);
  51926. if (fp != XBADFILE)
  51927. XFCLOSE(fp);
  51928. #endif
  51929. return EXPECT_RESULT();
  51930. }
  51931. static int test_wolfSSL_PEM_read(void)
  51932. {
  51933. EXPECT_DECLS;
  51934. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  51935. const char* filename = "./certs/server-keyEnc.pem";
  51936. XFILE fp = XBADFILE;
  51937. char* name = NULL;
  51938. char* header = NULL;
  51939. byte* data = NULL;
  51940. long len;
  51941. EVP_CIPHER_INFO cipher;
  51942. WOLFSSL_BIO* bio = NULL;
  51943. byte* fileData = NULL;
  51944. size_t fileDataSz = 0;
  51945. byte* out;
  51946. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  51947. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  51948. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  51949. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  51950. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  51951. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  51952. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  51953. ExpectIntGT(XSTRLEN(header), 0);
  51954. ExpectIntGT(len, 0);
  51955. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51956. name = NULL;
  51957. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51958. header = NULL;
  51959. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51960. data = NULL;
  51961. BIO_free(bio);
  51962. bio = NULL;
  51963. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  51964. /* Fail cases. */
  51965. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  51966. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  51967. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  51968. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  51969. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  51970. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  51971. ExpectIntGT(XSTRLEN(header), 0);
  51972. ExpectIntGT(len, 0);
  51973. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  51974. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  51975. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  51976. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  51977. DYNAMIC_TYPE_TMP_BUFFER));
  51978. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  51979. if (fp != XBADFILE) {
  51980. XFCLOSE(fp);
  51981. fp = XBADFILE;
  51982. }
  51983. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51984. /* Fail cases. */
  51985. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  51986. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  51987. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  51988. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  51989. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  51990. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  51991. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  51992. /* Fail cases. */
  51993. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  51994. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  51995. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  51996. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  51997. /* Pass case */
  51998. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  51999. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52000. name = NULL;
  52001. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52002. header = NULL;
  52003. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52004. data = NULL;
  52005. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  52006. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  52007. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  52008. ExpectIntGT(XSTRLEN(header), 0);
  52009. ExpectIntGT(len, 0);
  52010. /* Fail cases. */
  52011. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  52012. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  52013. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  52014. #ifndef NO_DES3
  52015. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  52016. #endif
  52017. /* Fail cases. */
  52018. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  52019. (void*)"yassl123"), WOLFSSL_FAILURE);
  52020. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  52021. (void*)"yassl123"), WOLFSSL_FAILURE);
  52022. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  52023. (void*)"yassl123"), WOLFSSL_FAILURE);
  52024. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  52025. (void*)"yassl123"), WOLFSSL_FAILURE);
  52026. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  52027. (void*)"yassl123"), WOLFSSL_FAILURE);
  52028. #if !defined(NO_DES3) && !defined(NO_MD5)
  52029. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  52030. (void*)"yassl123"), WOLFSSL_SUCCESS);
  52031. #else
  52032. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  52033. (void*)"yassl123"), WOLFSSL_FAILURE);
  52034. #endif
  52035. BIO_free(bio);
  52036. bio = NULL;
  52037. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52038. fileData = NULL;
  52039. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52040. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52041. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52042. name = NULL;
  52043. header = NULL;
  52044. data = NULL;
  52045. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  52046. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  52047. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  52048. ExpectIntEQ(XSTRLEN(header), 0);
  52049. ExpectIntGT(len, 0);
  52050. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  52051. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  52052. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  52053. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  52054. DYNAMIC_TYPE_TMP_BUFFER));
  52055. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  52056. if (fp != XBADFILE)
  52057. XFCLOSE(fp);
  52058. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  52059. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  52060. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  52061. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  52062. BIO_free(bio);
  52063. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52064. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52065. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52066. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52067. #endif
  52068. return EXPECT_RESULT();
  52069. }
  52070. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  52071. {
  52072. EXPECT_DECLS;
  52073. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52074. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52075. const byte iv[12] = { 0 };
  52076. const byte key[16] = { 0 };
  52077. const byte cleartext[16] = { 0 };
  52078. const byte aad[] = {
  52079. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  52080. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  52081. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  52082. 0x4f
  52083. };
  52084. byte out1Part[16];
  52085. byte outTag1Part[16];
  52086. byte out2Part[16];
  52087. byte outTag2Part[16];
  52088. byte decryptBuf[16];
  52089. int len = 0;
  52090. int tlen;
  52091. EVP_CIPHER_CTX* ctx = NULL;
  52092. /* ENCRYPT */
  52093. /* Send AAD and data in 1 part */
  52094. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52095. tlen = 0;
  52096. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52097. 1);
  52098. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52099. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  52100. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  52101. sizeof(cleartext)), 1);
  52102. tlen += len;
  52103. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  52104. tlen += len;
  52105. ExpectIntEQ(tlen, sizeof(cleartext));
  52106. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  52107. outTag1Part), 1);
  52108. EVP_CIPHER_CTX_free(ctx);
  52109. ctx = NULL;
  52110. /* DECRYPT */
  52111. /* Send AAD and data in 1 part */
  52112. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52113. tlen = 0;
  52114. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52115. 1);
  52116. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52117. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  52118. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  52119. sizeof(cleartext)), 1);
  52120. tlen += len;
  52121. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  52122. outTag1Part), 1);
  52123. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  52124. tlen += len;
  52125. ExpectIntEQ(tlen, sizeof(cleartext));
  52126. EVP_CIPHER_CTX_free(ctx);
  52127. ctx = NULL;
  52128. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  52129. /* ENCRYPT */
  52130. /* Send AAD and data in 2 parts */
  52131. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52132. tlen = 0;
  52133. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52134. 1);
  52135. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52136. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  52137. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  52138. 1);
  52139. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  52140. tlen += len;
  52141. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  52142. sizeof(cleartext) - 1), 1);
  52143. tlen += len;
  52144. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  52145. tlen += len;
  52146. ExpectIntEQ(tlen, sizeof(cleartext));
  52147. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  52148. outTag2Part), 1);
  52149. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  52150. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  52151. EVP_CIPHER_CTX_free(ctx);
  52152. ctx = NULL;
  52153. /* DECRYPT */
  52154. /* Send AAD and data in 2 parts */
  52155. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  52156. tlen = 0;
  52157. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  52158. 1);
  52159. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  52160. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  52161. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  52162. 1);
  52163. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  52164. tlen += len;
  52165. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  52166. sizeof(cleartext) - 1), 1);
  52167. tlen += len;
  52168. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  52169. outTag1Part), 1);
  52170. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  52171. tlen += len;
  52172. ExpectIntEQ(tlen, sizeof(cleartext));
  52173. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  52174. /* Test AAD reuse */
  52175. EVP_CIPHER_CTX_free(ctx);
  52176. #endif
  52177. return EXPECT_RESULT();
  52178. }
  52179. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  52180. {
  52181. EXPECT_DECLS;
  52182. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52183. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52184. /* Zero length plain text */
  52185. byte key[] = {
  52186. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52187. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52188. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52189. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52190. }; /* align */
  52191. byte iv[] = {
  52192. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52193. }; /* align */
  52194. byte plaintxt[1];
  52195. int ivSz = 12;
  52196. int plaintxtSz = 0;
  52197. unsigned char tag[16];
  52198. unsigned char tag_kat[] = {
  52199. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  52200. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  52201. };
  52202. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52203. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52204. int ciphertxtSz = 0;
  52205. int decryptedtxtSz = 0;
  52206. int len = 0;
  52207. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  52208. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  52209. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  52210. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52211. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  52212. plaintxtSz));
  52213. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  52214. ciphertxtSz += len;
  52215. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  52216. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  52217. ExpectIntEQ(0, ciphertxtSz);
  52218. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  52219. EVP_CIPHER_CTX_init(de);
  52220. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  52221. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52222. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  52223. decryptedtxtSz = len;
  52224. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  52225. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  52226. decryptedtxtSz += len;
  52227. ExpectIntEQ(0, decryptedtxtSz);
  52228. EVP_CIPHER_CTX_free(en);
  52229. EVP_CIPHER_CTX_free(de);
  52230. #endif
  52231. return EXPECT_RESULT();
  52232. }
  52233. static int test_wolfssl_EVP_aes_gcm(void)
  52234. {
  52235. EXPECT_DECLS;
  52236. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  52237. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52238. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52239. byte *key = (byte*)"01234567890123456789012345678901";
  52240. /* A 128 bit IV */
  52241. byte *iv = (byte*)"0123456789012345";
  52242. int ivSz = AES_BLOCK_SIZE;
  52243. /* Message to be encrypted */
  52244. byte *plaintxt = (byte*)"for things to change you have to change";
  52245. /* Additional non-confidential data */
  52246. byte *aad = (byte*)"Don't spend major time on minor things.";
  52247. unsigned char tag[AES_BLOCK_SIZE] = {0};
  52248. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  52249. int aadSz = (int)XSTRLEN((char*)aad);
  52250. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52251. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52252. int ciphertxtSz = 0;
  52253. int decryptedtxtSz = 0;
  52254. int len = 0;
  52255. int i = 0;
  52256. EVP_CIPHER_CTX en[2];
  52257. EVP_CIPHER_CTX de[2];
  52258. for (i = 0; i < 2; i++) {
  52259. EVP_CIPHER_CTX_init(&en[i]);
  52260. if (i == 0) {
  52261. /* Default uses 96-bits IV length */
  52262. #ifdef WOLFSSL_AES_128
  52263. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  52264. key, iv));
  52265. #elif defined(WOLFSSL_AES_192)
  52266. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  52267. key, iv));
  52268. #elif defined(WOLFSSL_AES_256)
  52269. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  52270. key, iv));
  52271. #endif
  52272. }
  52273. else {
  52274. #ifdef WOLFSSL_AES_128
  52275. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  52276. NULL, NULL));
  52277. #elif defined(WOLFSSL_AES_192)
  52278. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  52279. NULL, NULL));
  52280. #elif defined(WOLFSSL_AES_256)
  52281. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  52282. NULL, NULL));
  52283. #endif
  52284. /* non-default must to set the IV length first */
  52285. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  52286. ivSz, NULL));
  52287. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52288. }
  52289. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52290. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  52291. plaintxtSz));
  52292. ciphertxtSz = len;
  52293. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52294. ciphertxtSz += len;
  52295. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  52296. AES_BLOCK_SIZE, tag));
  52297. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  52298. EVP_CIPHER_CTX_init(&de[i]);
  52299. if (i == 0) {
  52300. /* Default uses 96-bits IV length */
  52301. #ifdef WOLFSSL_AES_128
  52302. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52303. key, iv));
  52304. #elif defined(WOLFSSL_AES_192)
  52305. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52306. key, iv));
  52307. #elif defined(WOLFSSL_AES_256)
  52308. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52309. key, iv));
  52310. #endif
  52311. }
  52312. else {
  52313. #ifdef WOLFSSL_AES_128
  52314. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52315. NULL, NULL));
  52316. #elif defined(WOLFSSL_AES_192)
  52317. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52318. NULL, NULL));
  52319. #elif defined(WOLFSSL_AES_256)
  52320. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52321. NULL, NULL));
  52322. #endif
  52323. /* non-default must to set the IV length first */
  52324. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  52325. ivSz, NULL));
  52326. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52327. }
  52328. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52329. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52330. ciphertxtSz));
  52331. decryptedtxtSz = len;
  52332. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  52333. AES_BLOCK_SIZE, tag));
  52334. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52335. decryptedtxtSz += len;
  52336. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  52337. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52338. /* modify tag*/
  52339. if (i == 0) {
  52340. /* Default uses 96-bits IV length */
  52341. #ifdef WOLFSSL_AES_128
  52342. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52343. key, iv));
  52344. #elif defined(WOLFSSL_AES_192)
  52345. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52346. key, iv));
  52347. #elif defined(WOLFSSL_AES_256)
  52348. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52349. key, iv));
  52350. #endif
  52351. }
  52352. else {
  52353. #ifdef WOLFSSL_AES_128
  52354. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  52355. NULL, NULL));
  52356. #elif defined(WOLFSSL_AES_192)
  52357. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  52358. NULL, NULL));
  52359. #elif defined(WOLFSSL_AES_256)
  52360. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  52361. NULL, NULL));
  52362. #endif
  52363. /* non-default must to set the IV length first */
  52364. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  52365. ivSz, NULL));
  52366. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52367. }
  52368. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52369. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52370. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  52371. AES_BLOCK_SIZE, tag));
  52372. /* fail due to wrong tag */
  52373. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52374. ciphertxtSz));
  52375. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52376. ExpectIntEQ(0, len);
  52377. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  52378. }
  52379. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  52380. return EXPECT_RESULT();
  52381. }
  52382. static int test_wolfssl_EVP_aria_gcm(void)
  52383. {
  52384. int res = TEST_SKIPPED;
  52385. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  52386. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52387. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52388. byte *key = (byte*)"01234567890123456789012345678901";
  52389. /* A 128 bit IV */
  52390. byte *iv = (byte*)"0123456789012345";
  52391. int ivSz = ARIA_BLOCK_SIZE;
  52392. /* Message to be encrypted */
  52393. const int plaintxtSz = 40;
  52394. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  52395. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  52396. /* Additional non-confidential data */
  52397. byte *aad = (byte*)"Don't spend major time on minor things.";
  52398. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  52399. int aadSz = (int)XSTRLEN((char*)aad);
  52400. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  52401. byte decryptedtxt[plaintxtSz];
  52402. int ciphertxtSz = 0;
  52403. int decryptedtxtSz = 0;
  52404. int len = 0;
  52405. int i = 0;
  52406. #define TEST_ARIA_GCM_COUNT 6
  52407. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  52408. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  52409. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  52410. EVP_CIPHER_CTX_init(&en[i]);
  52411. switch (i) {
  52412. case 0:
  52413. /* Default uses 96-bits IV length */
  52414. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  52415. break;
  52416. case 1:
  52417. /* Default uses 96-bits IV length */
  52418. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  52419. break;
  52420. case 2:
  52421. /* Default uses 96-bits IV length */
  52422. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  52423. break;
  52424. case 3:
  52425. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  52426. /* non-default must to set the IV length first */
  52427. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52428. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52429. break;
  52430. case 4:
  52431. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  52432. /* non-default must to set the IV length first */
  52433. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52434. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52435. break;
  52436. case 5:
  52437. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  52438. /* non-default must to set the IV length first */
  52439. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52440. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52441. break;
  52442. }
  52443. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  52444. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52445. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  52446. ciphertxtSz = len;
  52447. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52448. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  52449. ciphertxtSz += len;
  52450. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  52451. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  52452. EVP_CIPHER_CTX_init(&de[i]);
  52453. switch (i) {
  52454. case 0:
  52455. /* Default uses 96-bits IV length */
  52456. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  52457. break;
  52458. case 1:
  52459. /* Default uses 96-bits IV length */
  52460. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  52461. break;
  52462. case 2:
  52463. /* Default uses 96-bits IV length */
  52464. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  52465. break;
  52466. case 3:
  52467. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  52468. /* non-default must to set the IV length first */
  52469. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52470. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52471. break;
  52472. case 4:
  52473. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  52474. /* non-default must to set the IV length first */
  52475. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52476. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52477. break;
  52478. case 5:
  52479. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  52480. /* non-default must to set the IV length first */
  52481. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  52482. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52483. break;
  52484. }
  52485. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  52486. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52487. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  52488. decryptedtxtSz = len;
  52489. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  52490. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52491. decryptedtxtSz += len;
  52492. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  52493. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52494. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  52495. /* modify tag*/
  52496. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52497. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52498. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  52499. /* fail due to wrong tag */
  52500. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  52501. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52502. AssertIntEQ(0, len);
  52503. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  52504. }
  52505. res = TEST_RES_CHECK(1);
  52506. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  52507. return res;
  52508. }
  52509. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  52510. {
  52511. EXPECT_DECLS;
  52512. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  52513. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52514. /* Zero length plain text */
  52515. byte key[] = {
  52516. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52517. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52518. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  52519. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52520. }; /* align */
  52521. byte iv[] = {
  52522. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  52523. }; /* align */
  52524. byte plaintxt[1];
  52525. int ivSz = 12;
  52526. int plaintxtSz = 0;
  52527. unsigned char tag[16];
  52528. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52529. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52530. int ciphertxtSz = 0;
  52531. int decryptedtxtSz = 0;
  52532. int len = 0;
  52533. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  52534. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  52535. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  52536. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  52537. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  52538. plaintxtSz));
  52539. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  52540. ciphertxtSz += len;
  52541. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  52542. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  52543. ExpectIntEQ(0, ciphertxtSz);
  52544. EVP_CIPHER_CTX_init(de);
  52545. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  52546. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  52547. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  52548. decryptedtxtSz = len;
  52549. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  52550. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  52551. decryptedtxtSz += len;
  52552. ExpectIntEQ(0, decryptedtxtSz);
  52553. EVP_CIPHER_CTX_free(en);
  52554. EVP_CIPHER_CTX_free(de);
  52555. #endif
  52556. return EXPECT_RESULT();
  52557. }
  52558. static int test_wolfssl_EVP_aes_ccm(void)
  52559. {
  52560. EXPECT_DECLS;
  52561. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  52562. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  52563. /* A 256 bit key, AES_128 will use the first 128 bit*/
  52564. byte *key = (byte*)"01234567890123456789012345678901";
  52565. /* A 128 bit IV */
  52566. byte *iv = (byte*)"0123456789012";
  52567. int ivSz = (int)XSTRLEN((char*)iv);
  52568. /* Message to be encrypted */
  52569. byte *plaintxt = (byte*)"for things to change you have to change";
  52570. /* Additional non-confidential data */
  52571. byte *aad = (byte*)"Don't spend major time on minor things.";
  52572. unsigned char tag[AES_BLOCK_SIZE] = {0};
  52573. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  52574. int aadSz = (int)XSTRLEN((char*)aad);
  52575. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  52576. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  52577. int ciphertxtSz = 0;
  52578. int decryptedtxtSz = 0;
  52579. int len = 0;
  52580. int i = 0;
  52581. int ret;
  52582. EVP_CIPHER_CTX en[2];
  52583. EVP_CIPHER_CTX de[2];
  52584. for (i = 0; i < 2; i++) {
  52585. EVP_CIPHER_CTX_init(&en[i]);
  52586. if (i == 0) {
  52587. /* Default uses 96-bits IV length */
  52588. #ifdef WOLFSSL_AES_128
  52589. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  52590. key, iv));
  52591. #elif defined(WOLFSSL_AES_192)
  52592. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  52593. key, iv));
  52594. #elif defined(WOLFSSL_AES_256)
  52595. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  52596. key, iv));
  52597. #endif
  52598. }
  52599. else {
  52600. #ifdef WOLFSSL_AES_128
  52601. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  52602. NULL, NULL));
  52603. #elif defined(WOLFSSL_AES_192)
  52604. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  52605. NULL, NULL));
  52606. #elif defined(WOLFSSL_AES_256)
  52607. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  52608. NULL, NULL));
  52609. #endif
  52610. /* non-default must to set the IV length first */
  52611. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  52612. ivSz, NULL));
  52613. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  52614. }
  52615. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  52616. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  52617. plaintxtSz));
  52618. ciphertxtSz = len;
  52619. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  52620. ciphertxtSz += len;
  52621. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  52622. AES_BLOCK_SIZE, tag));
  52623. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  52624. ExpectIntEQ(ret, 1);
  52625. EVP_CIPHER_CTX_init(&de[i]);
  52626. if (i == 0) {
  52627. /* Default uses 96-bits IV length */
  52628. #ifdef WOLFSSL_AES_128
  52629. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  52630. key, iv));
  52631. #elif defined(WOLFSSL_AES_192)
  52632. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  52633. key, iv));
  52634. #elif defined(WOLFSSL_AES_256)
  52635. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  52636. key, iv));
  52637. #endif
  52638. }
  52639. else {
  52640. #ifdef WOLFSSL_AES_128
  52641. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  52642. NULL, NULL));
  52643. #elif defined(WOLFSSL_AES_192)
  52644. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  52645. NULL, NULL));
  52646. #elif defined(WOLFSSL_AES_256)
  52647. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  52648. NULL, NULL));
  52649. #endif
  52650. /* non-default must to set the IV length first */
  52651. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  52652. ivSz, NULL));
  52653. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  52654. }
  52655. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52656. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52657. ciphertxtSz));
  52658. decryptedtxtSz = len;
  52659. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  52660. AES_BLOCK_SIZE, tag));
  52661. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52662. decryptedtxtSz += len;
  52663. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  52664. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  52665. /* modify tag*/
  52666. tag[AES_BLOCK_SIZE-1]+=0xBB;
  52667. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  52668. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  52669. AES_BLOCK_SIZE, tag));
  52670. /* fail due to wrong tag */
  52671. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  52672. ciphertxtSz));
  52673. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  52674. ExpectIntEQ(0, len);
  52675. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  52676. ExpectIntEQ(ret, 1);
  52677. }
  52678. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  52679. return EXPECT_RESULT();
  52680. }
  52681. static int test_wolfssl_EVP_chacha20_poly1305(void)
  52682. {
  52683. EXPECT_DECLS;
  52684. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  52685. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  52686. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  52687. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52688. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  52689. byte cipherText[sizeof(plainText)];
  52690. byte decryptedText[sizeof(plainText)];
  52691. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  52692. EVP_CIPHER_CTX* ctx = NULL;
  52693. int outSz;
  52694. /* Encrypt. */
  52695. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52696. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  52697. NULL), WOLFSSL_SUCCESS);
  52698. /* Invalid IV length. */
  52699. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52700. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  52701. /* Valid IV length. */
  52702. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52703. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  52704. /* Invalid tag length. */
  52705. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52706. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  52707. /* Valid tag length. */
  52708. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52709. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  52710. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52711. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  52712. WOLFSSL_SUCCESS);
  52713. ExpectIntEQ(outSz, sizeof(aad));
  52714. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52715. sizeof(plainText)), WOLFSSL_SUCCESS);
  52716. ExpectIntEQ(outSz, sizeof(plainText));
  52717. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  52718. ExpectIntEQ(outSz, 0);
  52719. /* Invalid tag length. */
  52720. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  52721. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  52722. /* Valid tag length. */
  52723. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  52724. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  52725. EVP_CIPHER_CTX_free(ctx);
  52726. ctx = NULL;
  52727. /* Decrypt. */
  52728. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52729. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  52730. NULL), WOLFSSL_SUCCESS);
  52731. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  52732. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  52733. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52734. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  52735. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52736. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  52737. WOLFSSL_SUCCESS);
  52738. ExpectIntEQ(outSz, sizeof(aad));
  52739. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52740. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52741. ExpectIntEQ(outSz, sizeof(cipherText));
  52742. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52743. WOLFSSL_SUCCESS);
  52744. ExpectIntEQ(outSz, 0);
  52745. EVP_CIPHER_CTX_free(ctx);
  52746. ctx = NULL;
  52747. /* Test partial Inits. CipherInit() allow setting of key and iv
  52748. * in separate calls. */
  52749. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52750. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  52751. key, NULL, 1), WOLFSSL_SUCCESS);
  52752. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  52753. WOLFSSL_SUCCESS);
  52754. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  52755. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  52756. ExpectIntEQ(outSz, sizeof(aad));
  52757. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52758. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52759. ExpectIntEQ(outSz, sizeof(cipherText));
  52760. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52761. WOLFSSL_SUCCESS);
  52762. ExpectIntEQ(outSz, 0);
  52763. EVP_CIPHER_CTX_free(ctx);
  52764. #endif
  52765. return EXPECT_RESULT();
  52766. }
  52767. static int test_wolfssl_EVP_chacha20(void)
  52768. {
  52769. EXPECT_DECLS;
  52770. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  52771. byte key[CHACHA_MAX_KEY_SZ];
  52772. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  52773. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52774. byte cipherText[sizeof(plainText)];
  52775. byte decryptedText[sizeof(plainText)];
  52776. EVP_CIPHER_CTX* ctx = NULL;
  52777. int outSz;
  52778. XMEMSET(key, 0, sizeof(key));
  52779. XMEMSET(iv, 0, sizeof(iv));
  52780. /* Encrypt. */
  52781. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52782. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  52783. NULL), WOLFSSL_SUCCESS);
  52784. /* Any tag length must fail - not an AEAD cipher. */
  52785. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  52786. 16, NULL), WOLFSSL_FAILURE);
  52787. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52788. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52789. sizeof(plainText)), WOLFSSL_SUCCESS);
  52790. ExpectIntEQ(outSz, sizeof(plainText));
  52791. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  52792. ExpectIntEQ(outSz, 0);
  52793. EVP_CIPHER_CTX_free(ctx);
  52794. ctx = NULL;
  52795. /* Decrypt. */
  52796. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52797. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  52798. NULL), WOLFSSL_SUCCESS);
  52799. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52800. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52801. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52802. ExpectIntEQ(outSz, sizeof(cipherText));
  52803. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52804. WOLFSSL_SUCCESS);
  52805. ExpectIntEQ(outSz, 0);
  52806. EVP_CIPHER_CTX_free(ctx);
  52807. ctx = NULL;
  52808. /* Test partial Inits. CipherInit() allow setting of key and iv
  52809. * in separate calls. */
  52810. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52811. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  52812. key, NULL, 1), WOLFSSL_SUCCESS);
  52813. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  52814. WOLFSSL_SUCCESS);
  52815. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52816. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52817. ExpectIntEQ(outSz, sizeof(cipherText));
  52818. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52819. WOLFSSL_SUCCESS);
  52820. ExpectIntEQ(outSz, 0);
  52821. EVP_CIPHER_CTX_free(ctx);
  52822. #endif
  52823. return EXPECT_RESULT();
  52824. }
  52825. static int test_wolfssl_EVP_sm4_ecb(void)
  52826. {
  52827. int res = TEST_SKIPPED;
  52828. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  52829. EXPECT_DECLS;
  52830. byte key[SM4_KEY_SIZE];
  52831. byte plainText[SM4_BLOCK_SIZE] = {
  52832. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  52833. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  52834. };
  52835. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52836. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52837. EVP_CIPHER_CTX* ctx;
  52838. int outSz;
  52839. XMEMSET(key, 0, sizeof(key));
  52840. /* Encrypt. */
  52841. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52842. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  52843. WOLFSSL_SUCCESS);
  52844. /* Any tag length must fail - not an AEAD cipher. */
  52845. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52846. WOLFSSL_FAILURE);
  52847. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  52848. WOLFSSL_SUCCESS);
  52849. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52850. sizeof(plainText)), WOLFSSL_SUCCESS);
  52851. ExpectIntEQ(outSz, sizeof(plainText));
  52852. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  52853. WOLFSSL_SUCCESS);
  52854. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  52855. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  52856. EVP_CIPHER_CTX_free(ctx);
  52857. /* Decrypt. */
  52858. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52859. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  52860. WOLFSSL_SUCCESS);
  52861. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  52862. WOLFSSL_SUCCESS);
  52863. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52864. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52865. ExpectIntEQ(outSz, sizeof(plainText));
  52866. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52867. WOLFSSL_SUCCESS);
  52868. ExpectIntEQ(outSz, 0);
  52869. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52870. EVP_CIPHER_CTX_free(ctx);
  52871. res = EXPECT_RESULT();
  52872. #endif
  52873. return res;
  52874. }
  52875. static int test_wolfssl_EVP_sm4_cbc(void)
  52876. {
  52877. int res = TEST_SKIPPED;
  52878. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  52879. EXPECT_DECLS;
  52880. byte key[SM4_KEY_SIZE];
  52881. byte iv[SM4_BLOCK_SIZE];
  52882. byte plainText[SM4_BLOCK_SIZE] = {
  52883. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  52884. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  52885. };
  52886. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52887. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  52888. EVP_CIPHER_CTX* ctx;
  52889. int outSz;
  52890. XMEMSET(key, 0, sizeof(key));
  52891. XMEMSET(iv, 0, sizeof(iv));
  52892. /* Encrypt. */
  52893. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52894. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  52895. WOLFSSL_SUCCESS);
  52896. /* Any tag length must fail - not an AEAD cipher. */
  52897. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52898. WOLFSSL_FAILURE);
  52899. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52900. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52901. sizeof(plainText)), WOLFSSL_SUCCESS);
  52902. ExpectIntEQ(outSz, sizeof(plainText));
  52903. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  52904. WOLFSSL_SUCCESS);
  52905. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  52906. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  52907. EVP_CIPHER_CTX_free(ctx);
  52908. /* Decrypt. */
  52909. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52910. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  52911. WOLFSSL_SUCCESS);
  52912. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52913. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52914. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52915. ExpectIntEQ(outSz, sizeof(plainText));
  52916. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52917. WOLFSSL_SUCCESS);
  52918. ExpectIntEQ(outSz, 0);
  52919. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52920. EVP_CIPHER_CTX_free(ctx);
  52921. /* Test partial Inits. CipherInit() allow setting of key and iv
  52922. * in separate calls. */
  52923. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52924. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  52925. WOLFSSL_SUCCESS);
  52926. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  52927. WOLFSSL_SUCCESS);
  52928. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52929. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52930. ExpectIntEQ(outSz, sizeof(plainText));
  52931. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  52932. WOLFSSL_SUCCESS);
  52933. ExpectIntEQ(outSz, 0);
  52934. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52935. EVP_CIPHER_CTX_free(ctx);
  52936. res = EXPECT_RESULT();
  52937. #endif
  52938. return res;
  52939. }
  52940. static int test_wolfssl_EVP_sm4_ctr(void)
  52941. {
  52942. int res = TEST_SKIPPED;
  52943. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  52944. EXPECT_DECLS;
  52945. byte key[SM4_KEY_SIZE];
  52946. byte iv[SM4_BLOCK_SIZE];
  52947. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  52948. byte cipherText[sizeof(plainText)];
  52949. byte decryptedText[sizeof(plainText)];
  52950. EVP_CIPHER_CTX* ctx;
  52951. int outSz;
  52952. XMEMSET(key, 0, sizeof(key));
  52953. XMEMSET(iv, 0, sizeof(iv));
  52954. /* Encrypt. */
  52955. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52956. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  52957. WOLFSSL_SUCCESS);
  52958. /* Any tag length must fail - not an AEAD cipher. */
  52959. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  52960. WOLFSSL_FAILURE);
  52961. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52962. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  52963. sizeof(plainText)), WOLFSSL_SUCCESS);
  52964. ExpectIntEQ(outSz, sizeof(plainText));
  52965. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  52966. ExpectIntEQ(outSz, 0);
  52967. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  52968. EVP_CIPHER_CTX_free(ctx);
  52969. /* Decrypt. */
  52970. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52971. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  52972. WOLFSSL_SUCCESS);
  52973. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  52974. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52975. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52976. ExpectIntEQ(outSz, sizeof(cipherText));
  52977. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52978. WOLFSSL_SUCCESS);
  52979. ExpectIntEQ(outSz, 0);
  52980. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52981. EVP_CIPHER_CTX_free(ctx);
  52982. /* Test partial Inits. CipherInit() allow setting of key and iv
  52983. * in separate calls. */
  52984. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  52985. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  52986. WOLFSSL_SUCCESS);
  52987. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  52988. WOLFSSL_SUCCESS);
  52989. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  52990. sizeof(cipherText)), WOLFSSL_SUCCESS);
  52991. ExpectIntEQ(outSz, sizeof(cipherText));
  52992. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  52993. WOLFSSL_SUCCESS);
  52994. ExpectIntEQ(outSz, 0);
  52995. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  52996. EVP_CIPHER_CTX_free(ctx);
  52997. res = EXPECT_RESULT();
  52998. #endif
  52999. return res;
  53000. }
  53001. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  53002. {
  53003. int res = TEST_SKIPPED;
  53004. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  53005. /* Zero length plain text */
  53006. EXPECT_DECLS;
  53007. byte key[] = {
  53008. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53009. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53010. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53011. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53012. }; /* align */
  53013. byte iv[] = {
  53014. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53015. }; /* align */
  53016. byte plaintxt[1];
  53017. int ivSz = 12;
  53018. int plaintxtSz = 0;
  53019. unsigned char tag[16];
  53020. unsigned char tag_kat[16] = {
  53021. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  53022. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  53023. };
  53024. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53025. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53026. int ciphertxtSz = 0;
  53027. int decryptedtxtSz = 0;
  53028. int len = 0;
  53029. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  53030. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  53031. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  53032. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  53033. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  53034. plaintxtSz));
  53035. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  53036. ciphertxtSz += len;
  53037. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  53038. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  53039. ExpectIntEQ(0, ciphertxtSz);
  53040. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  53041. EVP_CIPHER_CTX_init(de);
  53042. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  53043. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  53044. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  53045. decryptedtxtSz = len;
  53046. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  53047. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  53048. decryptedtxtSz += len;
  53049. ExpectIntEQ(0, decryptedtxtSz);
  53050. EVP_CIPHER_CTX_free(en);
  53051. EVP_CIPHER_CTX_free(de);
  53052. res = EXPECT_RESULT();
  53053. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  53054. return res;
  53055. }
  53056. static int test_wolfssl_EVP_sm4_gcm(void)
  53057. {
  53058. int res = TEST_SKIPPED;
  53059. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  53060. EXPECT_DECLS;
  53061. byte *key = (byte*)"0123456789012345";
  53062. /* A 128 bit IV */
  53063. byte *iv = (byte*)"0123456789012345";
  53064. int ivSz = SM4_BLOCK_SIZE;
  53065. /* Message to be encrypted */
  53066. byte *plaintxt = (byte*)"for things to change you have to change";
  53067. /* Additional non-confidential data */
  53068. byte *aad = (byte*)"Don't spend major time on minor things.";
  53069. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  53070. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  53071. int aadSz = (int)XSTRLEN((char*)aad);
  53072. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53073. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53074. int ciphertxtSz = 0;
  53075. int decryptedtxtSz = 0;
  53076. int len = 0;
  53077. int i = 0;
  53078. EVP_CIPHER_CTX en[2];
  53079. EVP_CIPHER_CTX de[2];
  53080. for (i = 0; i < 2; i++) {
  53081. EVP_CIPHER_CTX_init(&en[i]);
  53082. if (i == 0) {
  53083. /* Default uses 96-bits IV length */
  53084. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  53085. iv));
  53086. }
  53087. else {
  53088. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  53089. NULL));
  53090. /* non-default must to set the IV length first */
  53091. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  53092. ivSz, NULL));
  53093. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  53094. }
  53095. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  53096. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  53097. plaintxtSz));
  53098. ciphertxtSz = len;
  53099. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  53100. ciphertxtSz += len;
  53101. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  53102. SM4_BLOCK_SIZE, tag));
  53103. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  53104. EVP_CIPHER_CTX_init(&de[i]);
  53105. if (i == 0) {
  53106. /* Default uses 96-bits IV length */
  53107. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  53108. iv));
  53109. }
  53110. else {
  53111. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  53112. NULL));
  53113. /* non-default must to set the IV length first */
  53114. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  53115. ivSz, NULL));
  53116. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  53117. }
  53118. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53119. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53120. ciphertxtSz));
  53121. decryptedtxtSz = len;
  53122. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  53123. SM4_BLOCK_SIZE, tag));
  53124. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53125. decryptedtxtSz += len;
  53126. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  53127. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  53128. /* modify tag*/
  53129. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  53130. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53131. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  53132. SM4_BLOCK_SIZE, tag));
  53133. /* fail due to wrong tag */
  53134. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53135. ciphertxtSz));
  53136. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53137. ExpectIntEQ(0, len);
  53138. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  53139. }
  53140. res = EXPECT_RESULT();
  53141. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  53142. return res;
  53143. }
  53144. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  53145. {
  53146. int res = TEST_SKIPPED;
  53147. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  53148. /* Zero length plain text */
  53149. EXPECT_DECLS;
  53150. byte key[] = {
  53151. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53152. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53153. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  53154. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53155. }; /* align */
  53156. byte iv[] = {
  53157. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  53158. }; /* align */
  53159. byte plaintxt[1];
  53160. int ivSz = 12;
  53161. int plaintxtSz = 0;
  53162. unsigned char tag[16];
  53163. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53164. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53165. int ciphertxtSz = 0;
  53166. int decryptedtxtSz = 0;
  53167. int len = 0;
  53168. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  53169. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  53170. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  53171. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  53172. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  53173. plaintxtSz));
  53174. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  53175. ciphertxtSz += len;
  53176. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  53177. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  53178. ExpectIntEQ(0, ciphertxtSz);
  53179. EVP_CIPHER_CTX_init(de);
  53180. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  53181. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  53182. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  53183. decryptedtxtSz = len;
  53184. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  53185. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  53186. decryptedtxtSz += len;
  53187. ExpectIntEQ(0, decryptedtxtSz);
  53188. EVP_CIPHER_CTX_free(en);
  53189. EVP_CIPHER_CTX_free(de);
  53190. res = EXPECT_RESULT();
  53191. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  53192. return res;
  53193. }
  53194. static int test_wolfssl_EVP_sm4_ccm(void)
  53195. {
  53196. int res = TEST_SKIPPED;
  53197. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  53198. EXPECT_DECLS;
  53199. byte *key = (byte*)"0123456789012345";
  53200. byte *iv = (byte*)"0123456789012";
  53201. int ivSz = (int)XSTRLEN((char*)iv);
  53202. /* Message to be encrypted */
  53203. byte *plaintxt = (byte*)"for things to change you have to change";
  53204. /* Additional non-confidential data */
  53205. byte *aad = (byte*)"Don't spend major time on minor things.";
  53206. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  53207. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  53208. int aadSz = (int)XSTRLEN((char*)aad);
  53209. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  53210. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  53211. int ciphertxtSz = 0;
  53212. int decryptedtxtSz = 0;
  53213. int len = 0;
  53214. int i = 0;
  53215. EVP_CIPHER_CTX en[2];
  53216. EVP_CIPHER_CTX de[2];
  53217. for (i = 0; i < 2; i++) {
  53218. EVP_CIPHER_CTX_init(&en[i]);
  53219. if (i == 0) {
  53220. /* Default uses 96-bits IV length */
  53221. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  53222. iv));
  53223. }
  53224. else {
  53225. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  53226. NULL));
  53227. /* non-default must to set the IV length first */
  53228. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  53229. ivSz, NULL));
  53230. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  53231. }
  53232. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  53233. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  53234. plaintxtSz));
  53235. ciphertxtSz = len;
  53236. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  53237. ciphertxtSz += len;
  53238. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  53239. SM4_BLOCK_SIZE, tag));
  53240. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  53241. EVP_CIPHER_CTX_init(&de[i]);
  53242. if (i == 0) {
  53243. /* Default uses 96-bits IV length */
  53244. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  53245. iv));
  53246. }
  53247. else {
  53248. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  53249. NULL));
  53250. /* non-default must to set the IV length first */
  53251. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  53252. ivSz, NULL));
  53253. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  53254. }
  53255. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53256. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53257. ciphertxtSz));
  53258. decryptedtxtSz = len;
  53259. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  53260. SM4_BLOCK_SIZE, tag));
  53261. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53262. decryptedtxtSz += len;
  53263. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  53264. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  53265. /* modify tag*/
  53266. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  53267. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  53268. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  53269. SM4_BLOCK_SIZE, tag));
  53270. /* fail due to wrong tag */
  53271. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  53272. ciphertxtSz));
  53273. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  53274. ExpectIntEQ(0, len);
  53275. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  53276. }
  53277. res = EXPECT_RESULT();
  53278. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  53279. return res;
  53280. }
  53281. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  53282. {
  53283. EXPECT_DECLS;
  53284. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  53285. EVP_PKEY_CTX* ctx = NULL;
  53286. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  53287. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  53288. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  53289. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  53290. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  53291. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  53292. byte outKey[34];
  53293. size_t outKeySz = sizeof(outKey);
  53294. /* These expected outputs were gathered by running the same test below using
  53295. * OpenSSL. */
  53296. const byte extractAndExpand[] = {
  53297. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  53298. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  53299. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  53300. };
  53301. const byte extractOnly[] = {
  53302. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  53303. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  53304. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  53305. };
  53306. const byte expandOnly[] = {
  53307. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  53308. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  53309. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  53310. };
  53311. const byte extractAndExpandAddInfo[] = {
  53312. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  53313. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  53314. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  53315. };
  53316. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  53317. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  53318. /* NULL ctx. */
  53319. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  53320. /* NULL md. */
  53321. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  53322. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  53323. /* NULL ctx. */
  53324. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  53325. WOLFSSL_FAILURE);
  53326. /* NULL salt is ok. */
  53327. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  53328. WOLFSSL_SUCCESS);
  53329. /* Salt length <= 0. */
  53330. /* Length 0 salt is ok. */
  53331. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  53332. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  53333. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  53334. WOLFSSL_SUCCESS);
  53335. /* NULL ctx. */
  53336. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  53337. WOLFSSL_FAILURE);
  53338. /* NULL key. */
  53339. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  53340. WOLFSSL_FAILURE);
  53341. /* Key length <= 0 */
  53342. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  53343. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  53344. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  53345. WOLFSSL_SUCCESS);
  53346. /* NULL ctx. */
  53347. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  53348. WOLFSSL_FAILURE);
  53349. /* NULL info is ok. */
  53350. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  53351. WOLFSSL_SUCCESS);
  53352. /* Info length <= 0 */
  53353. /* Length 0 info is ok. */
  53354. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  53355. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  53356. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  53357. WOLFSSL_SUCCESS);
  53358. /* NULL ctx. */
  53359. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  53360. WOLFSSL_FAILURE);
  53361. /* Extract and expand (default). */
  53362. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53363. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  53364. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  53365. /* Extract only. */
  53366. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  53367. WOLFSSL_SUCCESS);
  53368. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53369. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  53370. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  53371. outKeySz = sizeof(outKey);
  53372. /* Expand only. */
  53373. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  53374. WOLFSSL_SUCCESS);
  53375. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53376. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  53377. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  53378. outKeySz = sizeof(outKey);
  53379. /* Extract and expand with appended additional info. */
  53380. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  53381. WOLFSSL_SUCCESS);
  53382. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  53383. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  53384. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  53385. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  53386. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  53387. EVP_PKEY_CTX_free(ctx);
  53388. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  53389. return EXPECT_RESULT();
  53390. }
  53391. #ifndef NO_BIO
  53392. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  53393. {
  53394. EXPECT_DECLS;
  53395. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53396. BIO* bio = NULL;
  53397. X509_INFO* info = NULL;
  53398. STACK_OF(X509_INFO)* sk = NULL;
  53399. char* subject = NULL;
  53400. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  53401. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  53402. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  53403. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  53404. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53405. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  53406. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  53407. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  53408. /* using dereference to maintain testing for Apache port*/
  53409. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  53410. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  53411. 0, 0));
  53412. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  53413. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  53414. X509_INFO_free(info);
  53415. info = NULL;
  53416. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  53417. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  53418. 0, 0));
  53419. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  53420. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  53421. X509_INFO_free(info);
  53422. ExpectNull(info = sk_X509_INFO_pop(sk));
  53423. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  53424. BIO_free(bio);
  53425. #endif
  53426. return EXPECT_RESULT();
  53427. }
  53428. #endif /* !NO_BIO */
  53429. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  53430. {
  53431. EXPECT_DECLS;
  53432. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53433. X509 *x509 = NULL;
  53434. X509_NAME* name = NULL;
  53435. int idx = 0;
  53436. X509_NAME_ENTRY *ne = NULL;
  53437. ASN1_OBJECT *object = NULL;
  53438. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  53439. WOLFSSL_FILETYPE_PEM));
  53440. ExpectNotNull(name = X509_get_subject_name(x509));
  53441. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  53442. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  53443. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  53444. X509_free(x509);
  53445. #endif
  53446. return EXPECT_RESULT();
  53447. }
  53448. static int test_wolfSSL_X509_STORE_get1_certs(void)
  53449. {
  53450. EXPECT_DECLS;
  53451. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  53452. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  53453. X509_STORE_CTX *storeCtx = NULL;
  53454. X509_STORE *store = NULL;
  53455. X509 *caX509 = NULL;
  53456. X509 *svrX509 = NULL;
  53457. X509_NAME *subject = NULL;
  53458. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  53459. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  53460. SSL_FILETYPE_PEM));
  53461. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  53462. SSL_FILETYPE_PEM)));
  53463. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  53464. ExpectNotNull(store = X509_STORE_new());
  53465. ExpectNotNull(subject = X509_get_subject_name(caX509));
  53466. /* Errors */
  53467. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  53468. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  53469. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  53470. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  53471. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  53472. SSL_SUCCESS);
  53473. /* Should find the cert */
  53474. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  53475. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  53476. sk_X509_pop_free(certs, NULL);
  53477. certs = NULL;
  53478. /* Should not find the cert */
  53479. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  53480. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  53481. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  53482. sk_X509_pop_free(certs, NULL);
  53483. certs = NULL;
  53484. X509_STORE_free(store);
  53485. X509_STORE_CTX_free(storeCtx);
  53486. X509_free(svrX509);
  53487. X509_free(caX509);
  53488. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  53489. return EXPECT_RESULT();
  53490. }
  53491. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53492. defined(WOLFSSL_LOCAL_X509_STORE) && \
  53493. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  53494. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  53495. X509_CRL** crl_out, X509* cert) {
  53496. X509_CRL *crl = NULL;
  53497. XFILE fp = XBADFILE;
  53498. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  53499. int ret = 0;
  53500. (void)ctx;
  53501. if (cert_issuer == NULL)
  53502. return 0;
  53503. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  53504. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  53505. XFCLOSE(fp);
  53506. if (crl != NULL) {
  53507. char* crl_issuer = X509_NAME_oneline(
  53508. X509_CRL_get_issuer(crl), NULL, 0);
  53509. if (XSTRCMP(cert_issuer, crl_issuer) == 0) {
  53510. *crl_out = X509_CRL_dup(crl);
  53511. if (*crl_out != NULL)
  53512. ret = 1;
  53513. }
  53514. OPENSSL_free(crl_issuer);
  53515. }
  53516. }
  53517. X509_CRL_free(crl);
  53518. OPENSSL_free(cert_issuer);
  53519. return ret;
  53520. }
  53521. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  53522. X509_CRL** crl_out, X509* cert) {
  53523. (void)ctx;
  53524. (void)cert;
  53525. *crl_out = NULL;
  53526. return 1;
  53527. }
  53528. #ifndef NO_WOLFSSL_STUB
  53529. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  53530. X509_CRL* crl) {
  53531. (void)ctx;
  53532. (void)crl;
  53533. return 1;
  53534. }
  53535. #endif
  53536. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  53537. X509_STORE_CTX* ctx) {
  53538. int cert_error = X509_STORE_CTX_get_error(ctx);
  53539. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  53540. int flags = X509_VERIFY_PARAM_get_flags(param);
  53541. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  53542. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  53543. /* Make sure the flags are set */
  53544. return 0;
  53545. }
  53546. /* Ignore CRL missing error */
  53547. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  53548. if (cert_error == CRL_MISSING)
  53549. #else
  53550. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  53551. #endif
  53552. return 1;
  53553. return ok;
  53554. }
  53555. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  53556. {
  53557. EXPECT_DECLS;
  53558. X509_STORE* cert_store = NULL;
  53559. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  53560. WOLFSSL_SUCCESS);
  53561. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  53562. X509_STORE_set_get_crl(cert_store,
  53563. test_wolfSSL_X509_STORE_set_get_crl_provider);
  53564. #ifndef NO_WOLFSSL_STUB
  53565. X509_STORE_set_check_crl(cert_store,
  53566. test_wolfSSL_X509_STORE_set_get_crl_check);
  53567. #endif
  53568. return EXPECT_RESULT();
  53569. }
  53570. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  53571. {
  53572. EXPECT_DECLS;
  53573. X509_STORE* cert_store = NULL;
  53574. X509_VERIFY_PARAM* param = NULL;
  53575. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  53576. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  53577. WOLFSSL_SUCCESS);
  53578. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  53579. X509_STORE_set_get_crl(cert_store,
  53580. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  53581. #ifndef NO_WOLFSSL_STUB
  53582. X509_STORE_set_check_crl(cert_store,
  53583. test_wolfSSL_X509_STORE_set_get_crl_check);
  53584. #endif
  53585. X509_STORE_set_verify_cb(cert_store,
  53586. test_wolfSSL_X509_STORE_set_get_crl_verify);
  53587. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  53588. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  53589. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  53590. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  53591. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  53592. return EXPECT_RESULT();
  53593. }
  53594. #endif
  53595. /* This test mimics the usage of the CRL provider in gRPC */
  53596. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  53597. {
  53598. EXPECT_DECLS;
  53599. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  53600. defined(WOLFSSL_LOCAL_X509_STORE) && \
  53601. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  53602. test_ssl_cbf func_cb_client;
  53603. test_ssl_cbf func_cb_server;
  53604. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  53605. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  53606. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  53607. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  53608. &func_cb_server, NULL), TEST_SUCCESS);
  53609. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  53610. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  53611. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  53612. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  53613. &func_cb_server, NULL), TEST_SUCCESS);
  53614. #endif
  53615. return EXPECT_RESULT();
  53616. }
  53617. static int test_wolfSSL_dup_CA_list(void)
  53618. {
  53619. int res = TEST_SKIPPED;
  53620. #if defined(OPENSSL_ALL)
  53621. EXPECT_DECLS;
  53622. STACK_OF(X509_NAME) *originalStack = NULL;
  53623. STACK_OF(X509_NAME) *copyStack = NULL;
  53624. int originalCount = 0;
  53625. int copyCount = 0;
  53626. X509_NAME *name = NULL;
  53627. int i;
  53628. originalStack = sk_X509_NAME_new_null();
  53629. ExpectNotNull(originalStack);
  53630. for (i = 0; i < 3; i++) {
  53631. name = X509_NAME_new();
  53632. ExpectNotNull(name);
  53633. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  53634. }
  53635. copyStack = SSL_dup_CA_list(originalStack);
  53636. ExpectNotNull(copyStack);
  53637. originalCount = sk_X509_NAME_num(originalStack);
  53638. copyCount = sk_X509_NAME_num(copyStack);
  53639. AssertIntEQ(originalCount, copyCount);
  53640. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  53641. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  53642. originalStack = NULL;
  53643. copyStack = NULL;
  53644. res = EXPECT_RESULT();
  53645. #endif /* OPENSSL_ALL */
  53646. return res;
  53647. }
  53648. static int test_ForceZero(void)
  53649. {
  53650. EXPECT_DECLS;
  53651. unsigned char data[32];
  53652. unsigned int i, j, len;
  53653. /* Test case with 0 length */
  53654. ForceZero(data, 0);
  53655. /* Test ForceZero */
  53656. for (i = 0; i < sizeof(data); i++) {
  53657. for (len = 1; len < sizeof(data) - i; len++) {
  53658. for (j = 0; j < sizeof(data); j++)
  53659. data[j] = j + 1;
  53660. ForceZero(data + i, len);
  53661. for (j = 0; j < sizeof(data); j++) {
  53662. if (j < i || j >= i + len) {
  53663. ExpectIntNE(data[j], 0x00);
  53664. }
  53665. else {
  53666. ExpectIntEQ(data[j], 0x00);
  53667. }
  53668. }
  53669. }
  53670. }
  53671. return EXPECT_RESULT();
  53672. }
  53673. #ifndef NO_BIO
  53674. static int test_wolfSSL_X509_print(void)
  53675. {
  53676. EXPECT_DECLS;
  53677. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53678. !defined(NO_RSA) && defined(XSNPRINTF)
  53679. X509 *x509 = NULL;
  53680. BIO *bio = NULL;
  53681. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  53682. const X509_ALGOR *cert_sig_alg = NULL;
  53683. #endif
  53684. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  53685. WOLFSSL_FILETYPE_PEM));
  53686. /* print to memory */
  53687. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53688. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  53689. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  53690. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  53691. /* Will print IP address subject alt name. */
  53692. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  53693. #elif defined(NO_ASN_TIME)
  53694. /* Will print IP address subject alt name but not Validity. */
  53695. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  53696. #else
  53697. /* Will print IP address subject alt name. */
  53698. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  53699. #endif
  53700. #elif defined(NO_ASN_TIME)
  53701. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  53702. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  53703. #else
  53704. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  53705. #endif
  53706. BIO_free(bio);
  53707. bio = NULL;
  53708. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  53709. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  53710. /* Print signature */
  53711. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  53712. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  53713. #endif
  53714. /* print to stderr */
  53715. #if !defined(NO_WOLFSSL_DIR)
  53716. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  53717. #endif
  53718. /* print again */
  53719. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  53720. X509_free(x509);
  53721. BIO_free(bio);
  53722. #endif
  53723. return EXPECT_RESULT();
  53724. }
  53725. static int test_wolfSSL_X509_CRL_print(void)
  53726. {
  53727. EXPECT_DECLS;
  53728. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  53729. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  53730. X509_CRL* crl = NULL;
  53731. BIO *bio = NULL;
  53732. XFILE fp = XBADFILE;
  53733. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  53734. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  53735. NULL, NULL));
  53736. if (fp != XBADFILE)
  53737. XFCLOSE(fp);
  53738. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53739. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  53740. X509_CRL_free(crl);
  53741. BIO_free(bio);
  53742. #endif
  53743. return EXPECT_RESULT();
  53744. }
  53745. static int test_wolfSSL_BIO_get_len(void)
  53746. {
  53747. EXPECT_DECLS;
  53748. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53749. BIO *bio = NULL;
  53750. const char txt[] = "Some example text to push to the BIO.";
  53751. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  53752. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  53753. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  53754. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  53755. BIO_free(bio);
  53756. bio = NULL;
  53757. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  53758. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  53759. BIO_free(bio);
  53760. #endif
  53761. return EXPECT_RESULT();
  53762. }
  53763. #endif /* !NO_BIO */
  53764. static int test_wolfSSL_RSA(void)
  53765. {
  53766. EXPECT_DECLS;
  53767. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  53768. RSA* rsa = NULL;
  53769. const BIGNUM *n = NULL;
  53770. const BIGNUM *e = NULL;
  53771. const BIGNUM *d = NULL;
  53772. const BIGNUM *p = NULL;
  53773. const BIGNUM *q = NULL;
  53774. const BIGNUM *dmp1 = NULL;
  53775. const BIGNUM *dmq1 = NULL;
  53776. const BIGNUM *iqmp = NULL;
  53777. ExpectNotNull(rsa = RSA_new());
  53778. ExpectIntEQ(RSA_size(NULL), 0);
  53779. ExpectIntEQ(RSA_size(rsa), 0);
  53780. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  53781. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  53782. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  53783. #ifdef WOLFSSL_RSA_KEY_CHECK
  53784. ExpectIntEQ(RSA_check_key(rsa), 0);
  53785. #endif
  53786. RSA_free(rsa);
  53787. rsa = NULL;
  53788. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  53789. ExpectIntEQ(RSA_size(rsa), 256);
  53790. #ifdef WOLFSSL_RSA_KEY_CHECK
  53791. ExpectIntEQ(RSA_check_key(NULL), 0);
  53792. ExpectIntEQ(RSA_check_key(rsa), 1);
  53793. #endif
  53794. /* sanity check */
  53795. ExpectIntEQ(RSA_bits(NULL), 0);
  53796. /* key */
  53797. ExpectIntEQ(RSA_bits(rsa), 2048);
  53798. RSA_get0_key(rsa, &n, &e, &d);
  53799. ExpectPtrEq(rsa->n, n);
  53800. ExpectPtrEq(rsa->e, e);
  53801. ExpectPtrEq(rsa->d, d);
  53802. n = NULL;
  53803. e = NULL;
  53804. d = NULL;
  53805. ExpectNotNull(n = BN_new());
  53806. ExpectNotNull(e = BN_new());
  53807. ExpectNotNull(d = BN_new());
  53808. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  53809. if (EXPECT_FAIL()) {
  53810. BN_free((BIGNUM*)n);
  53811. BN_free((BIGNUM*)e);
  53812. BN_free((BIGNUM*)d);
  53813. }
  53814. ExpectPtrEq(rsa->n, n);
  53815. ExpectPtrEq(rsa->e, e);
  53816. ExpectPtrEq(rsa->d, d);
  53817. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  53818. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  53819. /* crt_params */
  53820. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  53821. ExpectPtrEq(rsa->dmp1, dmp1);
  53822. ExpectPtrEq(rsa->dmq1, dmq1);
  53823. ExpectPtrEq(rsa->iqmp, iqmp);
  53824. dmp1 = NULL;
  53825. dmq1 = NULL;
  53826. iqmp = NULL;
  53827. ExpectNotNull(dmp1 = BN_new());
  53828. ExpectNotNull(dmq1 = BN_new());
  53829. ExpectNotNull(iqmp = BN_new());
  53830. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  53831. (BIGNUM*)iqmp), 1);
  53832. if (EXPECT_FAIL()) {
  53833. BN_free((BIGNUM*)dmp1);
  53834. BN_free((BIGNUM*)dmq1);
  53835. BN_free((BIGNUM*)iqmp);
  53836. }
  53837. ExpectPtrEq(rsa->dmp1, dmp1);
  53838. ExpectPtrEq(rsa->dmq1, dmq1);
  53839. ExpectPtrEq(rsa->iqmp, iqmp);
  53840. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  53841. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  53842. (BIGNUM*)iqmp), 0);
  53843. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  53844. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  53845. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  53846. ExpectNull(dmp1);
  53847. ExpectNull(dmq1);
  53848. ExpectNull(iqmp);
  53849. /* factors */
  53850. RSA_get0_factors(rsa, NULL, NULL);
  53851. RSA_get0_factors(rsa, &p, &q);
  53852. ExpectPtrEq(rsa->p, p);
  53853. ExpectPtrEq(rsa->q, q);
  53854. p = NULL;
  53855. q = NULL;
  53856. ExpectNotNull(p = BN_new());
  53857. ExpectNotNull(q = BN_new());
  53858. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  53859. if (EXPECT_FAIL()) {
  53860. BN_free((BIGNUM*)p);
  53861. BN_free((BIGNUM*)q);
  53862. }
  53863. ExpectPtrEq(rsa->p, p);
  53864. ExpectPtrEq(rsa->q, q);
  53865. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  53866. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  53867. RSA_get0_factors(NULL, NULL, NULL);
  53868. RSA_get0_factors(NULL, &p, &q);
  53869. ExpectNull(p);
  53870. ExpectNull(q);
  53871. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  53872. ExpectIntEQ(RSA_bits(rsa), 21);
  53873. RSA_free(rsa);
  53874. rsa = NULL;
  53875. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  53876. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  53877. ExpectIntEQ(RSA_size(rsa), 384);
  53878. ExpectIntEQ(RSA_bits(rsa), 3072);
  53879. RSA_free(rsa);
  53880. rsa = NULL;
  53881. #endif
  53882. /* remove for now with odd key size until adjusting rsa key size check with
  53883. wc_MakeRsaKey()
  53884. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  53885. RSA_free(rsa);
  53886. rsa = NULL;
  53887. */
  53888. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  53889. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  53890. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  53891. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  53892. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  53893. {
  53894. byte buff[FOURK_BUF];
  53895. byte der[FOURK_BUF];
  53896. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  53897. XFILE f = XBADFILE;
  53898. int bytes = 0;
  53899. /* test loading encrypted RSA private pem w/o password */
  53900. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  53901. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  53902. if (f != XBADFILE)
  53903. XFCLOSE(f);
  53904. XMEMSET(der, 0, sizeof(der));
  53905. /* test that error value is returned with no password */
  53906. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  53907. 0);
  53908. }
  53909. #endif
  53910. #endif
  53911. return EXPECT_RESULT();
  53912. }
  53913. static int test_wolfSSL_RSA_DER(void)
  53914. {
  53915. EXPECT_DECLS;
  53916. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  53917. RSA *rsa = NULL;
  53918. int i;
  53919. const unsigned char *buff = NULL;
  53920. unsigned char *newBuff = NULL;
  53921. struct tbl_s
  53922. {
  53923. const unsigned char *der;
  53924. int sz;
  53925. } tbl[] = {
  53926. #ifdef USE_CERT_BUFFERS_1024
  53927. {client_key_der_1024, sizeof_client_key_der_1024},
  53928. {server_key_der_1024, sizeof_server_key_der_1024},
  53929. #endif
  53930. #ifdef USE_CERT_BUFFERS_2048
  53931. {client_key_der_2048, sizeof_client_key_der_2048},
  53932. {server_key_der_2048, sizeof_server_key_der_2048},
  53933. #endif
  53934. {NULL, 0}
  53935. };
  53936. /* Public Key DER */
  53937. struct tbl_s pub[] = {
  53938. #ifdef USE_CERT_BUFFERS_1024
  53939. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  53940. #endif
  53941. #ifdef USE_CERT_BUFFERS_2048
  53942. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  53943. #endif
  53944. {NULL, 0}
  53945. };
  53946. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  53947. buff = pub[0].der;
  53948. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  53949. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  53950. buff = tbl[0].der;
  53951. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  53952. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  53953. rsa = RSA_new();
  53954. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  53955. RSA_free(rsa);
  53956. rsa = NULL;
  53957. for (i = 0; tbl[i].der != NULL; i++)
  53958. {
  53959. /* Passing in pointer results in pointer moving. */
  53960. buff = tbl[i].der;
  53961. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  53962. ExpectNotNull(rsa);
  53963. RSA_free(rsa);
  53964. rsa = NULL;
  53965. }
  53966. for (i = 0; tbl[i].der != NULL; i++)
  53967. {
  53968. /* Passing in pointer results in pointer moving. */
  53969. buff = tbl[i].der;
  53970. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  53971. ExpectNotNull(rsa);
  53972. RSA_free(rsa);
  53973. rsa = NULL;
  53974. }
  53975. for (i = 0; pub[i].der != NULL; i++)
  53976. {
  53977. buff = pub[i].der;
  53978. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  53979. ExpectNotNull(rsa);
  53980. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  53981. newBuff = NULL;
  53982. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  53983. ExpectNotNull(newBuff);
  53984. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  53985. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53986. RSA_free(rsa);
  53987. rsa = NULL;
  53988. }
  53989. #endif
  53990. return EXPECT_RESULT();
  53991. }
  53992. static int test_wolfSSL_RSA_print(void)
  53993. {
  53994. EXPECT_DECLS;
  53995. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53996. !defined(NO_STDIO_FILESYSTEM) && \
  53997. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  53998. !defined(NO_BIO) && defined(XFPRINTF)
  53999. BIO *bio = NULL;
  54000. WOLFSSL_RSA* rsa = NULL;
  54001. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  54002. ExpectNotNull(rsa = RSA_new());
  54003. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  54004. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  54005. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  54006. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  54007. /* Some very large number of indent spaces. */
  54008. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  54009. /* RSA is empty. */
  54010. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  54011. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  54012. RSA_free(rsa);
  54013. rsa = NULL;
  54014. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54015. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  54016. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  54017. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  54018. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  54019. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  54020. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  54021. BIO_free(bio);
  54022. RSA_free(rsa);
  54023. #endif
  54024. return EXPECT_RESULT();
  54025. }
  54026. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  54027. {
  54028. EXPECT_DECLS;
  54029. #ifndef NO_RSA
  54030. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  54031. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54032. RSA *rsa = NULL;
  54033. const unsigned char *derBuf = client_key_der_2048;
  54034. unsigned char em[256] = {0}; /* len = 2048/8 */
  54035. /* Random data simulating a hash */
  54036. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  54037. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  54038. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  54039. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  54040. };
  54041. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  54042. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  54043. RSA_PSS_SALTLEN_DIGEST), 0);
  54044. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  54045. RSA_PSS_SALTLEN_DIGEST), 0);
  54046. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  54047. RSA_PSS_SALTLEN_DIGEST), 0);
  54048. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  54049. RSA_PSS_SALTLEN_DIGEST), 0);
  54050. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  54051. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  54052. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54053. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  54054. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54055. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  54056. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54057. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  54058. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54059. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54060. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  54061. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  54062. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54063. RSA_PSS_SALTLEN_DIGEST), 1);
  54064. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54065. RSA_PSS_SALTLEN_DIGEST), 1);
  54066. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54067. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  54068. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54069. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  54070. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  54071. RSA_PSS_SALTLEN_MAX), 1);
  54072. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  54073. RSA_PSS_SALTLEN_MAX), 1);
  54074. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  54075. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  54076. RSA_free(rsa);
  54077. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54078. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  54079. #endif
  54080. return EXPECT_RESULT();
  54081. }
  54082. static int test_wolfSSL_RSA_sign_sha3(void)
  54083. {
  54084. EXPECT_DECLS;
  54085. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  54086. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  54087. RSA* rsa = NULL;
  54088. const unsigned char *derBuf = client_key_der_2048;
  54089. unsigned char sigRet[256] = {0};
  54090. unsigned int sigLen = sizeof(sigRet);
  54091. /* Random data simulating a hash */
  54092. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  54093. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  54094. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  54095. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  54096. };
  54097. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  54098. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  54099. rsa), 1);
  54100. RSA_free(rsa);
  54101. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  54102. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  54103. return EXPECT_RESULT();
  54104. }
  54105. static int test_wolfSSL_RSA_get0_key(void)
  54106. {
  54107. EXPECT_DECLS;
  54108. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54109. RSA *rsa = NULL;
  54110. const BIGNUM* n = NULL;
  54111. const BIGNUM* e = NULL;
  54112. const BIGNUM* d = NULL;
  54113. const unsigned char* der;
  54114. int derSz;
  54115. #ifdef USE_CERT_BUFFERS_1024
  54116. der = client_key_der_1024;
  54117. derSz = sizeof_client_key_der_1024;
  54118. #elif defined(USE_CERT_BUFFERS_2048)
  54119. der = client_key_der_2048;
  54120. derSz = sizeof_client_key_der_2048;
  54121. #else
  54122. der = NULL;
  54123. derSz = 0;
  54124. #endif
  54125. if (der != NULL) {
  54126. RSA_get0_key(NULL, NULL, NULL, NULL);
  54127. RSA_get0_key(rsa, NULL, NULL, NULL);
  54128. RSA_get0_key(NULL, &n, &e, &d);
  54129. ExpectNull(n);
  54130. ExpectNull(e);
  54131. ExpectNull(d);
  54132. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  54133. ExpectNotNull(rsa);
  54134. RSA_get0_key(rsa, NULL, NULL, NULL);
  54135. RSA_get0_key(rsa, &n, NULL, NULL);
  54136. ExpectNotNull(n);
  54137. RSA_get0_key(rsa, NULL, &e, NULL);
  54138. ExpectNotNull(e);
  54139. RSA_get0_key(rsa, NULL, NULL, &d);
  54140. ExpectNotNull(d);
  54141. RSA_get0_key(rsa, &n, &e, &d);
  54142. ExpectNotNull(n);
  54143. ExpectNotNull(e);
  54144. ExpectNotNull(d);
  54145. RSA_free(rsa);
  54146. }
  54147. #endif
  54148. return EXPECT_RESULT();
  54149. }
  54150. static int test_wolfSSL_RSA_meth(void)
  54151. {
  54152. EXPECT_DECLS;
  54153. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54154. RSA *rsa = NULL;
  54155. RSA_METHOD *rsa_meth = NULL;
  54156. #ifdef WOLFSSL_KEY_GEN
  54157. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54158. RSA_free(rsa);
  54159. rsa = NULL;
  54160. #else
  54161. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  54162. #endif
  54163. ExpectNotNull(RSA_get_default_method());
  54164. wolfSSL_RSA_meth_free(NULL);
  54165. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  54166. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  54167. RSA_METHOD_FLAG_NO_CHECK));
  54168. #ifndef NO_WOLFSSL_STUB
  54169. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  54170. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  54171. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  54172. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  54173. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  54174. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  54175. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  54176. #endif
  54177. ExpectIntEQ(RSA_flags(NULL), 0);
  54178. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  54179. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  54180. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  54181. ExpectNotNull(rsa = RSA_new());
  54182. /* No method set. */
  54183. ExpectIntEQ(RSA_flags(rsa), 0);
  54184. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54185. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54186. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54187. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  54188. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  54189. if (EXPECT_FAIL()) {
  54190. wolfSSL_RSA_meth_free(rsa_meth);
  54191. }
  54192. ExpectNull(RSA_get_method(NULL));
  54193. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  54194. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  54195. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54196. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54197. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  54198. RSA_METHOD_FLAG_NO_CHECK);
  54199. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  54200. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  54201. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  54202. /* rsa_meth is freed here */
  54203. RSA_free(rsa);
  54204. #endif
  54205. return EXPECT_RESULT();
  54206. }
  54207. static int test_wolfSSL_RSA_verify(void)
  54208. {
  54209. EXPECT_DECLS;
  54210. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  54211. #ifndef NO_BIO
  54212. XFILE fp = XBADFILE;
  54213. RSA *pKey = NULL;
  54214. RSA *pubKey = NULL;
  54215. X509 *cert = NULL;
  54216. const char *text = "Hello wolfSSL !";
  54217. unsigned char hash[SHA256_DIGEST_LENGTH];
  54218. unsigned char signature[2048/8];
  54219. unsigned int signatureLength;
  54220. byte *buf = NULL;
  54221. BIO *bio = NULL;
  54222. SHA256_CTX c;
  54223. EVP_PKEY *evpPkey = NULL;
  54224. EVP_PKEY *evpPubkey = NULL;
  54225. size_t sz;
  54226. /* generate hash */
  54227. SHA256_Init(&c);
  54228. SHA256_Update(&c, text, strlen(text));
  54229. SHA256_Final(hash, &c);
  54230. #ifdef WOLFSSL_SMALL_STACK_CACHE
  54231. /* workaround for small stack cache case */
  54232. wc_Sha256Free((wc_Sha256*)&c);
  54233. #endif
  54234. /* read privete key file */
  54235. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  54236. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  54237. ExpectTrue((sz = XFTELL(fp)) > 0);
  54238. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  54239. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  54240. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  54241. if (fp != XBADFILE) {
  54242. XFCLOSE(fp);
  54243. fp = XBADFILE;
  54244. }
  54245. /* read private key and sign hash data */
  54246. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  54247. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  54248. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  54249. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  54250. signature, &signatureLength, pKey), SSL_SUCCESS);
  54251. /* read public key and verify signed data */
  54252. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  54253. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  54254. if (fp != XBADFILE)
  54255. XFCLOSE(fp);
  54256. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  54257. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  54258. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  54259. signatureLength, pubKey), SSL_SUCCESS);
  54260. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  54261. signatureLength, NULL), SSL_FAILURE);
  54262. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  54263. signatureLength, pubKey), SSL_FAILURE);
  54264. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  54265. signatureLength, pubKey), SSL_FAILURE);
  54266. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  54267. signatureLength, NULL), SSL_FAILURE);
  54268. RSA_free(pKey);
  54269. EVP_PKEY_free(evpPkey);
  54270. RSA_free(pubKey);
  54271. EVP_PKEY_free(evpPubkey);
  54272. X509_free(cert);
  54273. BIO_free(bio);
  54274. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  54275. #endif
  54276. #endif
  54277. return EXPECT_RESULT();
  54278. }
  54279. static int test_wolfSSL_RSA_sign(void)
  54280. {
  54281. EXPECT_DECLS;
  54282. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54283. RSA *rsa;
  54284. unsigned char hash[SHA256_DIGEST_LENGTH];
  54285. #ifdef USE_CERT_BUFFERS_1024
  54286. const unsigned char* privDer = client_key_der_1024;
  54287. size_t privDerSz = sizeof_client_key_der_1024;
  54288. const unsigned char* pubDer = client_keypub_der_1024;
  54289. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54290. unsigned char signature[1024/8];
  54291. #else
  54292. const unsigned char* privDer = client_key_der_2048;
  54293. size_t privDerSz = sizeof_client_key_der_2048;
  54294. const unsigned char* pubDer = client_keypub_der_2048;
  54295. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54296. unsigned char signature[2048/8];
  54297. #endif
  54298. unsigned int signatureLen;
  54299. const unsigned char* der;
  54300. XMEMSET(hash, 0, sizeof(hash));
  54301. der = privDer;
  54302. rsa = NULL;
  54303. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54304. /* Invalid parameters. */
  54305. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  54306. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  54307. &signatureLen, rsa), 0);
  54308. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  54309. &signatureLen, rsa), 0);
  54310. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  54311. &signatureLen, rsa), 0);
  54312. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54313. NULL, rsa), 0);
  54314. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54315. &signatureLen, NULL), 0);
  54316. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  54317. &signatureLen, rsa), 1);
  54318. RSA_free(rsa);
  54319. der = pubDer;
  54320. rsa = NULL;
  54321. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54322. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  54323. signatureLen, rsa), 1);
  54324. RSA_free(rsa);
  54325. #endif
  54326. return EXPECT_RESULT();
  54327. }
  54328. static int test_wolfSSL_RSA_sign_ex(void)
  54329. {
  54330. EXPECT_DECLS;
  54331. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54332. RSA *rsa = NULL;
  54333. unsigned char hash[SHA256_DIGEST_LENGTH];
  54334. #ifdef USE_CERT_BUFFERS_1024
  54335. const unsigned char* privDer = client_key_der_1024;
  54336. size_t privDerSz = sizeof_client_key_der_1024;
  54337. const unsigned char* pubDer = client_keypub_der_1024;
  54338. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54339. unsigned char signature[1024/8];
  54340. #else
  54341. const unsigned char* privDer = client_key_der_2048;
  54342. size_t privDerSz = sizeof_client_key_der_2048;
  54343. const unsigned char* pubDer = client_keypub_der_2048;
  54344. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54345. unsigned char signature[2048/8];
  54346. #endif
  54347. unsigned int signatureLen;
  54348. const unsigned char* der;
  54349. unsigned char encodedHash[51];
  54350. unsigned int encodedHashLen;
  54351. const unsigned char expEncHash[] = {
  54352. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  54353. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  54354. 0x00, 0x04, 0x20,
  54355. /* Hash data */
  54356. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54357. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54359. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  54360. };
  54361. XMEMSET(hash, 0, sizeof(hash));
  54362. ExpectNotNull(rsa = wolfSSL_RSA_new());
  54363. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54364. &signatureLen, rsa, 1), 0);
  54365. wolfSSL_RSA_free(rsa);
  54366. der = privDer;
  54367. rsa = NULL;
  54368. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54369. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  54370. -1), 0);
  54371. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  54372. signature, &signatureLen, rsa, 1), 0);
  54373. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  54374. &signatureLen, rsa, 1), 0);
  54375. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  54376. &signatureLen, rsa, 1), 0);
  54377. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54378. NULL, rsa, 1), 0);
  54379. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54380. &signatureLen, NULL, 1), 0);
  54381. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54382. &signatureLen, rsa, -1), 0);
  54383. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  54384. &signatureLen, rsa, 0), 0);
  54385. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  54386. &signatureLen, rsa, 0), 0);
  54387. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54388. NULL, rsa, 0), 0);
  54389. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  54390. &signatureLen, rsa, 1), 1);
  54391. /* Test returning encoded hash. */
  54392. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  54393. &encodedHashLen, rsa, 0), 1);
  54394. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  54395. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  54396. RSA_free(rsa);
  54397. der = pubDer;
  54398. rsa = NULL;
  54399. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54400. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  54401. signatureLen, rsa), 1);
  54402. RSA_free(rsa);
  54403. #endif
  54404. return EXPECT_RESULT();
  54405. }
  54406. static int test_wolfSSL_RSA_public_decrypt(void)
  54407. {
  54408. EXPECT_DECLS;
  54409. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54410. RSA *rsa;
  54411. unsigned char msg[SHA256_DIGEST_LENGTH];
  54412. #ifdef USE_CERT_BUFFERS_1024
  54413. const unsigned char* pubDer = client_keypub_der_1024;
  54414. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54415. unsigned char decMsg[1024/8];
  54416. const unsigned char encMsg[] = {
  54417. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  54418. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  54419. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  54420. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  54421. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  54422. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  54423. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  54424. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  54425. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  54426. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  54427. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  54428. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  54429. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  54430. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  54431. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  54432. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  54433. };
  54434. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54435. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54436. defined(WC_RSA_NO_PADDING)
  54437. const unsigned char encMsgNoPad[] = {
  54438. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  54439. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  54440. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  54441. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  54442. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  54443. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  54444. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  54445. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  54446. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  54447. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  54448. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  54449. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  54450. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  54451. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  54452. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  54453. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  54454. };
  54455. #endif
  54456. #else
  54457. const unsigned char* pubDer = client_keypub_der_2048;
  54458. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54459. unsigned char decMsg[2048/8];
  54460. const unsigned char encMsg[] = {
  54461. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  54462. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  54463. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  54464. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  54465. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  54466. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  54467. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  54468. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  54469. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  54470. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  54471. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  54472. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  54473. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  54474. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  54475. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  54476. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  54477. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  54478. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  54479. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  54480. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  54481. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  54482. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  54483. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  54484. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  54485. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  54486. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  54487. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  54488. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  54489. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  54490. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  54491. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  54492. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  54493. };
  54494. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54495. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54496. defined(WC_RSA_NO_PADDING)
  54497. const unsigned char encMsgNoPad[] = {
  54498. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  54499. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  54500. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  54501. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  54502. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  54503. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  54504. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  54505. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  54506. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  54507. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  54508. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  54509. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  54510. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  54511. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  54512. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  54513. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  54514. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  54515. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  54516. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  54517. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  54518. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  54519. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  54520. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  54521. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  54522. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  54523. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  54524. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  54525. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  54526. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  54527. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  54528. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  54529. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  54530. };
  54531. #endif
  54532. #endif
  54533. const unsigned char* der;
  54534. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54535. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54536. defined(WC_RSA_NO_PADDING)
  54537. int i;
  54538. #endif
  54539. XMEMSET(msg, 0, sizeof(msg));
  54540. der = pubDer;
  54541. rsa = NULL;
  54542. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54543. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  54544. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  54545. RSA_PKCS1_PADDING), -1);
  54546. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  54547. RSA_PKCS1_PADDING), -1);
  54548. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  54549. RSA_PKCS1_PADDING), -1);
  54550. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  54551. RSA_PKCS1_PADDING), -1);
  54552. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  54553. RSA_PKCS1_PSS_PADDING), -1);
  54554. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  54555. RSA_PKCS1_PADDING), 32);
  54556. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  54557. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  54558. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  54559. defined(WC_RSA_NO_PADDING)
  54560. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  54561. rsa, RSA_NO_PADDING), sizeof(decMsg));
  54562. /* Zeros before actual data. */
  54563. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  54564. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  54565. }
  54566. /* Check actual data. */
  54567. XMEMSET(msg, 0x01, sizeof(msg));
  54568. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  54569. #endif
  54570. RSA_free(rsa);
  54571. #endif
  54572. return EXPECT_RESULT();
  54573. }
  54574. static int test_wolfSSL_RSA_private_encrypt(void)
  54575. {
  54576. EXPECT_DECLS;
  54577. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54578. RSA *rsa;
  54579. unsigned char msg[SHA256_DIGEST_LENGTH];
  54580. #ifdef USE_CERT_BUFFERS_1024
  54581. const unsigned char* privDer = client_key_der_1024;
  54582. size_t privDerSz = sizeof_client_key_der_1024;
  54583. unsigned char encMsg[1024/8];
  54584. const unsigned char expEncMsg[] = {
  54585. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  54586. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  54587. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  54588. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  54589. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  54590. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  54591. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  54592. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  54593. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  54594. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  54595. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  54596. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  54597. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  54598. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  54599. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  54600. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  54601. };
  54602. #ifdef WC_RSA_NO_PADDING
  54603. const unsigned char expEncMsgNoPad[] = {
  54604. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  54605. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  54606. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  54607. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  54608. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  54609. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  54610. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  54611. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  54612. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  54613. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  54614. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  54615. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  54616. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  54617. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  54618. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  54619. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  54620. };
  54621. #endif
  54622. #else
  54623. const unsigned char* privDer = client_key_der_2048;
  54624. size_t privDerSz = sizeof_client_key_der_2048;
  54625. unsigned char encMsg[2048/8];
  54626. const unsigned char expEncMsg[] = {
  54627. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  54628. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  54629. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  54630. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  54631. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  54632. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  54633. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  54634. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  54635. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  54636. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  54637. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  54638. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  54639. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  54640. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  54641. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  54642. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  54643. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  54644. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  54645. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  54646. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  54647. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  54648. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  54649. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  54650. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  54651. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  54652. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  54653. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  54654. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  54655. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  54656. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  54657. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  54658. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  54659. };
  54660. #ifdef WC_RSA_NO_PADDING
  54661. const unsigned char expEncMsgNoPad[] = {
  54662. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  54663. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  54664. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  54665. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  54666. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  54667. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  54668. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  54669. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  54670. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  54671. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  54672. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  54673. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  54674. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  54675. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  54676. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  54677. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  54678. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  54679. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  54680. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  54681. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  54682. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  54683. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  54684. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  54685. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  54686. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  54687. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  54688. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  54689. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  54690. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  54691. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  54692. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  54693. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  54694. };
  54695. #endif
  54696. #endif
  54697. const unsigned char* der;
  54698. XMEMSET(msg, 0x00, sizeof(msg));
  54699. der = privDer;
  54700. rsa = NULL;
  54701. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54702. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  54703. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  54704. -1);
  54705. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  54706. RSA_PKCS1_PADDING), -1);
  54707. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  54708. RSA_PKCS1_PADDING), -1);
  54709. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  54710. RSA_PKCS1_PADDING), -1);
  54711. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54712. RSA_PKCS1_PSS_PADDING), -1);
  54713. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54714. RSA_PKCS1_PADDING), sizeof(encMsg));
  54715. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  54716. #ifdef WC_RSA_NO_PADDING
  54717. /* Non-zero message. */
  54718. XMEMSET(msg, 0x01, sizeof(msg));
  54719. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  54720. RSA_NO_PADDING), sizeof(encMsg));
  54721. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  54722. #endif
  54723. RSA_free(rsa);
  54724. #endif
  54725. return EXPECT_RESULT();
  54726. }
  54727. static int test_wolfSSL_RSA_public_encrypt(void)
  54728. {
  54729. EXPECT_DECLS;
  54730. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54731. RSA* rsa = NULL;
  54732. const unsigned char msg[2048/8] = { 0 };
  54733. unsigned char encMsg[2048/8];
  54734. ExpectNotNull(rsa = RSA_new());
  54735. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  54736. RSA_PKCS1_PADDING), -1);
  54737. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  54738. RSA_PKCS1_PADDING), -1);
  54739. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  54740. RSA_PKCS1_PADDING), -1);
  54741. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  54742. RSA_PKCS1_PADDING), -1);
  54743. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  54744. RSA_PKCS1_PSS_PADDING), -1);
  54745. /* Empty RSA key. */
  54746. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  54747. RSA_PKCS1_PADDING), -1);
  54748. RSA_free(rsa);
  54749. #endif
  54750. return EXPECT_RESULT();
  54751. }
  54752. static int test_wolfSSL_RSA_private_decrypt(void)
  54753. {
  54754. EXPECT_DECLS;
  54755. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54756. RSA* rsa = NULL;
  54757. unsigned char msg[2048/8];
  54758. const unsigned char encMsg[2048/8] = { 0 };
  54759. ExpectNotNull(rsa = RSA_new());
  54760. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  54761. RSA_PKCS1_PADDING), -1);
  54762. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  54763. RSA_PKCS1_PADDING), -1);
  54764. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  54765. RSA_PKCS1_PADDING), -1);
  54766. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  54767. RSA_PKCS1_PADDING), -1);
  54768. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  54769. RSA_PKCS1_PSS_PADDING), -1);
  54770. /* Empty RSA key. */
  54771. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  54772. RSA_PKCS1_PADDING), -1);
  54773. RSA_free(rsa);
  54774. #endif
  54775. return EXPECT_RESULT();
  54776. }
  54777. static int test_wolfSSL_RSA_GenAdd(void)
  54778. {
  54779. EXPECT_DECLS;
  54780. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54781. RSA *rsa;
  54782. #ifdef USE_CERT_BUFFERS_1024
  54783. const unsigned char* privDer = client_key_der_1024;
  54784. size_t privDerSz = sizeof_client_key_der_1024;
  54785. const unsigned char* pubDer = client_keypub_der_1024;
  54786. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54787. #else
  54788. const unsigned char* privDer = client_key_der_2048;
  54789. size_t privDerSz = sizeof_client_key_der_2048;
  54790. const unsigned char* pubDer = client_keypub_der_2048;
  54791. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54792. #endif
  54793. const unsigned char* der;
  54794. der = privDer;
  54795. rsa = NULL;
  54796. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54797. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  54798. #ifndef RSA_LOW_MEM
  54799. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  54800. #else
  54801. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  54802. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  54803. #endif
  54804. RSA_free(rsa);
  54805. der = pubDer;
  54806. rsa = NULL;
  54807. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54808. /* Need private values. */
  54809. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  54810. RSA_free(rsa);
  54811. #endif
  54812. return EXPECT_RESULT();
  54813. }
  54814. static int test_wolfSSL_RSA_blinding_on(void)
  54815. {
  54816. EXPECT_DECLS;
  54817. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  54818. RSA *rsa;
  54819. WOLFSSL_BN_CTX *bnCtx = NULL;
  54820. #ifdef USE_CERT_BUFFERS_1024
  54821. const unsigned char* privDer = client_key_der_1024;
  54822. size_t privDerSz = sizeof_client_key_der_1024;
  54823. #else
  54824. const unsigned char* privDer = client_key_der_2048;
  54825. size_t privDerSz = sizeof_client_key_der_2048;
  54826. #endif
  54827. const unsigned char* der;
  54828. der = privDer;
  54829. rsa = NULL;
  54830. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54831. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  54832. /* Does nothing so all parameters are valid. */
  54833. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  54834. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  54835. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  54836. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  54837. wolfSSL_BN_CTX_free(bnCtx);
  54838. RSA_free(rsa);
  54839. #endif
  54840. return EXPECT_RESULT();
  54841. }
  54842. static int test_wolfSSL_RSA_ex_data(void)
  54843. {
  54844. EXPECT_DECLS;
  54845. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  54846. RSA* rsa = NULL;
  54847. unsigned char data[1];
  54848. ExpectNotNull(rsa = RSA_new());
  54849. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  54850. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  54851. #ifdef MAX_EX_DATA
  54852. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  54853. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  54854. #endif
  54855. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  54856. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  54857. #ifdef HAVE_EX_DATA
  54858. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  54859. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  54860. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  54861. #else
  54862. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  54863. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  54864. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  54865. #endif
  54866. RSA_free(rsa);
  54867. #endif /* !NO_RSA && OPENSSL_EXTRA */
  54868. return EXPECT_RESULT();
  54869. }
  54870. static int test_wolfSSL_RSA_LoadDer(void)
  54871. {
  54872. EXPECT_DECLS;
  54873. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  54874. defined(OPENSSL_EXTRA_X509_SMALL))
  54875. RSA *rsa = NULL;
  54876. #ifdef USE_CERT_BUFFERS_1024
  54877. const unsigned char* privDer = client_key_der_1024;
  54878. size_t privDerSz = sizeof_client_key_der_1024;
  54879. #else
  54880. const unsigned char* privDer = client_key_der_2048;
  54881. size_t privDerSz = sizeof_client_key_der_2048;
  54882. #endif
  54883. ExpectNotNull(rsa = RSA_new());
  54884. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  54885. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  54886. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  54887. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  54888. RSA_free(rsa);
  54889. #endif /* !NO_RSA && OPENSSL_EXTRA */
  54890. return EXPECT_RESULT();
  54891. }
  54892. /* Local API. */
  54893. static int test_wolfSSL_RSA_To_Der(void)
  54894. {
  54895. EXPECT_DECLS;
  54896. #ifdef WOLFSSL_TEST_STATIC_BUILD
  54897. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  54898. RSA* rsa;
  54899. #ifdef USE_CERT_BUFFERS_1024
  54900. const unsigned char* privDer = client_key_der_1024;
  54901. size_t privDerSz = sizeof_client_key_der_1024;
  54902. const unsigned char* pubDer = client_keypub_der_1024;
  54903. size_t pubDerSz = sizeof_client_keypub_der_1024;
  54904. unsigned char out[sizeof(client_key_der_1024)];
  54905. #else
  54906. const unsigned char* privDer = client_key_der_2048;
  54907. size_t privDerSz = sizeof_client_key_der_2048;
  54908. const unsigned char* pubDer = client_keypub_der_2048;
  54909. size_t pubDerSz = sizeof_client_keypub_der_2048;
  54910. unsigned char out[sizeof(client_key_der_2048)];
  54911. #endif
  54912. const unsigned char* der;
  54913. unsigned char* outDer = NULL;
  54914. der = privDer;
  54915. rsa = NULL;
  54916. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  54917. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54918. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  54919. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  54920. outDer = out;
  54921. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  54922. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  54923. outDer = NULL;
  54924. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  54925. ExpectNotNull(outDer);
  54926. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  54927. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  54928. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  54929. outDer = out;
  54930. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  54931. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  54932. RSA_free(rsa);
  54933. ExpectNotNull(rsa = RSA_new());
  54934. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54935. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  54936. RSA_free(rsa);
  54937. der = pubDer;
  54938. rsa = NULL;
  54939. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  54940. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  54941. RSA_free(rsa);
  54942. #endif
  54943. #endif
  54944. return EXPECT_RESULT();
  54945. }
  54946. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  54947. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  54948. {
  54949. EXPECT_DECLS;
  54950. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  54951. XFILE file = XBADFILE;
  54952. const char* fname = "./certs/server-keyPub.pem";
  54953. RSA *rsa = NULL;
  54954. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  54955. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  54956. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  54957. ExpectIntEQ(RSA_size(rsa), 256);
  54958. RSA_free(rsa);
  54959. if (file != XBADFILE)
  54960. XFCLOSE(file);
  54961. #endif
  54962. return EXPECT_RESULT();
  54963. }
  54964. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  54965. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  54966. {
  54967. EXPECT_DECLS;
  54968. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  54969. defined(WOLFSSL_KEY_GEN)
  54970. RSA* rsa = NULL;
  54971. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  54972. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  54973. /* Valid but stub so returns 0. */
  54974. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  54975. #endif
  54976. return EXPECT_RESULT();
  54977. }
  54978. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  54979. {
  54980. EXPECT_DECLS;
  54981. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  54982. (defined(WOLFSSL_PEM_TO_DER) || \
  54983. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  54984. RSA* rsa = NULL;
  54985. #ifdef USE_CERT_BUFFERS_1024
  54986. const unsigned char* privDer = client_key_der_1024;
  54987. size_t privDerSz = sizeof_client_key_der_1024;
  54988. #else
  54989. const unsigned char* privDer = client_key_der_2048;
  54990. size_t privDerSz = sizeof_client_key_der_2048;
  54991. #endif
  54992. const unsigned char* der;
  54993. #ifndef NO_AES
  54994. unsigned char passwd[] = "password";
  54995. #endif
  54996. ExpectNotNull(rsa = RSA_new());
  54997. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  54998. NULL, NULL), 0);
  54999. RSA_free(rsa);
  55000. der = privDer;
  55001. rsa = NULL;
  55002. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  55003. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  55004. NULL, NULL), 0);
  55005. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  55006. NULL, NULL), 0);
  55007. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  55008. NULL, NULL), 1);
  55009. #ifndef NO_AES
  55010. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  55011. NULL, 0, NULL, NULL), 1);
  55012. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  55013. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  55014. #endif
  55015. RSA_free(rsa);
  55016. #endif
  55017. return EXPECT_RESULT();
  55018. }
  55019. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  55020. {
  55021. EXPECT_DECLS;
  55022. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  55023. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  55024. RSA* rsa = NULL;
  55025. #ifdef USE_CERT_BUFFERS_1024
  55026. const unsigned char* privDer = client_key_der_1024;
  55027. size_t privDerSz = sizeof_client_key_der_1024;
  55028. #else
  55029. const unsigned char* privDer = client_key_der_2048;
  55030. size_t privDerSz = sizeof_client_key_der_2048;
  55031. #endif
  55032. const unsigned char* der;
  55033. #ifndef NO_AES
  55034. unsigned char passwd[] = "password";
  55035. #endif
  55036. unsigned char* pem = NULL;
  55037. int plen;
  55038. ExpectNotNull(rsa = RSA_new());
  55039. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55040. &plen), 0);
  55041. RSA_free(rsa);
  55042. der = privDer;
  55043. rsa = NULL;
  55044. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  55045. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  55046. &plen), 0);
  55047. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  55048. &plen), 0);
  55049. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55050. NULL), 0);
  55051. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  55052. &plen), 1);
  55053. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55054. pem = NULL;
  55055. #ifndef NO_AES
  55056. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  55057. NULL, 0, &pem, &plen), 1);
  55058. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55059. pem = NULL;
  55060. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  55061. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  55062. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  55063. #endif
  55064. RSA_free(rsa);
  55065. #endif
  55066. return EXPECT_RESULT();
  55067. }
  55068. static int test_wolfSSL_DH(void)
  55069. {
  55070. EXPECT_DECLS;
  55071. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55072. DH *dh = NULL;
  55073. BIGNUM* p;
  55074. BIGNUM* q;
  55075. BIGNUM* g;
  55076. BIGNUM* pub = NULL;
  55077. BIGNUM* priv = NULL;
  55078. #if defined(OPENSSL_ALL)
  55079. #if !defined(HAVE_FIPS) || \
  55080. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  55081. FILE* f = NULL;
  55082. unsigned char buf[268];
  55083. const unsigned char* pt = buf;
  55084. long len = 0;
  55085. dh = NULL;
  55086. XMEMSET(buf, 0, sizeof(buf));
  55087. /* Test 2048 bit parameters */
  55088. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  55089. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55090. if (f != XBADFILE)
  55091. XFCLOSE(f);
  55092. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  55093. ExpectNotNull(dh->p);
  55094. ExpectNotNull(dh->g);
  55095. ExpectTrue(pt == buf);
  55096. ExpectIntEQ(DH_generate_key(dh), 1);
  55097. ExpectIntEQ(DH_generate_key(dh), 1);
  55098. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  55099. ExpectNotNull(pub = BN_new());
  55100. ExpectIntEQ(BN_set_word(pub, 1), 1);
  55101. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  55102. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  55103. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  55104. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  55105. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  55106. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  55107. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  55108. BN_free(pub);
  55109. pub = NULL;
  55110. DH_get0_pqg(dh, (const BIGNUM**)&p,
  55111. (const BIGNUM**)&q,
  55112. (const BIGNUM**)&g);
  55113. ExpectPtrEq(p, dh->p);
  55114. ExpectPtrEq(q, dh->q);
  55115. ExpectPtrEq(g, dh->g);
  55116. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  55117. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  55118. ExpectPtrEq(pub, dh->pub_key);
  55119. ExpectPtrEq(priv, dh->priv_key);
  55120. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  55121. ExpectPtrEq(pub, dh->pub_key);
  55122. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  55123. ExpectPtrEq(priv, dh->priv_key);
  55124. pub = NULL;
  55125. priv = NULL;
  55126. ExpectNotNull(pub = BN_new());
  55127. ExpectNotNull(priv = BN_new());
  55128. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  55129. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  55130. if (EXPECT_FAIL()) {
  55131. BN_free(pub);
  55132. BN_free(priv);
  55133. }
  55134. pub = NULL;
  55135. priv = NULL;
  55136. ExpectNotNull(pub = BN_new());
  55137. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  55138. if (EXPECT_FAIL()) {
  55139. BN_free(pub);
  55140. }
  55141. ExpectNotNull(priv = BN_new());
  55142. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  55143. if (EXPECT_FAIL()) {
  55144. BN_free(priv);
  55145. }
  55146. ExpectPtrEq(pub, dh->pub_key);
  55147. ExpectPtrEq(priv, dh->priv_key);
  55148. pub = NULL;
  55149. priv = NULL;
  55150. DH_free(dh);
  55151. dh = NULL;
  55152. ExpectNotNull(dh = DH_new());
  55153. p = NULL;
  55154. ExpectNotNull(p = BN_new());
  55155. ExpectIntEQ(BN_set_word(p, 1), 1);
  55156. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  55157. ExpectNotNull(pub = BN_new());
  55158. ExpectNotNull(priv = BN_new());
  55159. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  55160. if (EXPECT_FAIL()) {
  55161. BN_free(pub);
  55162. BN_free(priv);
  55163. }
  55164. pub = NULL;
  55165. priv = NULL;
  55166. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  55167. BN_free(p);
  55168. p = NULL;
  55169. DH_free(dh);
  55170. dh = NULL;
  55171. #ifdef WOLFSSL_KEY_GEN
  55172. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  55173. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  55174. DH_free(dh);
  55175. dh = NULL;
  55176. #endif
  55177. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  55178. #endif /* OPENSSL_ALL */
  55179. (void)dh;
  55180. (void)p;
  55181. (void)q;
  55182. (void)g;
  55183. (void)pub;
  55184. (void)priv;
  55185. ExpectNotNull(dh = wolfSSL_DH_new());
  55186. /* invalid parameters test */
  55187. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  55188. (const BIGNUM**)&q,
  55189. (const BIGNUM**)&g);
  55190. DH_get0_pqg(dh, NULL,
  55191. (const BIGNUM**)&q,
  55192. (const BIGNUM**)&g);
  55193. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  55194. DH_get0_pqg(dh, NULL, NULL, NULL);
  55195. DH_get0_pqg(dh, (const BIGNUM**)&p,
  55196. (const BIGNUM**)&q,
  55197. (const BIGNUM**)&g);
  55198. ExpectPtrEq(p, NULL);
  55199. ExpectPtrEq(q, NULL);
  55200. ExpectPtrEq(g, NULL);
  55201. DH_free(dh);
  55202. dh = NULL;
  55203. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  55204. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  55205. #if defined(OPENSSL_ALL) || \
  55206. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55207. dh = wolfSSL_DH_new();
  55208. ExpectNotNull(dh);
  55209. p = wolfSSL_BN_new();
  55210. ExpectNotNull(p);
  55211. ExpectIntEQ(BN_set_word(p, 11), 1);
  55212. g = wolfSSL_BN_new();
  55213. ExpectNotNull(g);
  55214. ExpectIntEQ(BN_set_word(g, 2), 1);
  55215. q = wolfSSL_BN_new();
  55216. ExpectNotNull(q);
  55217. ExpectIntEQ(BN_set_word(q, 5), 1);
  55218. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  55219. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  55220. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  55221. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  55222. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  55223. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  55224. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  55225. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  55226. /* Don't need q. */
  55227. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55228. if (EXPECT_FAIL()) {
  55229. BN_free(p);
  55230. BN_free(g);
  55231. }
  55232. p = NULL;
  55233. g = NULL;
  55234. /* Setting again will free the p and g. */
  55235. wolfSSL_BN_free(q);
  55236. q = NULL;
  55237. DH_free(dh);
  55238. dh = NULL;
  55239. dh = wolfSSL_DH_new();
  55240. ExpectNotNull(dh);
  55241. p = wolfSSL_BN_new();
  55242. ExpectNotNull(p);
  55243. ExpectIntEQ(BN_set_word(p, 11), 1);
  55244. g = wolfSSL_BN_new();
  55245. ExpectNotNull(g);
  55246. ExpectIntEQ(BN_set_word(g, 2), 1);
  55247. q = wolfSSL_BN_new();
  55248. ExpectNotNull(q);
  55249. ExpectIntEQ(BN_set_word(q, 5), 1);
  55250. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  55251. /* p, q and g are now owned by dh - don't free. */
  55252. if (EXPECT_FAIL()) {
  55253. BN_free(p);
  55254. BN_free(q);
  55255. BN_free(g);
  55256. }
  55257. p = NULL;
  55258. q = NULL;
  55259. g = NULL;
  55260. p = wolfSSL_BN_new();
  55261. ExpectNotNull(p);
  55262. ExpectIntEQ(BN_set_word(p, 11), 1);
  55263. g = wolfSSL_BN_new();
  55264. ExpectNotNull(g);
  55265. ExpectIntEQ(BN_set_word(g, 2), 1);
  55266. q = wolfSSL_BN_new();
  55267. ExpectNotNull(q);
  55268. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  55269. if (EXPECT_FAIL()) {
  55270. BN_free(p);
  55271. }
  55272. p = NULL;
  55273. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  55274. if (EXPECT_FAIL()) {
  55275. BN_free(q);
  55276. }
  55277. q = NULL;
  55278. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  55279. if (EXPECT_FAIL()) {
  55280. BN_free(g);
  55281. }
  55282. g = NULL;
  55283. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  55284. /* p, q and g are now owned by dh - don't free. */
  55285. DH_free(dh);
  55286. dh = NULL;
  55287. ExpectIntEQ(DH_generate_key(NULL), 0);
  55288. ExpectNotNull(dh = DH_new());
  55289. ExpectIntEQ(DH_generate_key(dh), 0);
  55290. p = wolfSSL_BN_new();
  55291. ExpectNotNull(p);
  55292. ExpectIntEQ(BN_set_word(p, 0), 1);
  55293. g = wolfSSL_BN_new();
  55294. ExpectNotNull(g);
  55295. ExpectIntEQ(BN_set_word(g, 2), 1);
  55296. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55297. if (EXPECT_FAIL()) {
  55298. BN_free(p);
  55299. BN_free(g);
  55300. }
  55301. p = NULL;
  55302. g = NULL;
  55303. ExpectIntEQ(DH_generate_key(dh), 0);
  55304. DH_free(dh);
  55305. dh = NULL;
  55306. #endif
  55307. #endif
  55308. /* Test DH_up_ref() */
  55309. dh = wolfSSL_DH_new();
  55310. ExpectNotNull(dh);
  55311. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  55312. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  55313. DH_free(dh); /* decrease ref count */
  55314. DH_free(dh); /* free WOLFSSL_DH */
  55315. dh = NULL;
  55316. q = NULL;
  55317. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  55318. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  55319. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  55320. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  55321. #ifdef HAVE_FFDHE_2048
  55322. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  55323. DH_free(dh);
  55324. dh = NULL;
  55325. q = NULL;
  55326. #endif
  55327. #ifdef HAVE_FFDHE_3072
  55328. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  55329. DH_free(dh);
  55330. dh = NULL;
  55331. q = NULL;
  55332. #endif
  55333. #ifdef HAVE_FFDHE_4096
  55334. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  55335. DH_free(dh);
  55336. dh = NULL;
  55337. q = NULL;
  55338. #endif
  55339. #else
  55340. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  55341. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  55342. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  55343. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  55344. #endif /* OPENSSL_EXTRA && !NO_DH */
  55345. return EXPECT_RESULT();
  55346. }
  55347. static int test_wolfSSL_DH_dup(void)
  55348. {
  55349. EXPECT_DECLS;
  55350. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  55351. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  55352. defined(OPENSSL_EXTRA)
  55353. DH *dh = NULL;
  55354. DH *dhDup = NULL;
  55355. ExpectNotNull(dh = wolfSSL_DH_new());
  55356. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  55357. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  55358. #if defined(OPENSSL_ALL) || \
  55359. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  55360. {
  55361. WOLFSSL_BIGNUM* p = NULL;
  55362. WOLFSSL_BIGNUM* g = NULL;
  55363. ExpectNotNull(p = wolfSSL_BN_new());
  55364. ExpectNotNull(g = wolfSSL_BN_new());
  55365. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  55366. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  55367. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  55368. if (EXPECT_FAIL()) {
  55369. wolfSSL_BN_free(p);
  55370. wolfSSL_BN_free(g);
  55371. }
  55372. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  55373. wolfSSL_DH_free(dhDup);
  55374. }
  55375. #endif
  55376. wolfSSL_DH_free(dh);
  55377. #endif
  55378. #endif
  55379. return EXPECT_RESULT();
  55380. }
  55381. static int test_wolfSSL_DH_check(void)
  55382. {
  55383. EXPECT_DECLS;
  55384. #ifdef OPENSSL_ALL
  55385. #ifndef NO_DH
  55386. #ifndef NO_BIO
  55387. #ifndef NO_DSA
  55388. byte buf[6000];
  55389. char file[] = "./certs/dsaparams.pem";
  55390. XFILE f = XBADFILE;
  55391. int bytes;
  55392. BIO* bio = NULL;
  55393. DSA* dsa = NULL;
  55394. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  55395. static const byte dh2048[] = {
  55396. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  55397. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  55398. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  55399. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  55400. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  55401. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  55402. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  55403. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  55404. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  55405. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  55406. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  55407. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  55408. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  55409. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  55410. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  55411. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  55412. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  55413. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  55414. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  55415. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  55416. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  55417. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  55418. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  55419. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  55420. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  55421. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  55422. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  55423. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  55424. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  55425. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  55426. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  55427. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  55428. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  55429. 0x93, 0x02, 0x01, 0x02
  55430. };
  55431. const byte* params;
  55432. #endif
  55433. DH* dh = NULL;
  55434. WOLFSSL_BIGNUM* p = NULL;
  55435. WOLFSSL_BIGNUM* g = NULL;
  55436. WOLFSSL_BIGNUM* pTmp = NULL;
  55437. WOLFSSL_BIGNUM* gTmp = NULL;
  55438. int codes = -1;
  55439. #ifndef NO_DSA
  55440. /* Initialize DH */
  55441. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  55442. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  55443. if (f != XBADFILE)
  55444. XFCLOSE(f);
  55445. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  55446. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  55447. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  55448. ExpectNotNull(dh);
  55449. BIO_free(bio);
  55450. DSA_free(dsa);
  55451. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  55452. params = dh2048;
  55453. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  55454. (long)sizeof(dh2048)));
  55455. #else
  55456. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  55457. #endif
  55458. /* Test assumed to be valid dh.
  55459. * Should return WOLFSSL_SUCCESS
  55460. * codes should be 0
  55461. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  55462. */
  55463. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55464. ExpectIntEQ(codes, 0);
  55465. /* Test NULL dh: expected BAD_FUNC_ARG */
  55466. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  55467. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  55468. if (dh != NULL) {
  55469. pTmp = dh->p;
  55470. dh->p = NULL;
  55471. }
  55472. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55473. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55474. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  55475. /* set dh->p back to normal so it won't fail on next tests */
  55476. if (dh != NULL) {
  55477. dh->p = pTmp;
  55478. pTmp = NULL;
  55479. }
  55480. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  55481. if (dh != NULL) {
  55482. gTmp = dh->g;
  55483. dh->g = NULL;
  55484. }
  55485. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55486. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55487. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  55488. if (dh != NULL) {
  55489. dh->g = gTmp;
  55490. gTmp = NULL;
  55491. }
  55492. /* Cleanup */
  55493. DH_free(dh);
  55494. dh = NULL;
  55495. dh = DH_new();
  55496. ExpectNotNull(dh);
  55497. /* Check empty DH. */
  55498. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55499. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55500. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  55501. /* Check non-prime valued p. */
  55502. ExpectNotNull(p = BN_new());
  55503. ExpectIntEQ(BN_set_word(p, 4), 1);
  55504. ExpectNotNull(g = BN_new());
  55505. ExpectIntEQ(BN_set_word(g, 2), 1);
  55506. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  55507. if (EXPECT_FAIL()) {
  55508. wolfSSL_BN_free(p);
  55509. wolfSSL_BN_free(g);
  55510. }
  55511. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  55512. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  55513. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  55514. DH_free(dh);
  55515. dh = NULL;
  55516. #endif
  55517. #endif /* !NO_DH && !NO_DSA */
  55518. #endif
  55519. return EXPECT_RESULT();
  55520. }
  55521. static int test_wolfSSL_DH_prime(void)
  55522. {
  55523. EXPECT_DECLS;
  55524. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55525. WOLFSSL_BIGNUM* bn = NULL;
  55526. #if WOLFSSL_MAX_BN_BITS >= 768
  55527. WOLFSSL_BIGNUM* bn2 = NULL;
  55528. #endif
  55529. bn = wolfSSL_DH_768_prime(NULL);
  55530. #if WOLFSSL_MAX_BN_BITS >= 768
  55531. ExpectNotNull(bn);
  55532. bn2 = wolfSSL_DH_768_prime(bn);
  55533. ExpectNotNull(bn2);
  55534. ExpectTrue(bn == bn2);
  55535. wolfSSL_BN_free(bn);
  55536. bn = NULL;
  55537. #else
  55538. ExpectNull(bn);
  55539. #endif
  55540. bn = wolfSSL_DH_1024_prime(NULL);
  55541. #if WOLFSSL_MAX_BN_BITS >= 1024
  55542. ExpectNotNull(bn);
  55543. wolfSSL_BN_free(bn);
  55544. bn = NULL;
  55545. #else
  55546. ExpectNull(bn);
  55547. #endif
  55548. bn = wolfSSL_DH_2048_prime(NULL);
  55549. #if WOLFSSL_MAX_BN_BITS >= 2048
  55550. ExpectNotNull(bn);
  55551. wolfSSL_BN_free(bn);
  55552. bn = NULL;
  55553. #else
  55554. ExpectNull(bn);
  55555. #endif
  55556. bn = wolfSSL_DH_3072_prime(NULL);
  55557. #if WOLFSSL_MAX_BN_BITS >= 3072
  55558. ExpectNotNull(bn);
  55559. wolfSSL_BN_free(bn);
  55560. bn = NULL;
  55561. #else
  55562. ExpectNull(bn);
  55563. #endif
  55564. bn = wolfSSL_DH_4096_prime(NULL);
  55565. #if WOLFSSL_MAX_BN_BITS >= 4096
  55566. ExpectNotNull(bn);
  55567. wolfSSL_BN_free(bn);
  55568. bn = NULL;
  55569. #else
  55570. ExpectNull(bn);
  55571. #endif
  55572. bn = wolfSSL_DH_6144_prime(NULL);
  55573. #if WOLFSSL_MAX_BN_BITS >= 6144
  55574. ExpectNotNull(bn);
  55575. wolfSSL_BN_free(bn);
  55576. bn = NULL;
  55577. #else
  55578. ExpectNull(bn);
  55579. #endif
  55580. bn = wolfSSL_DH_8192_prime(NULL);
  55581. #if WOLFSSL_MAX_BN_BITS >= 8192
  55582. ExpectNotNull(bn);
  55583. wolfSSL_BN_free(bn);
  55584. bn = NULL;
  55585. #else
  55586. ExpectNull(bn);
  55587. #endif
  55588. #endif
  55589. return EXPECT_RESULT();
  55590. }
  55591. static int test_wolfSSL_DH_1536_prime(void)
  55592. {
  55593. EXPECT_DECLS;
  55594. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55595. BIGNUM* bn = NULL;
  55596. unsigned char bits[200];
  55597. int sz = 192; /* known binary size */
  55598. const byte expected[] = {
  55599. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  55600. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  55601. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  55602. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  55603. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  55604. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  55605. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  55606. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  55607. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  55608. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  55609. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  55610. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  55611. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  55612. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  55613. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  55614. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  55615. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  55616. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  55617. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  55618. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  55619. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  55620. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  55621. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  55622. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  55623. };
  55624. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  55625. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  55626. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  55627. BN_free(bn);
  55628. #endif
  55629. return EXPECT_RESULT();
  55630. }
  55631. static int test_wolfSSL_DH_get_2048_256(void)
  55632. {
  55633. EXPECT_DECLS;
  55634. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  55635. WOLFSSL_DH* dh = NULL;
  55636. const WOLFSSL_BIGNUM* pBn;
  55637. const WOLFSSL_BIGNUM* gBn;
  55638. const WOLFSSL_BIGNUM* qBn;
  55639. const byte pExpected[] = {
  55640. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  55641. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  55642. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  55643. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  55644. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  55645. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  55646. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  55647. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  55648. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  55649. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  55650. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  55651. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  55652. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  55653. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  55654. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  55655. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  55656. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  55657. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  55658. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  55659. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  55660. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  55661. 0x1E, 0x1A, 0x15, 0x97
  55662. };
  55663. const byte gExpected[] = {
  55664. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  55665. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  55666. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  55667. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  55668. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  55669. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  55670. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  55671. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  55672. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  55673. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  55674. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  55675. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  55676. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  55677. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  55678. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  55679. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  55680. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  55681. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  55682. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  55683. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  55684. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  55685. 0x6C, 0xC4, 0x16, 0x59
  55686. };
  55687. const byte qExpected[] = {
  55688. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  55689. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  55690. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  55691. };
  55692. int pSz = 0;
  55693. int qSz = 0;
  55694. int gSz = 0;
  55695. byte* pReturned = NULL;
  55696. byte* qReturned = NULL;
  55697. byte* gReturned = NULL;
  55698. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  55699. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  55700. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  55701. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  55702. DYNAMIC_TYPE_TMP_BUFFER));
  55703. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  55704. ExpectIntEQ(pSz, sizeof(pExpected));
  55705. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  55706. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  55707. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  55708. DYNAMIC_TYPE_TMP_BUFFER));
  55709. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  55710. ExpectIntEQ(qSz, sizeof(qExpected));
  55711. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  55712. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  55713. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  55714. DYNAMIC_TYPE_TMP_BUFFER));
  55715. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  55716. ExpectIntEQ(gSz, sizeof(gExpected));
  55717. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  55718. wolfSSL_DH_free(dh);
  55719. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55720. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55721. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55722. #endif
  55723. return EXPECT_RESULT();
  55724. }
  55725. static int test_wolfSSL_PEM_write_DHparams(void)
  55726. {
  55727. EXPECT_DECLS;
  55728. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  55729. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  55730. DH* dh = NULL;
  55731. BIO* bio = NULL;
  55732. XFILE fp = XBADFILE;
  55733. byte pem[2048];
  55734. int pemSz = 0;
  55735. const char expected[] =
  55736. "-----BEGIN DH PARAMETERS-----\n"
  55737. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  55738. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  55739. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  55740. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  55741. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  55742. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  55743. "-----END DH PARAMETERS-----\n";
  55744. const char badPem[] =
  55745. "-----BEGIN DH PARAMETERS-----\n"
  55746. "-----END DH PARAMETERS-----\n";
  55747. const char emptySeqPem[] =
  55748. "-----BEGIN DH PARAMETERS-----\n"
  55749. "MAA=\n"
  55750. "-----END DH PARAMETERS-----\n";
  55751. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  55752. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  55753. if (fp != XBADFILE) {
  55754. XFCLOSE(fp);
  55755. fp = XBADFILE;
  55756. }
  55757. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  55758. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55759. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55760. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  55761. (int)sizeof(badPem));
  55762. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55763. BIO_free(bio);
  55764. bio = NULL;
  55765. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55766. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55767. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  55768. (int)sizeof(emptySeqPem));
  55769. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55770. BIO_free(bio);
  55771. bio = NULL;
  55772. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  55773. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  55774. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  55775. BIO_free(bio);
  55776. bio = NULL;
  55777. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  55778. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  55779. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  55780. DH_free(dh);
  55781. dh = NULL;
  55782. dh = wolfSSL_DH_new();
  55783. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  55784. if (fp != XBADFILE) {
  55785. XFCLOSE(fp);
  55786. fp = XBADFILE;
  55787. }
  55788. wolfSSL_DH_free(dh);
  55789. dh = NULL;
  55790. /* check results */
  55791. XMEMSET(pem, 0, sizeof(pem));
  55792. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  55793. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  55794. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  55795. if (fp != XBADFILE)
  55796. XFCLOSE(fp);
  55797. #endif
  55798. return EXPECT_RESULT();
  55799. }
  55800. static int test_wolfSSL_d2i_DHparams(void)
  55801. {
  55802. EXPECT_DECLS;
  55803. #ifdef OPENSSL_ALL
  55804. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  55805. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55806. XFILE f = XBADFILE;
  55807. unsigned char buf[4096];
  55808. const unsigned char* pt = buf;
  55809. #ifdef HAVE_FFDHE_2048
  55810. const char* params1 = "./certs/dh2048.der";
  55811. #endif
  55812. #ifdef HAVE_FFDHE_3072
  55813. const char* params2 = "./certs/dh3072.der";
  55814. #endif
  55815. long len = 0;
  55816. WOLFSSL_DH* dh = NULL;
  55817. XMEMSET(buf, 0, sizeof(buf));
  55818. /* Test 2048 bit parameters */
  55819. #ifdef HAVE_FFDHE_2048
  55820. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  55821. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55822. if (f != XBADFILE) {
  55823. XFCLOSE(f);
  55824. f = XBADFILE;
  55825. }
  55826. /* Valid case */
  55827. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55828. ExpectNotNull(dh->p);
  55829. ExpectNotNull(dh->g);
  55830. ExpectTrue(pt == buf);
  55831. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  55832. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  55833. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  55834. /* Invalid cases */
  55835. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  55836. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  55837. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  55838. DH_free(dh);
  55839. dh = NULL;
  55840. *buf = 0;
  55841. pt = buf;
  55842. #endif /* HAVE_FFDHE_2048 */
  55843. /* Test 3072 bit parameters */
  55844. #ifdef HAVE_FFDHE_3072
  55845. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  55846. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55847. if (f != XBADFILE) {
  55848. XFCLOSE(f);
  55849. f = XBADFILE;
  55850. }
  55851. /* Valid case */
  55852. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  55853. ExpectNotNull(dh->p);
  55854. ExpectNotNull(dh->g);
  55855. ExpectTrue(pt != buf);
  55856. ExpectIntEQ(DH_generate_key(dh), 1);
  55857. /* Invalid cases */
  55858. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  55859. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  55860. DH_free(dh);
  55861. dh = NULL;
  55862. #endif /* HAVE_FFDHE_3072 */
  55863. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55864. #endif /* !NO_DH */
  55865. #endif
  55866. return EXPECT_RESULT();
  55867. }
  55868. static int test_wolfSSL_DH_LoadDer(void)
  55869. {
  55870. EXPECT_DECLS;
  55871. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  55872. defined(OPENSSL_EXTRA)
  55873. static const byte dh2048[] = {
  55874. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  55875. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  55876. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  55877. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  55878. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  55879. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  55880. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  55881. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  55882. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  55883. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  55884. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  55885. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  55886. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  55887. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  55888. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  55889. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  55890. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  55891. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  55892. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  55893. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  55894. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  55895. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  55896. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  55897. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  55898. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  55899. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  55900. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  55901. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  55902. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  55903. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  55904. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  55905. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  55906. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  55907. 0x93, 0x02, 0x01, 0x02
  55908. };
  55909. WOLFSSL_DH* dh = NULL;
  55910. ExpectNotNull(dh = wolfSSL_DH_new());
  55911. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  55912. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  55913. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  55914. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  55915. wolfSSL_DH_free(dh);
  55916. #endif
  55917. return EXPECT_RESULT();
  55918. }
  55919. static int test_wolfSSL_i2d_DHparams(void)
  55920. {
  55921. EXPECT_DECLS;
  55922. #ifdef OPENSSL_ALL
  55923. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  55924. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55925. XFILE f = XBADFILE;
  55926. unsigned char buf[4096];
  55927. const unsigned char* pt;
  55928. unsigned char* pt2;
  55929. #ifdef HAVE_FFDHE_2048
  55930. const char* params1 = "./certs/dh2048.der";
  55931. #endif
  55932. #ifdef HAVE_FFDHE_3072
  55933. const char* params2 = "./certs/dh3072.der";
  55934. #endif
  55935. long len = 0;
  55936. WOLFSSL_DH* dh = NULL;
  55937. /* Test 2048 bit parameters */
  55938. #ifdef HAVE_FFDHE_2048
  55939. pt = buf;
  55940. pt2 = buf;
  55941. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  55942. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55943. if (f != XBADFILE) {
  55944. XFCLOSE(f);
  55945. f = XBADFILE;
  55946. }
  55947. /* Valid case */
  55948. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55949. ExpectTrue(pt == buf);
  55950. ExpectIntEQ(DH_generate_key(dh), 1);
  55951. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  55952. /* Invalid case */
  55953. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  55954. /* Return length only */
  55955. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  55956. DH_free(dh);
  55957. dh = NULL;
  55958. *buf = 0;
  55959. #endif
  55960. /* Test 3072 bit parameters */
  55961. #ifdef HAVE_FFDHE_3072
  55962. pt = buf;
  55963. pt2 = buf;
  55964. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  55965. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  55966. if (f != XBADFILE) {
  55967. XFCLOSE(f);
  55968. f = XBADFILE;
  55969. }
  55970. /* Valid case */
  55971. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  55972. ExpectTrue(pt == buf);
  55973. ExpectIntEQ(DH_generate_key(dh), 1);
  55974. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  55975. /* Invalid case */
  55976. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  55977. /* Return length only */
  55978. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  55979. DH_free(dh);
  55980. dh = NULL;
  55981. #endif
  55982. dh = DH_new();
  55983. ExpectNotNull(dh);
  55984. pt2 = buf;
  55985. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  55986. DH_free(dh);
  55987. dh = NULL;
  55988. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55989. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  55990. #endif
  55991. return EXPECT_RESULT();
  55992. }
  55993. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  55994. /*----------------------------------------------------------------------------*
  55995. | EC
  55996. *----------------------------------------------------------------------------*/
  55997. static int test_wolfSSL_EC_GROUP(void)
  55998. {
  55999. EXPECT_DECLS;
  56000. #ifdef OPENSSL_EXTRA
  56001. EC_GROUP *group = NULL;
  56002. EC_GROUP *group2 = NULL;
  56003. EC_GROUP *group3 = NULL;
  56004. #ifndef HAVE_ECC_BRAINPOOL
  56005. EC_GROUP *group4 = NULL;
  56006. #endif
  56007. WOLFSSL_BIGNUM* order = NULL;
  56008. int group_bits;
  56009. int i;
  56010. static const int knownEccNids[] = {
  56011. NID_X9_62_prime192v1,
  56012. NID_X9_62_prime192v2,
  56013. NID_X9_62_prime192v3,
  56014. NID_X9_62_prime239v1,
  56015. NID_X9_62_prime239v2,
  56016. NID_X9_62_prime239v3,
  56017. NID_X9_62_prime256v1,
  56018. NID_secp112r1,
  56019. NID_secp112r2,
  56020. NID_secp128r1,
  56021. NID_secp128r2,
  56022. NID_secp160r1,
  56023. NID_secp160r2,
  56024. NID_secp224r1,
  56025. NID_secp384r1,
  56026. NID_secp521r1,
  56027. NID_secp160k1,
  56028. NID_secp192k1,
  56029. NID_secp224k1,
  56030. NID_secp256k1,
  56031. NID_brainpoolP160r1,
  56032. NID_brainpoolP192r1,
  56033. NID_brainpoolP224r1,
  56034. NID_brainpoolP256r1,
  56035. NID_brainpoolP320r1,
  56036. NID_brainpoolP384r1,
  56037. NID_brainpoolP512r1,
  56038. };
  56039. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  56040. static const int knownEccEnums[] = {
  56041. ECC_SECP192R1,
  56042. ECC_PRIME192V2,
  56043. ECC_PRIME192V3,
  56044. ECC_PRIME239V1,
  56045. ECC_PRIME239V2,
  56046. ECC_PRIME239V3,
  56047. ECC_SECP256R1,
  56048. ECC_SECP112R1,
  56049. ECC_SECP112R2,
  56050. ECC_SECP128R1,
  56051. ECC_SECP128R2,
  56052. ECC_SECP160R1,
  56053. ECC_SECP160R2,
  56054. ECC_SECP224R1,
  56055. ECC_SECP384R1,
  56056. ECC_SECP521R1,
  56057. ECC_SECP160K1,
  56058. ECC_SECP192K1,
  56059. ECC_SECP224K1,
  56060. ECC_SECP256K1,
  56061. ECC_BRAINPOOLP160R1,
  56062. ECC_BRAINPOOLP192R1,
  56063. ECC_BRAINPOOLP224R1,
  56064. ECC_BRAINPOOLP256R1,
  56065. ECC_BRAINPOOLP320R1,
  56066. ECC_BRAINPOOLP384R1,
  56067. ECC_BRAINPOOLP512R1,
  56068. };
  56069. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  56070. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56071. ExpectNotNull(group2 = EC_GROUP_dup(group));
  56072. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  56073. #ifndef HAVE_ECC_BRAINPOOL
  56074. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  56075. NID_brainpoolP256r1));
  56076. #endif
  56077. ExpectNull(EC_GROUP_dup(NULL));
  56078. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  56079. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  56080. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  56081. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  56082. #ifndef HAVE_ECC_BRAINPOOL
  56083. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  56084. #endif
  56085. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  56086. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  56087. ExpectNotNull(order = BN_new());
  56088. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  56089. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  56090. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  56091. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  56092. wolfSSL_BN_free(order);
  56093. ExpectNotNull(EC_GROUP_method_of(group));
  56094. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  56095. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  56096. NID_X9_62_prime_field);
  56097. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  56098. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  56099. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  56100. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  56101. #ifndef NO_WOLFSSL_STUB
  56102. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  56103. #endif
  56104. #ifndef HAVE_ECC_BRAINPOOL
  56105. EC_GROUP_free(group4);
  56106. #endif
  56107. EC_GROUP_free(group3);
  56108. EC_GROUP_free(group2);
  56109. EC_GROUP_free(group);
  56110. for (i = 0; i < knowEccNidsLen; i++) {
  56111. group = NULL;
  56112. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  56113. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  56114. EC_GROUP_free(group);
  56115. }
  56116. for (i = 0; i < knowEccEnumsLen; i++) {
  56117. group = NULL;
  56118. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  56119. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  56120. EC_GROUP_free(group);
  56121. }
  56122. #endif
  56123. return EXPECT_RESULT();
  56124. }
  56125. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  56126. {
  56127. EXPECT_DECLS;
  56128. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  56129. EC_GROUP *group = NULL;
  56130. BIO* bio = NULL;
  56131. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  56132. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  56133. EC_GROUP *ret = NULL;
  56134. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  56135. "BgUrgQQAIg==\n"
  56136. "-----END EC PARAMETERS-----";
  56137. #endif
  56138. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  56139. "MAA=\n"
  56140. "-----END EC PARAMETERS-----";
  56141. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  56142. "BgA=\n"
  56143. "-----END EC PARAMETERS-----";
  56144. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  56145. "BgE=\n"
  56146. "-----END EC PARAMETERS-----";
  56147. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  56148. "BgE*\n"
  56149. "-----END EC PARAMETERS-----";
  56150. /* Test that first parameter, bio, being NULL fails. */
  56151. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  56152. /* Test that reading named parameters works. */
  56153. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  56154. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  56155. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56156. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  56157. BIO_free(bio);
  56158. bio = NULL;
  56159. EC_GROUP_free(group);
  56160. group = NULL;
  56161. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  56162. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  56163. /* Test that reusing group works. */
  56164. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  56165. sizeof(ec_nc_p384)));
  56166. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  56167. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  56168. BIO_free(bio);
  56169. bio = NULL;
  56170. EC_GROUP_free(group);
  56171. group = NULL;
  56172. /* Test that returning through group works. */
  56173. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  56174. sizeof(ec_nc_p384)));
  56175. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  56176. ExpectIntEQ(group == ret, 1);
  56177. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  56178. BIO_free(bio);
  56179. bio = NULL;
  56180. EC_GROUP_free(group);
  56181. group = NULL;
  56182. #endif
  56183. /* Test 0x30, 0x00 (not and object id) fails. */
  56184. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  56185. sizeof(ec_nc_bad_1)));
  56186. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56187. BIO_free(bio);
  56188. bio = NULL;
  56189. /* Test 0x06, 0x00 (empty object id) fails. */
  56190. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  56191. sizeof(ec_nc_bad_2)));
  56192. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56193. BIO_free(bio);
  56194. bio = NULL;
  56195. /* Test 0x06, 0x01 (badly formed object id) fails. */
  56196. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  56197. sizeof(ec_nc_bad_3)));
  56198. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56199. BIO_free(bio);
  56200. bio = NULL;
  56201. /* Test invalid PEM encoding - invalid character. */
  56202. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  56203. sizeof(ec_nc_bad_4)));
  56204. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  56205. BIO_free(bio);
  56206. #endif
  56207. return EXPECT_RESULT();
  56208. }
  56209. static int test_wolfSSL_EC_POINT(void)
  56210. {
  56211. EXPECT_DECLS;
  56212. #if !defined(WOLFSSL_SP_MATH) && \
  56213. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  56214. #ifdef OPENSSL_EXTRA
  56215. BN_CTX* ctx = NULL;
  56216. EC_GROUP* group = NULL;
  56217. #ifndef HAVE_ECC_BRAINPOOL
  56218. EC_GROUP* group2 = NULL;
  56219. #endif
  56220. EC_POINT* Gxy = NULL;
  56221. EC_POINT* new_point = NULL;
  56222. EC_POINT* set_point = NULL;
  56223. EC_POINT* get_point = NULL;
  56224. EC_POINT* infinity = NULL;
  56225. BIGNUM* k = NULL;
  56226. BIGNUM* Gx = NULL;
  56227. BIGNUM* Gy = NULL;
  56228. BIGNUM* Gz = NULL;
  56229. BIGNUM* X = NULL;
  56230. BIGNUM* Y = NULL;
  56231. BIGNUM* set_point_bn = NULL;
  56232. char* hexStr = NULL;
  56233. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  56234. "17AF381913FF7A96314EA47055EA0FD0";
  56235. /* NISTP256R1 Gx/Gy */
  56236. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  56237. "77037D812DEB33A0F4A13945D898C296";
  56238. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  56239. "2BCE33576B315ECECBB6406837BF51F5";
  56240. const char* uncompG
  56241. = "046B17D1F2E12C4247F8BCE6E563A440F2"
  56242. "77037D812DEB33A0F4A13945D898C296"
  56243. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  56244. "2BCE33576B315ECECBB6406837BF51F5";
  56245. const char* compG
  56246. = "036B17D1F2E12C4247F8BCE6E563A440F2"
  56247. "77037D812DEB33A0F4A13945D898C296";
  56248. #ifndef HAVE_SELFTEST
  56249. EC_POINT *tmp = NULL;
  56250. size_t bin_len;
  56251. unsigned int blen = 0;
  56252. unsigned char* buf = NULL;
  56253. unsigned char bufInf[1] = { 0x00 };
  56254. const unsigned char binUncompG[] = {
  56255. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56256. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56257. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56258. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56259. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56260. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56261. };
  56262. const unsigned char binUncompGBad[] = {
  56263. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56264. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56265. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56266. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56267. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56268. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56269. };
  56270. #ifdef HAVE_COMP_KEY
  56271. const unsigned char binCompG[] = {
  56272. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56273. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56274. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56275. };
  56276. #endif
  56277. #endif
  56278. ExpectNotNull(ctx = BN_CTX_new());
  56279. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56280. #ifndef HAVE_ECC_BRAINPOOL
  56281. /* Used to make groups curve_idx == -1. */
  56282. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  56283. #endif
  56284. ExpectNull(EC_POINT_new(NULL));
  56285. ExpectNotNull(Gxy = EC_POINT_new(group));
  56286. ExpectNotNull(new_point = EC_POINT_new(group));
  56287. ExpectNotNull(set_point = EC_POINT_new(group));
  56288. ExpectNotNull(X = BN_new());
  56289. ExpectNotNull(Y = BN_new());
  56290. ExpectNotNull(set_point_bn = BN_new());
  56291. ExpectNotNull(infinity = EC_POINT_new(group));
  56292. /* load test values */
  56293. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  56294. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  56295. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  56296. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  56297. /* populate coordinates for input point */
  56298. if (Gxy != NULL) {
  56299. Gxy->X = Gx;
  56300. Gxy->Y = Gy;
  56301. Gxy->Z = Gz;
  56302. }
  56303. /* Test handling of NULL point. */
  56304. EC_POINT_clear_free(NULL);
  56305. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56306. NULL, NULL, ctx), 0);
  56307. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  56308. NULL, NULL, ctx), 0);
  56309. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  56310. NULL, NULL, ctx), 0);
  56311. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56312. X, NULL, ctx), 0);
  56313. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  56314. NULL, Y, ctx), 0);
  56315. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  56316. X, Y, ctx), 0);
  56317. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  56318. X, Y, ctx), 0);
  56319. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  56320. NULL, Y, ctx), 0);
  56321. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  56322. X, NULL, ctx), 0);
  56323. /* Getting point at infinity returns an error. */
  56324. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  56325. X, Y, ctx), 0);
  56326. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  56327. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  56328. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56329. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  56330. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  56331. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  56332. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  56333. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  56334. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  56335. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  56336. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  56337. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  56338. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  56339. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  56340. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  56341. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  56342. /* perform point multiplication */
  56343. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  56344. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56345. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56346. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56347. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  56348. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56349. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56350. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56351. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  56352. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56353. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56354. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56355. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  56356. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  56357. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  56358. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  56359. /* Set point to something. */
  56360. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  56361. #else
  56362. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  56363. ctx), 1);
  56364. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56365. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  56366. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  56367. #endif
  56368. /* check if point X coordinate is zero */
  56369. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  56370. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  56371. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  56372. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  56373. #endif
  56374. /* extract the coordinates from point */
  56375. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  56376. ctx), WOLFSSL_SUCCESS);
  56377. /* check if point X coordinate is zero */
  56378. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  56379. /* set the same X and Y points in another object */
  56380. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  56381. ctx), WOLFSSL_SUCCESS);
  56382. /* compare points as they should be the same */
  56383. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  56384. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  56385. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  56386. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  56387. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  56388. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  56389. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  56390. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  56391. /* Test copying */
  56392. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  56393. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  56394. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  56395. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  56396. /* Test inverting */
  56397. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  56398. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  56399. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  56400. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  56401. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  56402. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  56403. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56404. {
  56405. EC_POINT* orig_point = NULL;
  56406. ExpectNotNull(orig_point = EC_POINT_new(group));
  56407. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  56408. 1);
  56409. /* new_point should be set_point inverted so adding it will revert
  56410. * the point back to set_point */
  56411. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  56412. NULL), 1);
  56413. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  56414. EC_POINT_free(orig_point);
  56415. }
  56416. #endif
  56417. /* Test getting affine converts from projective. */
  56418. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  56419. /* Force non-affine coordinates */
  56420. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  56421. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  56422. if (new_point != NULL) {
  56423. new_point->inSet = 0;
  56424. }
  56425. /* extract the coordinates from point */
  56426. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  56427. ctx), WOLFSSL_SUCCESS);
  56428. /* check if point ordinates have changed. */
  56429. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  56430. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  56431. /* Test check for infinity */
  56432. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  56433. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  56434. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  56435. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  56436. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  56437. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  56438. #else
  56439. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  56440. #endif
  56441. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  56442. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  56443. /* check bn2hex */
  56444. hexStr = BN_bn2hex(k);
  56445. ExpectStrEQ(hexStr, kTest);
  56446. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56447. defined(XFPRINTF)
  56448. BN_print_fp(stderr, k);
  56449. fprintf(stderr, "\n");
  56450. #endif
  56451. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56452. hexStr = BN_bn2hex(Gx);
  56453. ExpectStrEQ(hexStr, kGx);
  56454. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56455. defined(XFPRINTF)
  56456. BN_print_fp(stderr, Gx);
  56457. fprintf(stderr, "\n");
  56458. #endif
  56459. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56460. hexStr = BN_bn2hex(Gy);
  56461. ExpectStrEQ(hexStr, kGy);
  56462. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  56463. defined(XFPRINTF)
  56464. BN_print_fp(stderr, Gy);
  56465. fprintf(stderr, "\n");
  56466. #endif
  56467. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56468. /* Test point to hex */
  56469. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56470. ctx));
  56471. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56472. ctx));
  56473. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56474. ctx));
  56475. #ifndef HAVE_ECC_BRAINPOOL
  56476. /* Group not supported in wolfCrypt. */
  56477. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56478. ctx));
  56479. #endif
  56480. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  56481. ExpectNotNull(hexStr);
  56482. ExpectStrEQ(hexStr, uncompG);
  56483. AssertNotNull(get_point = EC_POINT_hex2point(group, hexStr, NULL, ctx));
  56484. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  56485. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56486. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  56487. ExpectNotNull(hexStr);
  56488. ExpectStrEQ(hexStr, compG);
  56489. #ifdef HAVE_COMP_KEY
  56490. AssertNotNull(get_point = EC_POINT_hex2point
  56491. (group, hexStr, get_point, ctx));
  56492. AssertIntEQ(EC_POINT_cmp(group, Gxy, get_point, ctx), 0);
  56493. #endif
  56494. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  56495. EC_POINT_free(get_point);
  56496. #ifndef HAVE_SELFTEST
  56497. /* Test point to oct */
  56498. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56499. NULL, 0, ctx), 0);
  56500. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56501. NULL, 0, ctx), 0);
  56502. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  56503. NULL, 0, ctx), 0);
  56504. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56505. NULL, 0, ctx);
  56506. ExpectIntEQ(bin_len, sizeof(binUncompG));
  56507. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  56508. DYNAMIC_TYPE_ECC));
  56509. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  56510. buf, bin_len, ctx), bin_len);
  56511. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  56512. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56513. /* Infinity (x=0, y=0) encodes as '0x00'. */
  56514. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56515. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  56516. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56517. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  56518. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  56519. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  56520. ExpectIntEQ(bufInf[0], 0);
  56521. wolfSSL_EC_POINT_dump(NULL, NULL);
  56522. /* Test point i2d */
  56523. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  56524. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  56525. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  56526. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  56527. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  56528. ExpectIntEQ(blen, sizeof(binUncompG));
  56529. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  56530. blen -= 1;
  56531. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  56532. blen += 1;
  56533. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  56534. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  56535. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56536. #ifdef HAVE_COMP_KEY
  56537. /* Test point to oct compressed */
  56538. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  56539. 0, ctx);
  56540. ExpectIntEQ(bin_len, sizeof(binCompG));
  56541. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  56542. DYNAMIC_TYPE_ECC));
  56543. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  56544. bin_len, ctx), bin_len);
  56545. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  56546. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  56547. #endif
  56548. /* Test point BN */
  56549. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  56550. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56551. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  56552. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56553. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  56554. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  56555. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  56556. /* Test oct to point */
  56557. ExpectNotNull(tmp = EC_POINT_new(group));
  56558. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  56559. ctx), 0);
  56560. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  56561. ctx), 0);
  56562. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  56563. ctx), 0);
  56564. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  56565. sizeof(binUncompGBad), ctx), 0);
  56566. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  56567. ctx), 1);
  56568. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56569. EC_POINT_free(tmp);
  56570. tmp = NULL;
  56571. /* Test setting BN ordinates. */
  56572. ExpectNotNull(tmp = EC_POINT_new(group));
  56573. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  56574. NULL, ctx), 0);
  56575. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  56576. NULL, ctx), 0);
  56577. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  56578. NULL, ctx), 0);
  56579. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  56580. NULL, ctx), 0);
  56581. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  56582. Gy, ctx), 0);
  56583. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  56584. ctx), 0);
  56585. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  56586. ctx), 0);
  56587. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  56588. Gy, ctx), 0);
  56589. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  56590. NULL, ctx), 0);
  56591. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  56592. ctx), 1);
  56593. EC_POINT_free(tmp);
  56594. tmp = NULL;
  56595. /* Test point d2i */
  56596. ExpectNotNull(tmp = EC_POINT_new(group));
  56597. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  56598. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  56599. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  56600. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  56601. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  56602. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  56603. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  56604. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  56605. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  56606. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56607. EC_POINT_free(tmp);
  56608. tmp = NULL;
  56609. #ifdef HAVE_COMP_KEY
  56610. /* Test oct compressed to point */
  56611. ExpectNotNull(tmp = EC_POINT_new(group));
  56612. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  56613. 1);
  56614. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56615. EC_POINT_free(tmp);
  56616. tmp = NULL;
  56617. /* Test point d2i - compressed */
  56618. ExpectNotNull(tmp = EC_POINT_new(group));
  56619. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  56620. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  56621. EC_POINT_free(tmp);
  56622. tmp = NULL;
  56623. #endif
  56624. #endif
  56625. /* test BN_mod_add */
  56626. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  56627. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  56628. 1);
  56629. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  56630. /* cleanup */
  56631. BN_free(X);
  56632. BN_free(Y);
  56633. BN_free(k);
  56634. BN_free(set_point_bn);
  56635. EC_POINT_free(infinity);
  56636. EC_POINT_free(new_point);
  56637. EC_POINT_free(set_point);
  56638. EC_POINT_clear_free(Gxy);
  56639. #ifndef HAVE_ECC_BRAINPOOL
  56640. EC_GROUP_free(group2);
  56641. #endif
  56642. EC_GROUP_free(group);
  56643. BN_CTX_free(ctx);
  56644. #endif
  56645. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  56646. return EXPECT_RESULT();
  56647. }
  56648. static int test_wolfSSL_SPAKE(void)
  56649. {
  56650. EXPECT_DECLS;
  56651. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  56652. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  56653. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  56654. BIGNUM* x = NULL; /* kdc priv */
  56655. BIGNUM* y = NULL; /* client priv */
  56656. BIGNUM* w = NULL; /* shared value */
  56657. byte M_bytes[] = {
  56658. /* uncompressed */
  56659. 0x04,
  56660. /* x */
  56661. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  56662. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  56663. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  56664. /* y */
  56665. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  56666. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  56667. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  56668. };
  56669. EC_POINT* M = NULL; /* shared value */
  56670. byte N_bytes[] = {
  56671. /* uncompressed */
  56672. 0x04,
  56673. /* x */
  56674. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  56675. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  56676. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  56677. /* y */
  56678. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  56679. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  56680. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  56681. };
  56682. EC_POINT* N = NULL; /* shared value */
  56683. EC_POINT* T = NULL; /* kdc pub */
  56684. EC_POINT* tmp1 = NULL; /* kdc pub */
  56685. EC_POINT* tmp2 = NULL; /* kdc pub */
  56686. EC_POINT* S = NULL; /* client pub */
  56687. EC_POINT* client_secret = NULL;
  56688. EC_POINT* kdc_secret = NULL;
  56689. EC_GROUP* group = NULL;
  56690. BN_CTX* bn_ctx = NULL;
  56691. /* Values taken from a test run of Kerberos 5 */
  56692. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  56693. ExpectNotNull(bn_ctx = BN_CTX_new());
  56694. ExpectNotNull(M = EC_POINT_new(group));
  56695. ExpectNotNull(N = EC_POINT_new(group));
  56696. ExpectNotNull(T = EC_POINT_new(group));
  56697. ExpectNotNull(tmp1 = EC_POINT_new(group));
  56698. ExpectNotNull(tmp2 = EC_POINT_new(group));
  56699. ExpectNotNull(S = EC_POINT_new(group));
  56700. ExpectNotNull(client_secret = EC_POINT_new(group));
  56701. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  56702. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  56703. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  56704. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  56705. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  56706. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  56707. "506AB395478F0AAB647752CF117B36250"), 1);
  56708. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  56709. 1);
  56710. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  56711. 1);
  56712. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  56713. /* kdc */
  56714. /* T=x*P+w*M */
  56715. /* All in one function call */
  56716. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  56717. /* Spread into separate calls */
  56718. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  56719. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  56720. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  56721. 1);
  56722. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  56723. /* client */
  56724. /* S=y*P+w*N */
  56725. /* All in one function call */
  56726. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  56727. /* Spread into separate calls */
  56728. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  56729. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  56730. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  56731. 1);
  56732. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  56733. /* K=y*(T-w*M) */
  56734. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  56735. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  56736. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  56737. 1);
  56738. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  56739. bn_ctx), 1);
  56740. /* kdc */
  56741. /* K=x*(S-w*N) */
  56742. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  56743. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  56744. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  56745. 1);
  56746. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  56747. 1);
  56748. /* kdc_secret == client_secret */
  56749. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  56750. BN_free(x);
  56751. BN_free(y);
  56752. BN_free(w);
  56753. EC_POINT_free(M);
  56754. EC_POINT_free(N);
  56755. EC_POINT_free(T);
  56756. EC_POINT_free(tmp1);
  56757. EC_POINT_free(tmp2);
  56758. EC_POINT_free(S);
  56759. EC_POINT_free(client_secret);
  56760. EC_POINT_free(kdc_secret);
  56761. EC_GROUP_free(group);
  56762. BN_CTX_free(bn_ctx);
  56763. #endif
  56764. return EXPECT_RESULT();
  56765. }
  56766. static int test_wolfSSL_EC_KEY_generate(void)
  56767. {
  56768. EXPECT_DECLS;
  56769. #ifdef OPENSSL_EXTRA
  56770. WOLFSSL_EC_KEY* key = NULL;
  56771. #ifndef HAVE_ECC_BRAINPOOL
  56772. WOLFSSL_EC_GROUP* group = NULL;
  56773. #endif
  56774. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56775. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  56776. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  56777. wolfSSL_EC_KEY_free(key);
  56778. key = NULL;
  56779. #ifndef HAVE_ECC_BRAINPOOL
  56780. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  56781. NID_brainpoolP256r1));
  56782. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  56783. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  56784. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  56785. wolfSSL_EC_KEY_free(key);
  56786. wolfSSL_EC_GROUP_free(group);
  56787. #endif
  56788. #endif
  56789. return EXPECT_RESULT();
  56790. }
  56791. static int test_EC_i2d(void)
  56792. {
  56793. EXPECT_DECLS;
  56794. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  56795. EC_KEY *key = NULL;
  56796. EC_KEY *copy = NULL;
  56797. int len = 0;
  56798. unsigned char *buf = NULL;
  56799. unsigned char *p = NULL;
  56800. const unsigned char *tmp = NULL;
  56801. const unsigned char octBad[] = {
  56802. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  56803. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  56804. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  56805. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  56806. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  56807. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  56808. };
  56809. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  56810. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  56811. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  56812. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56813. DYNAMIC_TYPE_TMP_BUFFER));
  56814. p = buf;
  56815. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  56816. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  56817. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  56818. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  56819. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  56820. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  56821. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  56822. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  56823. tmp = buf;
  56824. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  56825. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  56826. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  56827. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  56828. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  56829. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  56830. tmp = buf;
  56831. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  56832. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  56833. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  56834. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  56835. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  56836. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  56837. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  56838. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  56839. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  56840. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  56841. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  56842. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  56843. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  56844. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56845. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  56846. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56847. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  56848. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56849. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  56850. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56851. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  56852. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  56853. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  56854. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  56855. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  56856. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  56857. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  56858. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56859. buf = NULL;
  56860. buf = NULL;
  56861. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  56862. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56863. DYNAMIC_TYPE_TMP_BUFFER));
  56864. p = buf;
  56865. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  56866. p = NULL;
  56867. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  56868. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56869. p = NULL;
  56870. /* Bad point is also an invalid private key. */
  56871. tmp = octBad;
  56872. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  56873. tmp = buf;
  56874. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  56875. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  56876. buf = NULL;
  56877. buf = NULL;
  56878. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  56879. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  56880. DYNAMIC_TYPE_TMP_BUFFER));
  56881. p = buf;
  56882. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  56883. p = NULL;
  56884. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  56885. tmp = buf;
  56886. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  56887. tmp = octBad;
  56888. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  56889. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  56890. ExpectIntEQ(EC_KEY_check_key(key), 1);
  56891. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  56892. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  56893. EC_KEY_free(key);
  56894. EC_KEY_free(copy);
  56895. #endif
  56896. return EXPECT_RESULT();
  56897. }
  56898. static int test_wolfSSL_EC_curve(void)
  56899. {
  56900. EXPECT_DECLS;
  56901. #if defined(OPENSSL_EXTRA)
  56902. int nid = NID_secp160k1;
  56903. const char* nid_name = NULL;
  56904. ExpectNull(EC_curve_nid2nist(NID_sha256));
  56905. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  56906. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  56907. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  56908. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  56909. #endif
  56910. return EXPECT_RESULT();
  56911. }
  56912. static int test_wolfSSL_EC_KEY_dup(void)
  56913. {
  56914. EXPECT_DECLS;
  56915. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  56916. WOLFSSL_EC_KEY* ecKey = NULL;
  56917. WOLFSSL_EC_KEY* dupKey = NULL;
  56918. ecc_key* srcKey = NULL;
  56919. ecc_key* destKey = NULL;
  56920. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56921. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  56922. /* Valid cases */
  56923. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56924. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  56925. /* Compare pubkey */
  56926. if (ecKey != NULL) {
  56927. srcKey = (ecc_key*)ecKey->internal;
  56928. }
  56929. if (dupKey != NULL) {
  56930. destKey = (ecc_key*)dupKey->internal;
  56931. }
  56932. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  56933. /* compare EC_GROUP */
  56934. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  56935. /* compare EC_POINT */
  56936. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  56937. dupKey->pub_key, NULL), MP_EQ);
  56938. /* compare BIGNUM */
  56939. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  56940. wolfSSL_EC_KEY_free(dupKey);
  56941. dupKey = NULL;
  56942. /* Invalid cases */
  56943. /* NULL key */
  56944. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  56945. /* NULL ecc_key */
  56946. if (ecKey != NULL) {
  56947. wc_ecc_free((ecc_key*)ecKey->internal);
  56948. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  56949. ecKey->internal = NULL; /* Set ecc_key to NULL */
  56950. }
  56951. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56952. wolfSSL_EC_KEY_free(ecKey);
  56953. ecKey = NULL;
  56954. wolfSSL_EC_KEY_free(dupKey);
  56955. dupKey = NULL;
  56956. /* NULL Group */
  56957. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56958. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  56959. if (ecKey != NULL) {
  56960. wolfSSL_EC_GROUP_free(ecKey->group);
  56961. ecKey->group = NULL; /* Set group to NULL */
  56962. }
  56963. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56964. wolfSSL_EC_KEY_free(ecKey);
  56965. ecKey = NULL;
  56966. wolfSSL_EC_KEY_free(dupKey);
  56967. dupKey = NULL;
  56968. /* NULL public key */
  56969. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56970. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  56971. if (ecKey != NULL) {
  56972. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  56973. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  56974. }
  56975. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56976. if (ecKey != NULL) {
  56977. wolfSSL_EC_POINT_free(ecKey->pub_key);
  56978. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  56979. }
  56980. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56981. wolfSSL_EC_KEY_free(ecKey);
  56982. ecKey = NULL;
  56983. wolfSSL_EC_KEY_free(dupKey);
  56984. dupKey = NULL;
  56985. /* NULL private key */
  56986. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56987. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  56988. if (ecKey != NULL) {
  56989. wolfSSL_BN_free(ecKey->priv_key);
  56990. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  56991. }
  56992. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  56993. wolfSSL_EC_KEY_free(ecKey);
  56994. ecKey = NULL;
  56995. wolfSSL_EC_KEY_free(dupKey);
  56996. dupKey = NULL;
  56997. /* Test EC_KEY_up_ref */
  56998. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  56999. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  57000. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  57001. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  57002. /* reference count doesn't follow duplicate */
  57003. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  57004. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  57005. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  57006. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  57007. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  57008. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  57009. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  57010. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  57011. #endif
  57012. return EXPECT_RESULT();
  57013. }
  57014. static int test_wolfSSL_EC_KEY_set_group(void)
  57015. {
  57016. EXPECT_DECLS;
  57017. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  57018. defined(OPENSSL_EXTRA)
  57019. EC_KEY *key = NULL;
  57020. EC_GROUP *group = NULL;
  57021. const EC_GROUP *group2 = NULL;
  57022. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  57023. ExpectNotNull(key = EC_KEY_new());
  57024. ExpectNull(EC_KEY_get0_group(NULL));
  57025. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  57026. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  57027. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  57028. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  57029. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  57030. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  57031. EC_GROUP_free(group);
  57032. EC_KEY_free(key);
  57033. #endif
  57034. return EXPECT_RESULT();
  57035. }
  57036. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  57037. {
  57038. EXPECT_DECLS;
  57039. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57040. BIO* bio = NULL;
  57041. EC_KEY* key = NULL;
  57042. /* Error condition: NULL key. */
  57043. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  57044. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  57045. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  57046. /* Conversion form defaults to uncompressed. */
  57047. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57048. #ifdef HAVE_COMP_KEY
  57049. /* Explicitly set to compressed. */
  57050. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  57051. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  57052. #else
  57053. /* Will still work just won't change anything. */
  57054. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  57055. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57056. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  57057. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  57058. #endif
  57059. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  57060. BIO_free(bio);
  57061. EC_KEY_free(key);
  57062. #endif
  57063. return EXPECT_RESULT();
  57064. }
  57065. static int test_wolfSSL_EC_KEY_private_key(void)
  57066. {
  57067. EXPECT_DECLS;
  57068. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57069. WOLFSSL_EC_KEY* key = NULL;
  57070. WOLFSSL_BIGNUM* priv = NULL;
  57071. WOLFSSL_BIGNUM* priv2 = NULL;
  57072. WOLFSSL_BIGNUM* bn;
  57073. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57074. ExpectNotNull(priv = wolfSSL_BN_new());
  57075. ExpectNotNull(priv2 = wolfSSL_BN_new());
  57076. ExpectIntNE(BN_set_word(priv, 2), 0);
  57077. ExpectIntNE(BN_set_word(priv2, 2), 0);
  57078. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  57079. /* No private key set. */
  57080. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  57081. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  57082. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  57083. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  57084. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  57085. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  57086. ExpectPtrNE(bn, priv);
  57087. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  57088. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  57089. ExpectPtrNE(bn, priv2);
  57090. wolfSSL_BN_free(priv2);
  57091. wolfSSL_BN_free(priv);
  57092. wolfSSL_EC_KEY_free(key);
  57093. #endif
  57094. return EXPECT_RESULT();
  57095. }
  57096. static int test_wolfSSL_EC_KEY_public_key(void)
  57097. {
  57098. EXPECT_DECLS;
  57099. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  57100. WOLFSSL_EC_KEY* key = NULL;
  57101. WOLFSSL_EC_POINT* pub = NULL;
  57102. WOLFSSL_EC_POINT* point = NULL;
  57103. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57104. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  57105. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  57106. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  57107. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  57108. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  57109. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  57110. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  57111. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  57112. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  57113. ExpectPtrEq(point, pub);
  57114. wolfSSL_EC_KEY_free(key);
  57115. #endif
  57116. return EXPECT_RESULT();
  57117. }
  57118. static int test_wolfSSL_EC_KEY_print_fp(void)
  57119. {
  57120. EXPECT_DECLS;
  57121. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  57122. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  57123. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  57124. !defined(NO_STDIO_FILESYSTEM)
  57125. EC_KEY* key = NULL;
  57126. /* Bad file pointer. */
  57127. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  57128. /* NULL key. */
  57129. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  57130. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  57131. /* Negative indent. */
  57132. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  57133. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57134. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  57135. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57136. wolfSSL_EC_KEY_free(key);
  57137. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  57138. NID_X9_62_prime256v1)));
  57139. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  57140. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  57141. wolfSSL_EC_KEY_free(key);
  57142. #endif
  57143. return EXPECT_RESULT();
  57144. }
  57145. static int test_wolfSSL_EC_get_builtin_curves(void)
  57146. {
  57147. EXPECT_DECLS;
  57148. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  57149. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  57150. EC_builtin_curve* curves = NULL;
  57151. size_t crv_len = 0;
  57152. size_t i = 0;
  57153. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  57154. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  57155. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  57156. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  57157. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  57158. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  57159. if (curves[i].comment != NULL) {
  57160. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  57161. }
  57162. }
  57163. if (crv_len > 1) {
  57164. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  57165. }
  57166. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  57167. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  57168. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  57169. return EXPECT_RESULT();
  57170. }
  57171. static int test_wolfSSL_ECDSA_SIG(void)
  57172. {
  57173. EXPECT_DECLS;
  57174. #ifdef OPENSSL_EXTRA
  57175. WOLFSSL_ECDSA_SIG* sig = NULL;
  57176. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  57177. WOLFSSL_BIGNUM* r = NULL;
  57178. WOLFSSL_BIGNUM* s = NULL;
  57179. const WOLFSSL_BIGNUM* r2 = NULL;
  57180. const WOLFSSL_BIGNUM* s2 = NULL;
  57181. const unsigned char* cp = NULL;
  57182. unsigned char* p = NULL;
  57183. unsigned char outSig[8];
  57184. unsigned char sigData[8] =
  57185. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  57186. unsigned char sigDataBad[8] =
  57187. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  57188. wolfSSL_ECDSA_SIG_free(NULL);
  57189. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  57190. ExpectNotNull(r = wolfSSL_BN_new());
  57191. ExpectNotNull(s = wolfSSL_BN_new());
  57192. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  57193. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  57194. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  57195. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  57196. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  57197. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  57198. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  57199. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  57200. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  57201. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  57202. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  57203. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  57204. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  57205. r2 = NULL;
  57206. s2 = NULL;
  57207. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  57208. ExpectNull(r2);
  57209. ExpectNull(s2);
  57210. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  57211. if (EXPECT_FAIL()) {
  57212. wolfSSL_BN_free(r);
  57213. wolfSSL_BN_free(s);
  57214. }
  57215. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  57216. ExpectPtrEq(r2, r);
  57217. ExpectPtrEq(s2, s);
  57218. r2 = NULL;
  57219. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  57220. ExpectPtrEq(r2, r);
  57221. s2 = NULL;
  57222. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  57223. ExpectPtrEq(s2, s);
  57224. /* r and s are freed when sig is freed. */
  57225. wolfSSL_ECDSA_SIG_free(sig);
  57226. sig = NULL;
  57227. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  57228. cp = sigDataBad;
  57229. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  57230. cp = sigData;
  57231. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  57232. ExpectIntEQ((cp == sigData + 8), 1);
  57233. cp = sigData;
  57234. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  57235. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  57236. ExpectIntEQ((sig == sig2), 1);
  57237. cp = outSig;
  57238. p = outSig;
  57239. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  57240. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  57241. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  57242. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  57243. ExpectIntEQ((p == outSig + 8), 1);
  57244. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  57245. p = NULL;
  57246. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), 8);
  57247. #ifndef WOLFSSL_I2D_ECDSA_SIG_ALLOC
  57248. ExpectNull(p);
  57249. #else
  57250. ExpectNotNull(p);
  57251. ExpectIntEQ(XMEMCMP(p, outSig, 8), 0);
  57252. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  57253. #endif
  57254. wolfSSL_ECDSA_SIG_free(sig);
  57255. #endif
  57256. return EXPECT_RESULT();
  57257. }
  57258. static int test_ECDSA_size_sign(void)
  57259. {
  57260. EXPECT_DECLS;
  57261. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  57262. EC_KEY* key = NULL;
  57263. ECDSA_SIG* ecdsaSig = NULL;
  57264. int id;
  57265. byte hash[WC_MAX_DIGEST_SIZE];
  57266. byte hash2[WC_MAX_DIGEST_SIZE];
  57267. byte sig[ECC_MAX_SIG_SIZE];
  57268. unsigned int sigSz = sizeof(sig);
  57269. XMEMSET(hash, 123, sizeof(hash));
  57270. XMEMSET(hash2, 234, sizeof(hash2));
  57271. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  57272. ExpectIntEQ(id, ECC_SECP256R1);
  57273. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57274. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  57275. ExpectIntGE(ECDSA_size(NULL), 0);
  57276. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  57277. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  57278. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  57279. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  57280. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  57281. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  57282. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  57283. ExpectIntGE(ECDSA_size(key), sigSz);
  57284. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  57285. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  57286. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  57287. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  57288. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  57289. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  57290. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  57291. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  57292. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  57293. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  57294. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  57295. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  57296. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  57297. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  57298. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  57299. ECDSA_SIG_free(ecdsaSig);
  57300. EC_KEY_free(key);
  57301. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  57302. return EXPECT_RESULT();
  57303. }
  57304. static int test_ECDH_compute_key(void)
  57305. {
  57306. EXPECT_DECLS;
  57307. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  57308. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  57309. EC_KEY* key1 = NULL;
  57310. EC_KEY* key2 = NULL;
  57311. EC_POINT* pub1 = NULL;
  57312. EC_POINT* pub2 = NULL;
  57313. byte secret1[32];
  57314. byte secret2[32];
  57315. int i;
  57316. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57317. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  57318. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  57319. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57320. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  57321. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  57322. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  57323. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  57324. 0);
  57325. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  57326. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  57327. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  57328. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  57329. 0);
  57330. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  57331. 0);
  57332. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  57333. NULL), 0);
  57334. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  57335. sizeof(secret1));
  57336. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  57337. sizeof(secret2));
  57338. for (i = 0; i < (int)sizeof(secret1); i++) {
  57339. ExpectIntEQ(secret1[i], secret2[i]);
  57340. }
  57341. EC_KEY_free(key2);
  57342. EC_KEY_free(key1);
  57343. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  57344. * !WOLF_CRYPTO_CB_ONLY_ECC */
  57345. return EXPECT_RESULT();
  57346. }
  57347. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  57348. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  57349. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  57350. !defined(NO_ASN_TIME)
  57351. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  57352. int expectedDerSz)
  57353. {
  57354. EXPECT_DECLS;
  57355. X509* x509 = NULL;
  57356. BIGNUM* serial_number = NULL;
  57357. X509_NAME* name = NULL;
  57358. time_t epoch_off = 0;
  57359. ASN1_INTEGER* asn1_serial_number;
  57360. long not_before, not_after;
  57361. int derSz;
  57362. ExpectNotNull(x509 = X509_new());
  57363. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  57364. ExpectNotNull(serial_number = BN_new());
  57365. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  57366. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  57367. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  57368. /* version 3 */
  57369. ExpectIntNE(X509_set_version(x509, 2L), 0);
  57370. ExpectNotNull(name = X509_NAME_new());
  57371. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  57372. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  57373. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  57374. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  57375. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  57376. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  57377. not_before = (long)wc_Time(NULL);
  57378. not_after = not_before + (365 * 24 * 60 * 60);
  57379. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  57380. &epoch_off));
  57381. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  57382. &epoch_off));
  57383. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  57384. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  57385. ExpectIntGE(derSz, expectedDerSz);
  57386. BN_free(serial_number);
  57387. X509_NAME_free(name);
  57388. X509_free(x509);
  57389. return EXPECT_RESULT();
  57390. }
  57391. #endif
  57392. static int test_openssl_generate_key_and_cert(void)
  57393. {
  57394. EXPECT_DECLS;
  57395. #if defined(OPENSSL_EXTRA)
  57396. int expectedDerSz;
  57397. EVP_PKEY* pkey = NULL;
  57398. #ifdef HAVE_ECC
  57399. EC_KEY* ec_key = NULL;
  57400. #endif
  57401. #if !defined(NO_RSA)
  57402. int key_length = 2048;
  57403. BIGNUM* exponent = NULL;
  57404. RSA* rsa = NULL;
  57405. ExpectNotNull(pkey = EVP_PKEY_new());
  57406. ExpectNotNull(exponent = BN_new());
  57407. ExpectNotNull(rsa = RSA_new());
  57408. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  57409. #ifndef WOLFSSL_KEY_GEN
  57410. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  57411. #if defined(USE_CERT_BUFFERS_1024)
  57412. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  57413. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  57414. key_length = 1024;
  57415. #elif defined(USE_CERT_BUFFERS_2048)
  57416. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  57417. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  57418. #else
  57419. RSA_free(rsa);
  57420. rsa = NULL;
  57421. #endif
  57422. #else
  57423. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  57424. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  57425. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  57426. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  57427. #endif
  57428. if (rsa) {
  57429. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  57430. if (EXPECT_FAIL()) {
  57431. RSA_free(rsa);
  57432. }
  57433. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  57434. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  57435. expectedDerSz = 743;
  57436. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  57437. expectedDerSz), TEST_SUCCESS);
  57438. #endif
  57439. }
  57440. EVP_PKEY_free(pkey);
  57441. pkey = NULL;
  57442. BN_free(exponent);
  57443. #endif /* !NO_RSA */
  57444. #ifdef HAVE_ECC
  57445. ExpectNotNull(pkey = EVP_PKEY_new());
  57446. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  57447. #ifndef NO_WOLFSSL_STUB
  57448. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  57449. #endif
  57450. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  57451. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  57452. if (EXPECT_FAIL()) {
  57453. EC_KEY_free(ec_key);
  57454. }
  57455. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  57456. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  57457. expectedDerSz = 344;
  57458. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  57459. TEST_SUCCESS);
  57460. #endif
  57461. EVP_PKEY_free(pkey);
  57462. #endif /* HAVE_ECC */
  57463. (void)pkey;
  57464. (void)expectedDerSz;
  57465. #endif /* OPENSSL_EXTRA */
  57466. return EXPECT_RESULT();
  57467. }
  57468. static int test_stubs_are_stubs(void)
  57469. {
  57470. EXPECT_DECLS;
  57471. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  57472. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  57473. WOLFSSL_CTX* ctx = NULL;
  57474. WOLFSSL_CTX* ctxN = NULL;
  57475. #ifndef NO_WOLFSSL_CLIENT
  57476. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57477. #elif !defined(NO_WOLFSSL_SERVER)
  57478. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57479. #endif
  57480. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  57481. ExpectIntEQ((int) x(z), 0)
  57482. /* test logic, all stubs return same result regardless of ctx being NULL
  57483. * as there are no sanity checks, it's just a stub! If at some
  57484. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  57485. * if invalid inputs are supplied. Test calling both
  57486. * with and without valid inputs, if a stub functionality remains unchanged.
  57487. */
  57488. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  57489. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  57490. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  57491. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  57492. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  57493. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  57494. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  57495. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  57496. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  57497. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  57498. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  57499. /* when implemented this should take WOLFSSL object insted, right now
  57500. * always returns 0 */
  57501. ExpectIntEQ(SSL_get_current_expansion(NULL), 0);
  57502. wolfSSL_CTX_free(ctx);
  57503. ctx = NULL;
  57504. ExpectStrEQ(SSL_COMP_get_name(NULL), "not supported");
  57505. ExpectIntEQ(SSL_get_current_expansion(), 0);
  57506. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  57507. * !NO_WOLFSSL_SERVER) */
  57508. return EXPECT_RESULT();
  57509. }
  57510. static int test_CONF_modules_xxx(void)
  57511. {
  57512. int res = TEST_SKIPPED;
  57513. #if defined(OPENSSL_EXTRA)
  57514. CONF_modules_free();
  57515. CONF_modules_unload(0);
  57516. CONF_modules_unload(1);
  57517. CONF_modules_unload(-1);
  57518. res = TEST_SUCCESS;
  57519. #endif /* OPENSSL_EXTRA */
  57520. return res;
  57521. }
  57522. static int test_CRYPTO_set_dynlock_xxx(void)
  57523. {
  57524. int res = TEST_SKIPPED;
  57525. #if defined(OPENSSL_EXTRA)
  57526. CRYPTO_set_dynlock_create_callback(
  57527. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  57528. CRYPTO_set_dynlock_create_callback(
  57529. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  57530. CRYPTO_set_dynlock_destroy_callback(
  57531. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  57532. CRYPTO_set_dynlock_destroy_callback(
  57533. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  57534. CRYPTO_set_dynlock_lock_callback(
  57535. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  57536. CRYPTO_set_dynlock_lock_callback(
  57537. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  57538. res = TEST_SUCCESS;
  57539. #endif /* OPENSSL_EXTRA */
  57540. return res;
  57541. }
  57542. static int test_CRYPTO_THREADID_xxx(void)
  57543. {
  57544. EXPECT_DECLS;
  57545. #if defined(OPENSSL_EXTRA)
  57546. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  57547. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  57548. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  57549. #endif /* OPENSSL_EXTRA */
  57550. return EXPECT_RESULT();
  57551. }
  57552. static int test_ENGINE_cleanup(void)
  57553. {
  57554. int res = TEST_SKIPPED;
  57555. #if defined(OPENSSL_EXTRA)
  57556. ENGINE_cleanup();
  57557. res = TEST_SUCCESS;
  57558. #endif /* OPENSSL_EXTRA */
  57559. return res;
  57560. }
  57561. static int test_wolfSSL_CTX_LoadCRL(void)
  57562. {
  57563. EXPECT_DECLS;
  57564. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  57565. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  57566. WOLFSSL_CTX* ctx = NULL;
  57567. WOLFSSL* ssl = NULL;
  57568. const char* badPath = "dummypath";
  57569. const char* validPath = "./certs/crl";
  57570. const char* validFilePath = "./certs/crl/cliCrl.pem";
  57571. const char* issuerCert = "./certs/client-cert.pem";
  57572. int derType = WOLFSSL_FILETYPE_ASN1;
  57573. int pemType = WOLFSSL_FILETYPE_PEM;
  57574. #ifdef HAVE_CRL_MONITOR
  57575. int monitor = WOLFSSL_CRL_MONITOR;
  57576. #else
  57577. int monitor = 0;
  57578. #endif
  57579. WOLFSSL_CERT_MANAGER* cm = NULL;
  57580. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57581. BAD_FUNC_ARG)
  57582. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57583. NOT_COMPILED_IN)
  57584. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  57585. WOLFSSL_SUCCESS)
  57586. #ifndef NO_WOLFSSL_CLIENT
  57587. #define NEW_CTX(ctx) ExpectNotNull( \
  57588. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  57589. #elif !defined(NO_WOLFSSL_SERVER)
  57590. #define NEW_CTX(ctx) ExpectNotNull( \
  57591. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  57592. #else
  57593. #define NEW_CTX(ctx) return
  57594. #endif
  57595. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  57596. NEW_CTX(ctx);
  57597. #ifndef HAVE_CRL_MONITOR
  57598. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  57599. wolfSSL_CTX_free(ctx);
  57600. NEW_CTX(ctx);
  57601. #endif
  57602. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  57603. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  57604. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  57605. wolfSSL_CTX_free(ctx);
  57606. ctx = NULL;
  57607. NEW_CTX(ctx);
  57608. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  57609. WOLFSSL_SUCCESS);
  57610. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  57611. wolfSSL_CTX_free(ctx);
  57612. ctx = NULL;
  57613. NEW_CTX(ctx);
  57614. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  57615. WOLFSSL_SUCCESS);
  57616. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57617. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  57618. wolfSSL_free(ssl);
  57619. ssl = NULL;
  57620. wolfSSL_CTX_free(ctx);
  57621. ctx = NULL;
  57622. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  57623. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  57624. WOLFSSL_SUCCESS);
  57625. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  57626. WOLFSSL_SUCCESS);
  57627. wolfSSL_CertManagerFree(cm);
  57628. #endif
  57629. return EXPECT_RESULT();
  57630. }
  57631. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  57632. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  57633. {
  57634. EXPECT_DECLS;
  57635. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  57636. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  57637. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57638. return EXPECT_RESULT();
  57639. }
  57640. #endif
  57641. static int test_multiple_crls_same_issuer(void)
  57642. {
  57643. EXPECT_DECLS;
  57644. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  57645. test_ssl_cbf client_cbs, server_cbs;
  57646. struct {
  57647. const char* server_cert;
  57648. const char* server_key;
  57649. } test_params[] = {
  57650. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  57651. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  57652. };
  57653. size_t i;
  57654. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  57655. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  57656. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  57657. server_cbs.certPemFile = test_params[i].server_cert;
  57658. server_cbs.keyPemFile = test_params[i].server_key;
  57659. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  57660. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  57661. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  57662. &server_cbs, NULL), TEST_FAIL);
  57663. }
  57664. #endif
  57665. return EXPECT_RESULT();
  57666. }
  57667. static int test_SetTmpEC_DHE_Sz(void)
  57668. {
  57669. EXPECT_DECLS;
  57670. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  57671. WOLFSSL_CTX *ctx = NULL;
  57672. WOLFSSL *ssl = NULL;
  57673. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57674. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  57675. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57676. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  57677. wolfSSL_free(ssl);
  57678. wolfSSL_CTX_free(ctx);
  57679. #endif
  57680. return EXPECT_RESULT();
  57681. }
  57682. static int test_wolfSSL_CTX_get0_privatekey(void)
  57683. {
  57684. EXPECT_DECLS;
  57685. #ifdef OPENSSL_ALL
  57686. WOLFSSL_CTX* ctx = NULL;
  57687. (void)ctx;
  57688. #ifndef NO_RSA
  57689. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57690. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57691. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57692. WOLFSSL_FILETYPE_PEM));
  57693. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57694. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57695. WOLFSSL_FILETYPE_PEM));
  57696. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  57697. wolfSSL_CTX_free(ctx);
  57698. ctx = NULL;
  57699. #endif
  57700. #ifdef HAVE_ECC
  57701. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57702. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57703. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  57704. WOLFSSL_FILETYPE_PEM));
  57705. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  57706. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  57707. WOLFSSL_FILETYPE_PEM));
  57708. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  57709. wolfSSL_CTX_free(ctx);
  57710. #endif
  57711. #endif
  57712. return EXPECT_RESULT();
  57713. }
  57714. static int test_wolfSSL_dtls_set_mtu(void)
  57715. {
  57716. EXPECT_DECLS;
  57717. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  57718. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  57719. !defined(WOLFSSL_NO_TLS12)
  57720. WOLFSSL_CTX* ctx = NULL;
  57721. WOLFSSL* ssl = NULL;
  57722. const char* testCertFile;
  57723. const char* testKeyFile;
  57724. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  57725. #ifndef NO_RSA
  57726. testCertFile = svrCertFile;
  57727. testKeyFile = svrKeyFile;
  57728. #elif defined(HAVE_ECC)
  57729. testCertFile = eccCertFile;
  57730. testKeyFile = eccKeyFile;
  57731. #endif
  57732. if (testCertFile != NULL && testKeyFile != NULL) {
  57733. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  57734. WOLFSSL_FILETYPE_PEM));
  57735. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57736. WOLFSSL_FILETYPE_PEM));
  57737. }
  57738. ExpectNotNull(ssl = wolfSSL_new(ctx));
  57739. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  57740. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  57741. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  57742. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  57743. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  57744. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  57745. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  57746. wolfSSL_free(ssl);
  57747. wolfSSL_CTX_free(ctx);
  57748. #endif
  57749. return EXPECT_RESULT();
  57750. }
  57751. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  57752. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57753. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  57754. enum HandShakeType hsType, word16 length)
  57755. {
  57756. size_t idx = 0;
  57757. byte* l;
  57758. /* record layer */
  57759. /* handshake type */
  57760. out[idx++] = handshake;
  57761. /* protocol version */
  57762. out[idx++] = 0xfe;
  57763. out[idx++] = 0xfd; /* DTLS 1.2 */
  57764. /* epoch 0 */
  57765. XMEMSET(out + idx, 0, 2);
  57766. idx += 2;
  57767. /* sequence number */
  57768. XMEMSET(out + idx, 0, 6);
  57769. c32toa(seq, out + idx + 2);
  57770. idx += 6;
  57771. /* length in BE */
  57772. if (length)
  57773. c16toa(length, out + idx);
  57774. else
  57775. c16toa(outSz - idx - 2, out + idx);
  57776. idx += 2;
  57777. /* handshake layer */
  57778. /* handshake type */
  57779. out[idx++] = (byte)hsType;
  57780. /* length */
  57781. l = out + idx;
  57782. idx += 3;
  57783. /* message seq */
  57784. c16toa(0, out + idx);
  57785. idx += 2;
  57786. /* frag offset */
  57787. c32to24(0, out + idx);
  57788. idx += 3;
  57789. /* frag length */
  57790. c32to24((word32)outSz - (word32)idx - 3, l);
  57791. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  57792. idx += 3;
  57793. XMEMSET(out + idx, 0, outSz - idx);
  57794. }
  57795. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  57796. {
  57797. byte msg[] = "This is a msg for the client";
  57798. byte reply[40];
  57799. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  57800. reply[sizeof(reply) - 1] = '\0';
  57801. fprintf(stderr, "Client message: %s\n", reply);
  57802. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  57803. }
  57804. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  57805. {
  57806. byte ch[50];
  57807. int fd = wolfSSL_get_fd(ssl);
  57808. byte msg[] = "This is a msg for the server";
  57809. byte reply[40];
  57810. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  57811. /* Server should ignore this datagram */
  57812. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  57813. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  57814. /* Server should ignore this datagram */
  57815. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  57816. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  57817. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  57818. reply[sizeof(reply) - 1] = '\0';
  57819. fprintf(stderr, "Server response: %s\n", reply);
  57820. }
  57821. static int test_wolfSSL_dtls_plaintext(void)
  57822. {
  57823. callback_functions func_cb_client;
  57824. callback_functions func_cb_server;
  57825. size_t i;
  57826. struct test_params {
  57827. method_provider client_meth;
  57828. method_provider server_meth;
  57829. ssl_callback on_result_server;
  57830. ssl_callback on_result_client;
  57831. } params[] = {
  57832. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  57833. test_wolfSSL_dtls_plaintext_server,
  57834. test_wolfSSL_dtls_plaintext_client},
  57835. };
  57836. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57837. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  57838. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  57839. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  57840. func_cb_server.method = params[i].server_meth;
  57841. func_cb_client.method = params[i].client_meth;
  57842. func_cb_client.on_result = params[i].on_result_client;
  57843. func_cb_server.on_result = params[i].on_result_server;
  57844. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57845. if (!func_cb_client.return_code)
  57846. return TEST_FAIL;
  57847. if (!func_cb_server.return_code)
  57848. return TEST_FAIL;
  57849. }
  57850. return TEST_RES_CHECK(1);
  57851. }
  57852. #else
  57853. static int test_wolfSSL_dtls_plaintext(void) {
  57854. return TEST_SKIPPED;
  57855. }
  57856. #endif
  57857. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  57858. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57859. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  57860. {
  57861. byte b[1100]; /* buffer for the messages to send */
  57862. size_t idx = 0;
  57863. size_t seq_offset = 0;
  57864. size_t msg_offset = 0;
  57865. int i;
  57866. int fd = wolfSSL_get_fd(ssl);
  57867. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  57868. word32 seq_number = 100; /* start high so server definitely reads this */
  57869. word16 msg_number = 50; /* start high so server has to buffer this */
  57870. AssertIntEQ(ret, 1);
  57871. /* Now let's start spamming the peer with fragments it needs to store */
  57872. XMEMSET(b, -1, sizeof(b));
  57873. /* record layer */
  57874. /* handshake type */
  57875. b[idx++] = 22;
  57876. /* protocol version */
  57877. b[idx++] = 0xfe;
  57878. b[idx++] = 0xfd; /* DTLS 1.2 */
  57879. /* epoch 0 */
  57880. XMEMSET(b + idx, 0, 2);
  57881. idx += 2;
  57882. /* sequence number */
  57883. XMEMSET(b + idx, 0, 6);
  57884. seq_offset = idx + 2; /* increment only the low 32 bits */
  57885. idx += 6;
  57886. /* static length in BE */
  57887. c16toa(42, b + idx);
  57888. idx += 2;
  57889. /* handshake layer */
  57890. /* cert type */
  57891. b[idx++] = 11;
  57892. /* length */
  57893. c32to24(1000, b + idx);
  57894. idx += 3;
  57895. /* message seq */
  57896. c16toa(0, b + idx);
  57897. msg_offset = idx;
  57898. idx += 2;
  57899. /* frag offset */
  57900. c32to24(500, b + idx);
  57901. idx += 3;
  57902. /* frag length */
  57903. c32to24(30, b + idx);
  57904. idx += 3;
  57905. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  57906. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  57907. seq_number++, msg_number++, i++) {
  57908. struct timespec delay;
  57909. XMEMSET(&delay, 0, sizeof(delay));
  57910. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  57911. c32toa(seq_number, b + seq_offset);
  57912. c16toa(msg_number, b + msg_offset);
  57913. ret = (int)send(fd, b, 55, 0);
  57914. nanosleep(&delay, NULL);
  57915. }
  57916. }
  57917. #ifdef WOLFSSL_DTLS13
  57918. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  57919. {
  57920. const word16 sendCountMax = 100;
  57921. byte b[150]; /* buffer for the messages to send */
  57922. size_t idx = 0;
  57923. size_t msg_offset = 0;
  57924. int fd = wolfSSL_get_fd(ssl);
  57925. word16 msg_number = 10; /* start high so server has to buffer this */
  57926. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  57927. AssertIntEQ(ret, 1);
  57928. /* Now let's start spamming the peer with fragments it needs to store */
  57929. XMEMSET(b, -1, sizeof(b));
  57930. /* handshake type */
  57931. b[idx++] = 11;
  57932. /* length */
  57933. c32to24(10000, b + idx);
  57934. idx += 3;
  57935. /* message_seq */
  57936. msg_offset = idx;
  57937. idx += 2;
  57938. /* fragment_offset */
  57939. c32to24(5000, b + idx);
  57940. idx += 3;
  57941. /* fragment_length */
  57942. c32to24(100, b + idx);
  57943. idx += 3;
  57944. /* fragment contents */
  57945. idx += 100;
  57946. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  57947. byte sendBuf[150];
  57948. int sendSz = sizeof(sendBuf);
  57949. struct timespec delay;
  57950. XMEMSET(&delay, 0, sizeof(delay));
  57951. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  57952. c16toa(msg_number, b + msg_offset);
  57953. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  57954. (int)idx, handshake, 0, 0, 0);
  57955. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  57956. nanosleep(&delay, NULL);
  57957. }
  57958. }
  57959. #endif
  57960. static int test_wolfSSL_dtls_fragments(void)
  57961. {
  57962. EXPECT_DECLS;
  57963. callback_functions func_cb_client;
  57964. callback_functions func_cb_server;
  57965. size_t i;
  57966. struct test_params {
  57967. method_provider client_meth;
  57968. method_provider server_meth;
  57969. ssl_callback spammer;
  57970. } params[] = {
  57971. #if !defined(WOLFSSL_NO_TLS12)
  57972. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  57973. test_wolfSSL_dtls12_fragments_spammer},
  57974. #endif
  57975. #ifdef WOLFSSL_DTLS13
  57976. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  57977. test_wolfSSL_dtls13_fragments_spammer},
  57978. #endif
  57979. };
  57980. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  57981. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  57982. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  57983. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  57984. func_cb_server.method = params[i].server_meth;
  57985. func_cb_client.method = params[i].client_meth;
  57986. func_cb_client.ssl_ready = params[i].spammer;
  57987. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  57988. ExpectFalse(func_cb_client.return_code);
  57989. ExpectFalse(func_cb_server.return_code);
  57990. /* The socket should be closed by the server resulting in a
  57991. * socket error, fatal error or reading a close notify alert */
  57992. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  57993. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  57994. func_cb_client.last_err != FATAL_ERROR) {
  57995. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  57996. }
  57997. /* Check the server returned an error indicating the msg buffer
  57998. * was full */
  57999. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  58000. if (EXPECT_FAIL())
  58001. break;
  58002. }
  58003. return EXPECT_RESULT();
  58004. }
  58005. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  58006. {
  58007. int fd, ret;
  58008. byte alert_msg[] = {
  58009. 0x15, /* alert type */
  58010. 0xfe, 0xfd, /* version */
  58011. 0x00, 0x00, /* epoch */
  58012. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  58013. 0x00, 0x02, /* length */
  58014. 0x02, /* level: fatal */
  58015. 0x46 /* protocol version */
  58016. };
  58017. fd = wolfSSL_get_fd(ssl);
  58018. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  58019. AssertIntGT(ret, 0);
  58020. }
  58021. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  58022. {
  58023. callback_functions client_cbs, server_cbs;
  58024. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58025. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58026. client_cbs.doUdp = server_cbs.doUdp = 1;
  58027. if (version12) {
  58028. #if !defined(WOLFSSL_NO_TLS12)
  58029. client_cbs.method = wolfDTLSv1_2_client_method;
  58030. server_cbs.method = wolfDTLSv1_2_server_method;
  58031. #else
  58032. return TEST_SKIPPED;
  58033. #endif
  58034. }
  58035. else
  58036. {
  58037. #ifdef WOLFSSL_DTLS13
  58038. client_cbs.method = wolfDTLSv1_3_client_method;
  58039. server_cbs.method = wolfDTLSv1_3_server_method;
  58040. #else
  58041. return TEST_SKIPPED;
  58042. #endif /* WOLFSSL_DTLS13 */
  58043. }
  58044. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  58045. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58046. if (!client_cbs.return_code)
  58047. return TEST_FAIL;
  58048. if (!server_cbs.return_code)
  58049. return TEST_FAIL;
  58050. return TEST_SUCCESS;
  58051. }
  58052. static int test_wolfSSL_ignore_alert_before_cookie(void)
  58053. {
  58054. int ret;
  58055. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  58056. if (ret != 0)
  58057. return ret;
  58058. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  58059. if (ret != 0)
  58060. return ret;
  58061. return 0;
  58062. }
  58063. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  58064. {
  58065. int ret;
  58066. int fd;
  58067. byte bad_msg[] = {
  58068. 0x17, /* app data */
  58069. 0xaa, 0xfd, /* bad version */
  58070. 0x00, 0x01, /* epoch 1 */
  58071. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  58072. 0x00, 0x26, /* length: 38 bytes */
  58073. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  58074. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  58075. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  58076. 0x03, 0x18, 0x72
  58077. };
  58078. fd = wolfSSL_get_fd(ssl);
  58079. AssertIntGE(fd, 0);
  58080. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  58081. AssertIntEQ(ret, sizeof(bad_msg));
  58082. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  58083. AssertIntEQ(ret, sizeof("badrecordtest"));
  58084. }
  58085. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  58086. {
  58087. byte buf[100];
  58088. int ret;
  58089. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  58090. AssertIntGT(ret, 0);
  58091. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  58092. }
  58093. static int _test_wolfSSL_dtls_bad_record(
  58094. method_provider client_method, method_provider server_method)
  58095. {
  58096. callback_functions client_cbs, server_cbs;
  58097. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58098. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58099. client_cbs.doUdp = server_cbs.doUdp = 1;
  58100. client_cbs.method = client_method;
  58101. server_cbs.method = server_method;
  58102. client_cbs.on_result = test_wolfSSL_send_bad_record;
  58103. server_cbs.on_result = test_wolfSSL_read_string;
  58104. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58105. if (!client_cbs.return_code)
  58106. return TEST_FAIL;
  58107. if (!server_cbs.return_code)
  58108. return TEST_FAIL;
  58109. return TEST_SUCCESS;
  58110. }
  58111. static int test_wolfSSL_dtls_bad_record(void)
  58112. {
  58113. int ret = TEST_SUCCESS;
  58114. #if !defined(WOLFSSL_NO_TLS12)
  58115. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  58116. wolfDTLSv1_2_server_method);
  58117. #endif
  58118. #ifdef WOLFSSL_DTLS13
  58119. if (ret == TEST_SUCCESS) {
  58120. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  58121. wolfDTLSv1_3_server_method);
  58122. }
  58123. #endif /* WOLFSSL_DTLS13 */
  58124. return ret;
  58125. }
  58126. #else
  58127. static int test_wolfSSL_dtls_fragments(void) {
  58128. return TEST_SKIPPED;
  58129. }
  58130. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  58131. return TEST_SKIPPED;
  58132. }
  58133. static int test_wolfSSL_dtls_bad_record(void) {
  58134. return TEST_SKIPPED;
  58135. }
  58136. #endif
  58137. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  58138. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58139. defined(HAVE_IO_TESTS_DEPENDENCIES)
  58140. static byte test_AEAD_fail_decryption = 0;
  58141. static byte test_AEAD_seq_num = 0;
  58142. static byte test_AEAD_done = 0;
  58143. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  58144. {
  58145. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  58146. if (ret > 0) {
  58147. if (test_AEAD_fail_decryption) {
  58148. /* Modify the packet to trigger a decryption failure */
  58149. buf[ret/2] ^= 0xFF;
  58150. if (test_AEAD_fail_decryption == 1)
  58151. test_AEAD_fail_decryption = 0;
  58152. }
  58153. }
  58154. (void)ctx;
  58155. return ret;
  58156. }
  58157. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  58158. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  58159. {
  58160. if (sendLimit)
  58161. w64Zero(sendLimit);
  58162. switch (ssl->specs.bulk_cipher_algorithm) {
  58163. case wolfssl_aes_gcm:
  58164. if (sendLimit)
  58165. *sendLimit = AEAD_AES_LIMIT;
  58166. FALL_THROUGH;
  58167. case wolfssl_chacha:
  58168. if (hardLimit)
  58169. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  58170. if (keyUpdateLimit)
  58171. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  58172. break;
  58173. case wolfssl_aes_ccm:
  58174. if (sendLimit)
  58175. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  58176. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  58177. if (hardLimit)
  58178. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  58179. if (keyUpdateLimit)
  58180. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  58181. }
  58182. else {
  58183. if (hardLimit)
  58184. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  58185. if (keyUpdateLimit)
  58186. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  58187. }
  58188. break;
  58189. default:
  58190. fprintf(stderr, "Unrecognized bulk cipher");
  58191. AssertFalse(1);
  58192. break;
  58193. }
  58194. }
  58195. static void test_AEAD_limit_client(WOLFSSL* ssl)
  58196. {
  58197. int ret;
  58198. int i;
  58199. int didReKey = 0;
  58200. char msgBuf[20];
  58201. w64wrapper hardLimit;
  58202. w64wrapper keyUpdateLimit;
  58203. w64wrapper counter;
  58204. w64wrapper sendLimit;
  58205. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  58206. w64Zero(&counter);
  58207. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  58208. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  58209. for (i = 0; i < 10; i++) {
  58210. /* Test some failed decryptions */
  58211. test_AEAD_fail_decryption = 1;
  58212. w64Increment(&counter);
  58213. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58214. /* Should succeed since decryption failures are dropped */
  58215. AssertIntGT(ret, 0);
  58216. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  58217. }
  58218. test_AEAD_fail_decryption = 1;
  58219. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  58220. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  58221. /* 100 read calls should be enough to complete the key update */
  58222. w64Zero(&counter);
  58223. for (i = 0; i < 100; i++) {
  58224. /* Key update should be sent and negotiated */
  58225. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58226. AssertIntGT(ret, 0);
  58227. /* Epoch after one key update is 4 */
  58228. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  58229. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  58230. didReKey = 1;
  58231. break;
  58232. }
  58233. }
  58234. AssertTrue(didReKey);
  58235. if (!w64IsZero(sendLimit)) {
  58236. /* Test the sending limit for AEAD ciphers */
  58237. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  58238. test_AEAD_seq_num = 1;
  58239. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  58240. AssertIntGT(ret, 0);
  58241. didReKey = 0;
  58242. w64Zero(&counter);
  58243. /* 100 read calls should be enough to complete the key update */
  58244. for (i = 0; i < 100; i++) {
  58245. /* Key update should be sent and negotiated */
  58246. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58247. AssertIntGT(ret, 0);
  58248. /* Epoch after another key update is 5 */
  58249. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  58250. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  58251. didReKey = 1;
  58252. break;
  58253. }
  58254. }
  58255. AssertTrue(didReKey);
  58256. }
  58257. test_AEAD_fail_decryption = 2;
  58258. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  58259. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  58260. /* Connection should fail with a DECRYPT_ERROR */
  58261. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58262. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  58263. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  58264. test_AEAD_done = 1;
  58265. }
  58266. int counter = 0;
  58267. static void test_AEAD_limit_server(WOLFSSL* ssl)
  58268. {
  58269. char msgBuf[] = "Sending data";
  58270. int ret = WOLFSSL_SUCCESS;
  58271. w64wrapper sendLimit;
  58272. SOCKET_T fd = wolfSSL_get_fd(ssl);
  58273. struct timespec delay;
  58274. XMEMSET(&delay, 0, sizeof(delay));
  58275. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  58276. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  58277. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  58278. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  58279. while (!test_AEAD_done && ret > 0) {
  58280. counter++;
  58281. if (test_AEAD_seq_num) {
  58282. /* We need to update the seq number so that we can understand the
  58283. * peer. Otherwise we will incorrectly interpret the seq number. */
  58284. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  58285. AssertNotNull(e);
  58286. e->nextPeerSeqNumber = sendLimit;
  58287. test_AEAD_seq_num = 0;
  58288. }
  58289. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  58290. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  58291. nanosleep(&delay, NULL);
  58292. }
  58293. }
  58294. static int test_wolfSSL_dtls_AEAD_limit(void)
  58295. {
  58296. callback_functions func_cb_client;
  58297. callback_functions func_cb_server;
  58298. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  58299. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  58300. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  58301. func_cb_server.method = wolfDTLSv1_3_server_method;
  58302. func_cb_client.method = wolfDTLSv1_3_client_method;
  58303. func_cb_server.on_result = test_AEAD_limit_server;
  58304. func_cb_client.on_result = test_AEAD_limit_client;
  58305. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  58306. if (!func_cb_client.return_code)
  58307. return TEST_FAIL;
  58308. if (!func_cb_server.return_code)
  58309. return TEST_FAIL;
  58310. return TEST_SUCCESS;
  58311. }
  58312. #else
  58313. static int test_wolfSSL_dtls_AEAD_limit(void)
  58314. {
  58315. return TEST_SKIPPED;
  58316. }
  58317. #endif
  58318. #if defined(WOLFSSL_DTLS) && \
  58319. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  58320. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  58321. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  58322. {
  58323. int fd, ret;
  58324. byte ch_msg[] = {
  58325. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  58326. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  58327. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  58328. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  58329. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  58330. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58331. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58332. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58333. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58334. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  58335. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  58336. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  58337. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  58338. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  58339. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  58340. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  58341. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  58342. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  58343. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  58344. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  58345. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  58346. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  58347. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  58348. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  58349. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  58350. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  58351. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  58352. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  58353. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  58354. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  58355. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  58356. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  58357. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  58358. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  58359. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  58360. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  58361. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  58362. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  58363. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  58364. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  58365. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  58366. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  58367. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  58368. 0x31, 0x68, 0x4c
  58369. };
  58370. fd = wolfSSL_get_fd(ssl);
  58371. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  58372. AssertIntGT(ret, 0);
  58373. /* consume the HRR otherwise handshake will fail */
  58374. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  58375. AssertIntGT(ret, 0);
  58376. }
  58377. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58378. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  58379. {
  58380. int fd, ret;
  58381. byte ch_msh_invalid_cookie[] = {
  58382. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  58383. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  58384. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  58385. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  58386. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  58387. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  58388. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  58389. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  58390. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  58391. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  58392. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  58393. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  58394. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  58395. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  58396. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58397. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58398. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58399. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58400. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  58401. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  58402. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  58403. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  58404. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  58405. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  58406. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  58407. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  58408. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  58409. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  58410. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  58411. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  58412. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  58413. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  58414. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  58415. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  58416. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  58417. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  58418. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  58419. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  58420. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  58421. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  58422. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  58423. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  58424. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  58425. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  58426. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  58427. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  58428. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  58429. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  58430. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  58431. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  58432. 0x23, 0x00, 0x00
  58433. };
  58434. byte alert_reply[50];
  58435. byte expected_alert_reply[] = {
  58436. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  58437. 0x02, 0x02, 0x2f
  58438. };
  58439. fd = wolfSSL_get_fd(ssl);
  58440. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  58441. AssertIntGT(ret, 0);
  58442. /* should reply with an illegal_parameter reply */
  58443. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  58444. AssertIntEQ(ret, sizeof(expected_alert_reply));
  58445. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  58446. }
  58447. #endif
  58448. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  58449. {
  58450. #ifndef NO_MD5
  58451. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  58452. #elif !defined(NO_SHA)
  58453. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  58454. #elif !defined(NO_SHA256)
  58455. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  58456. #else
  58457. #error "We need a digest to hash the WOLFSSL object"
  58458. #endif
  58459. byte hashBuf[WC_MAX_DIGEST_SIZE];
  58460. wc_HashAlg hash;
  58461. const TLSX* exts = ssl->extensions;
  58462. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  58463. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  58464. * InitHandshakeHashes */
  58465. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  58466. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  58467. /* Following fields are not important to compare */
  58468. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  58469. sslCopy.buffers.inputBuffer.buffer = NULL;
  58470. sslCopy.buffers.inputBuffer.bufferSize = 0;
  58471. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  58472. sslCopy.buffers.inputBuffer.offset = 0;
  58473. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  58474. sslCopy.buffers.outputBuffer.buffer = NULL;
  58475. sslCopy.buffers.outputBuffer.bufferSize = 0;
  58476. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  58477. sslCopy.buffers.outputBuffer.offset = 0;
  58478. sslCopy.error = 0;
  58479. sslCopy.curSize = 0;
  58480. sslCopy.curStartIdx = 0;
  58481. sslCopy.keys.curSeq_lo = 0;
  58482. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  58483. #ifdef WOLFSSL_DTLS13
  58484. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  58485. sslCopy.dtls13FastTimeout = 0;
  58486. #endif
  58487. sslCopy.keys.dtls_peer_handshake_number = 0;
  58488. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  58489. sslCopy.hsHashes = NULL;
  58490. #ifdef WOLFSSL_ASYNC_IO
  58491. #ifdef WOLFSSL_ASYNC_CRYPT
  58492. sslCopy.asyncDev = NULL;
  58493. #endif
  58494. sslCopy.async = NULL;
  58495. #endif
  58496. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  58497. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  58498. /* hash extension list */
  58499. while (exts != NULL) {
  58500. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  58501. exts = exts->next;
  58502. }
  58503. /* Hash suites */
  58504. if (sslCopy.suites != NULL) {
  58505. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  58506. sizeof(struct Suites)), 0);
  58507. }
  58508. /* Hash hsHashes */
  58509. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  58510. sizeof(*hsHashes)), 0);
  58511. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  58512. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  58513. return MakeWordFromHash(hashBuf);
  58514. }
  58515. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  58516. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  58517. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  58518. char *buf, int sz, void *ctx)
  58519. {
  58520. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  58521. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  58522. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  58523. }
  58524. else {
  58525. return WOLFSSL_CBIO_ERR_WANT_READ;
  58526. }
  58527. }
  58528. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  58529. {
  58530. /* Compare the ssl object before and after one ClientHello msg */
  58531. SOCKET_T fd = wolfSSL_get_fd(ssl);
  58532. int res;
  58533. int err;
  58534. word32 initHash;
  58535. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  58536. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  58537. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  58538. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  58539. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  58540. (void)initHash;
  58541. res = tcp_select(fd, 5);
  58542. /* We are expecting a msg. A timeout indicates failure. */
  58543. AssertIntEQ(res, TEST_RECV_READY);
  58544. res = wolfSSL_accept(ssl);
  58545. err = wolfSSL_get_error(ssl, res);
  58546. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  58547. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  58548. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  58549. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  58550. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  58551. }
  58552. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58553. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  58554. {
  58555. int ret;
  58556. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  58557. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  58558. test_wolfSSL_dtls_compare_stateless(ssl);
  58559. }
  58560. #endif
  58561. static int test_wolfSSL_dtls_stateless(void)
  58562. {
  58563. callback_functions client_cbs, server_cbs;
  58564. size_t i;
  58565. struct {
  58566. method_provider client_meth;
  58567. method_provider server_meth;
  58568. ssl_callback client_ssl_ready;
  58569. ssl_callback server_ssl_ready;
  58570. } test_params[] = {
  58571. #if !defined(WOLFSSL_NO_TLS12)
  58572. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  58573. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  58574. #endif
  58575. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  58576. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  58577. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  58578. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  58579. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  58580. #endif
  58581. };
  58582. if (0 == sizeof(test_params)){
  58583. return TEST_SKIPPED;
  58584. }
  58585. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  58586. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58587. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58588. client_cbs.doUdp = server_cbs.doUdp = 1;
  58589. client_cbs.method = test_params[i].client_meth;
  58590. server_cbs.method = test_params[i].server_meth;
  58591. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  58592. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  58593. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58594. if (!client_cbs.return_code)
  58595. return TEST_FAIL;
  58596. if (!server_cbs.return_code)
  58597. return TEST_FAIL;
  58598. }
  58599. return TEST_SUCCESS;
  58600. }
  58601. #else
  58602. static int test_wolfSSL_dtls_stateless(void)
  58603. {
  58604. return TEST_SKIPPED;
  58605. }
  58606. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  58607. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  58608. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  58609. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  58610. !defined(WOLFSSL_NO_CLIENT_AUTH))
  58611. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  58612. {
  58613. int ret;
  58614. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  58615. fprintf(stderr, "loading cert %s failed\n", certA);
  58616. fprintf(stderr, "Error: (%d): %s\n", ret,
  58617. wolfSSL_ERR_reason_error_string(ret));
  58618. return -1;
  58619. }
  58620. return 0;
  58621. }
  58622. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  58623. {
  58624. int ret;
  58625. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  58626. != WOLFSSL_SUCCESS) {
  58627. fprintf(stderr, "could not verify the cert: %s\n", certA);
  58628. fprintf(stderr, "Error: (%d): %s\n", ret,
  58629. wolfSSL_ERR_reason_error_string(ret));
  58630. return -1;
  58631. }
  58632. else {
  58633. fprintf(stderr, "successfully verified: %s\n", certA);
  58634. }
  58635. return 0;
  58636. }
  58637. #define LOAD_ONE_CA(a, b, c, d) \
  58638. do { \
  58639. (a) = load_ca_into_cm(c, d); \
  58640. if ((a) != 0) \
  58641. return (b); \
  58642. else \
  58643. (b)--; \
  58644. } while(0)
  58645. #define VERIFY_ONE_CERT(a, b, c, d) \
  58646. do { \
  58647. (a) = verify_cert_with_cm(c, d);\
  58648. if ((a) != 0) \
  58649. return (b); \
  58650. else \
  58651. (b)--; \
  58652. } while(0)
  58653. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  58654. {
  58655. int ret;
  58656. int i = -1;
  58657. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  58658. char chainGArr[9][50] = {"certs/ca-cert.pem",
  58659. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  58660. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  58661. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  58662. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  58663. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  58664. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  58665. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  58666. "certs/test-pathlen/chainG-entity.pem"};
  58667. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  58668. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  58669. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  58670. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  58671. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  58672. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  58673. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  58674. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  58675. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  58676. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  58677. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  58678. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  58679. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  58680. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  58681. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  58682. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  58683. /* test validating the entity twice, should have no effect on pathLen since
  58684. * entity/leaf cert */
  58685. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  58686. return ret;
  58687. }
  58688. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  58689. {
  58690. int ret;
  58691. int i = -1;
  58692. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  58693. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  58694. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  58695. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  58696. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  58697. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  58698. */
  58699. char chainHArr[6][50] = {"certs/ca-cert.pem",
  58700. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  58701. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  58702. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  58703. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  58704. "certs/test-pathlen/chainH-entity.pem"};
  58705. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  58706. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  58707. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  58708. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  58709. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  58710. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  58711. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  58712. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  58713. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  58714. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  58715. return ret;
  58716. }
  58717. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  58718. {
  58719. int ret;
  58720. int i = -1;
  58721. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  58722. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  58723. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  58724. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  58725. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  58726. */
  58727. char chainIArr[5][50] = {"certs/ca-cert.pem",
  58728. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  58729. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  58730. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  58731. "certs/test-pathlen/chainI-entity.pem"};
  58732. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  58733. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  58734. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  58735. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  58736. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  58737. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  58738. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  58739. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  58740. return ret;
  58741. }
  58742. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  58743. {
  58744. int ret;
  58745. int i = -1;
  58746. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  58747. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  58748. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  58749. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  58750. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  58751. */
  58752. char chainJArr[6][50] = {"certs/ca-cert.pem",
  58753. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  58754. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  58755. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  58756. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  58757. "certs/test-pathlen/chainJ-entity.pem"};
  58758. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  58759. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  58760. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  58761. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  58762. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  58763. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  58764. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  58765. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  58766. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  58767. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  58768. return ret;
  58769. }
  58770. static int test_various_pathlen_chains(void)
  58771. {
  58772. EXPECT_DECLS;
  58773. WOLFSSL_CERT_MANAGER* cm = NULL;
  58774. /* Test chain G (large chain with varying pathLens) */
  58775. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58776. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  58777. ExpectIntEQ(test_chainG(cm), -1);
  58778. #else
  58779. ExpectIntEQ(test_chainG(cm), 0);
  58780. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  58781. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58782. wolfSSL_CertManagerFree(cm);
  58783. /* end test chain G */
  58784. /* Test chain H (5 chain with same pathLens) */
  58785. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58786. ExpectIntLT(test_chainH(cm), 0);
  58787. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58788. wolfSSL_CertManagerFree(cm);
  58789. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58790. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58791. wolfSSL_CertManagerFree(cm);
  58792. /* end test chain H */
  58793. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  58794. * followed by 2 ICA's, should pass) */
  58795. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58796. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  58797. ExpectIntEQ(test_chainI(cm), -1);
  58798. #else
  58799. ExpectIntEQ(test_chainI(cm), 0);
  58800. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  58801. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58802. wolfSSL_CertManagerFree(cm);
  58803. cm = NULL;
  58804. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58805. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58806. wolfSSL_CertManagerFree(cm);
  58807. cm = NULL;
  58808. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  58809. * this time followed by 3 ICA's, should fail */
  58810. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58811. ExpectIntLT(test_chainJ(cm), 0);
  58812. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58813. wolfSSL_CertManagerFree(cm);
  58814. cm = NULL;
  58815. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  58816. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  58817. wolfSSL_CertManagerFree(cm);
  58818. return EXPECT_RESULT();
  58819. }
  58820. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  58821. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58822. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  58823. {
  58824. EXPECT_DECLS;
  58825. byte ekm[100] = {0};
  58826. (void)ctx;
  58827. /* Success Cases */
  58828. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58829. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  58830. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58831. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  58832. /* Use some random context */
  58833. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58834. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  58835. /* Failure cases */
  58836. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58837. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  58838. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58839. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  58840. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58841. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  58842. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58843. "extended master secret", XSTR_SIZEOF("extended master secret"),
  58844. NULL, 0, 0), 0);
  58845. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  58846. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  58847. return EXPECT_RESULT();
  58848. }
  58849. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  58850. {
  58851. wolfSSL_KeepArrays(ssl);
  58852. return TEST_SUCCESS;
  58853. }
  58854. static int test_export_keying_material(void)
  58855. {
  58856. EXPECT_DECLS;
  58857. test_ssl_cbf serverCb;
  58858. test_ssl_cbf clientCb;
  58859. XMEMSET(&serverCb, 0, sizeof(serverCb));
  58860. XMEMSET(&clientCb, 0, sizeof(clientCb));
  58861. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  58862. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  58863. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  58864. return EXPECT_RESULT();
  58865. }
  58866. #endif /* HAVE_KEYING_MATERIAL */
  58867. static int test_wolfSSL_THREADID_hash(void)
  58868. {
  58869. EXPECT_DECLS;
  58870. #if defined(OPENSSL_EXTRA)
  58871. CRYPTO_THREADID id;
  58872. CRYPTO_THREADID_current(NULL);
  58873. /* Hash result is unsigned long. */
  58874. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  58875. XMEMSET(&id, 0, sizeof(id));
  58876. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  58877. #endif /* OPENSSL_EXTRA */
  58878. return EXPECT_RESULT();
  58879. }
  58880. static int test_wolfSSL_set_ecdh_auto(void)
  58881. {
  58882. EXPECT_DECLS;
  58883. #if defined(OPENSSL_EXTRA)
  58884. WOLFSSL* ssl = NULL;
  58885. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  58886. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  58887. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  58888. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  58889. #endif /* OPENSSL_EXTRA */
  58890. return EXPECT_RESULT();
  58891. }
  58892. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  58893. {
  58894. EXPECT_DECLS;
  58895. #if defined(OPENSSL_EXTRA)
  58896. WOLFSSL_CTX* ctx = NULL;
  58897. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  58898. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  58899. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  58900. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  58901. #endif /* OPENSSL_EXTRA */
  58902. return EXPECT_RESULT();
  58903. }
  58904. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  58905. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  58906. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  58907. {
  58908. EXPECT_DECLS;
  58909. callback_functions* callbacks = NULL;
  58910. WOLFSSL_CTX* ctx = NULL;
  58911. WOLFSSL* ssl = NULL;
  58912. SOCKET_T sfd = 0;
  58913. SOCKET_T cfd = 0;
  58914. word16 port;
  58915. char msg[] = "I hear you fa shizzle!";
  58916. int len = (int) XSTRLEN(msg);
  58917. char input[1024];
  58918. int ret = 0;
  58919. int err = 0;
  58920. if (!args)
  58921. WOLFSSL_RETURN_FROM_THREAD(0);
  58922. ((func_args*)args)->return_code = TEST_FAIL;
  58923. callbacks = ((func_args*)args)->callbacks;
  58924. ctx = wolfSSL_CTX_new(callbacks->method());
  58925. #if defined(USE_WINDOWS_API)
  58926. port = ((func_args*)args)->signal->port;
  58927. #else
  58928. /* Let tcp_listen assign port */
  58929. port = 0;
  58930. #endif
  58931. #ifdef WOLFSSL_TIRTOS
  58932. fdOpenSession(Task_self());
  58933. #endif
  58934. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  58935. caCertFile, 0));
  58936. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  58937. svrCertFile, WOLFSSL_FILETYPE_PEM));
  58938. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  58939. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  58940. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  58941. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  58942. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58943. #elif !defined(NO_DH)
  58944. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  58945. #endif
  58946. if (callbacks->ctx_ready)
  58947. callbacks->ctx_ready(ctx);
  58948. ssl = wolfSSL_new(ctx);
  58949. ExpectNotNull(ssl);
  58950. /* listen and accept */
  58951. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  58952. CloseSocket(sfd);
  58953. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  58954. if (callbacks->ssl_ready)
  58955. callbacks->ssl_ready(ssl);
  58956. if (EXPECT_SUCCESS()) {
  58957. do {
  58958. err = 0; /* Reset error */
  58959. ret = wolfSSL_accept(ssl);
  58960. if (ret != WOLFSSL_SUCCESS) {
  58961. err = wolfSSL_get_error(ssl, 0);
  58962. }
  58963. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  58964. }
  58965. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  58966. /* read and write data */
  58967. XMEMSET(input, 0, sizeof(input));
  58968. while (EXPECT_SUCCESS()) {
  58969. ret = wolfSSL_read(ssl, input, sizeof(input));
  58970. if (ret > 0) {
  58971. break;
  58972. }
  58973. else {
  58974. err = wolfSSL_get_error(ssl,ret);
  58975. if (err == WOLFSSL_ERROR_WANT_READ) {
  58976. continue;
  58977. }
  58978. break;
  58979. }
  58980. }
  58981. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  58982. do {
  58983. ret = wolfSSL_write(ssl, msg, len);
  58984. if (ret > 0) {
  58985. break;
  58986. }
  58987. } while (ret < 0);
  58988. }
  58989. /* bidirectional shutdown */
  58990. while (EXPECT_SUCCESS()) {
  58991. ret = wolfSSL_shutdown(ssl);
  58992. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  58993. if (ret == WOLFSSL_SUCCESS) {
  58994. break;
  58995. }
  58996. }
  58997. if (EXPECT_SUCCESS()) {
  58998. /* wait for the peer to disconnect the tcp connection */
  58999. do {
  59000. ret = wolfSSL_read(ssl, input, sizeof(input));
  59001. err = wolfSSL_get_error(ssl, ret);
  59002. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  59003. }
  59004. /* detect TCP disconnect */
  59005. ExpectIntLE(ret,WOLFSSL_FAILURE);
  59006. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  59007. ((func_args*)args)->return_code = EXPECT_RESULT();
  59008. wolfSSL_free(ssl);
  59009. wolfSSL_CTX_free(ctx);
  59010. CloseSocket(cfd);
  59011. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  59012. wc_ecc_fp_free(); /* free per thread cache */
  59013. #endif
  59014. WOLFSSL_RETURN_FROM_THREAD(0);
  59015. }
  59016. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  59017. {
  59018. EXPECT_DECLS;
  59019. callback_functions* callbacks = NULL;
  59020. WOLFSSL_CTX* ctx = NULL;
  59021. WOLFSSL* ssl = NULL;
  59022. SOCKET_T sfd = 0;
  59023. char msg[] = "hello wolfssl server!";
  59024. int len = (int) XSTRLEN(msg);
  59025. char input[1024];
  59026. int idx;
  59027. int ret, err;
  59028. if (!args)
  59029. WOLFSSL_RETURN_FROM_THREAD(0);
  59030. ((func_args*)args)->return_code = TEST_FAIL;
  59031. callbacks = ((func_args*)args)->callbacks;
  59032. ctx = wolfSSL_CTX_new(callbacks->method());
  59033. #ifdef WOLFSSL_TIRTOS
  59034. fdOpenSession(Task_self());
  59035. #endif
  59036. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  59037. caCertFile, 0));
  59038. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  59039. cliCertFile, WOLFSSL_FILETYPE_PEM));
  59040. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  59041. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  59042. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  59043. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  59044. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  59045. if (EXPECT_SUCCESS()) {
  59046. do {
  59047. err = 0; /* Reset error */
  59048. ret = wolfSSL_connect(ssl);
  59049. if (ret != WOLFSSL_SUCCESS) {
  59050. err = wolfSSL_get_error(ssl, 0);
  59051. }
  59052. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  59053. }
  59054. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  59055. if (EXPECT_SUCCESS()) {
  59056. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  59057. input[idx] = 0;
  59058. }
  59059. }
  59060. if (EXPECT_SUCCESS()) {
  59061. ret = wolfSSL_shutdown(ssl);
  59062. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  59063. ret = wolfSSL_shutdown(ssl);
  59064. }
  59065. }
  59066. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  59067. ((func_args*)args)->return_code = EXPECT_RESULT();
  59068. wolfSSL_free(ssl);
  59069. wolfSSL_CTX_free(ctx);
  59070. CloseSocket(sfd);
  59071. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  59072. wc_ecc_fp_free(); /* free per thread cache */
  59073. #endif
  59074. WOLFSSL_RETURN_FROM_THREAD(0);
  59075. }
  59076. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  59077. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  59078. /* This test is to check wolfSSL_read behaves as same as
  59079. * openSSL when it is called after SSL_shutdown completes.
  59080. */
  59081. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  59082. {
  59083. EXPECT_DECLS;
  59084. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  59085. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  59086. tcp_ready ready;
  59087. func_args client_args;
  59088. func_args server_args;
  59089. THREAD_TYPE serverThread;
  59090. THREAD_TYPE clientThread;
  59091. callback_functions server_cbf;
  59092. callback_functions client_cbf;
  59093. #ifdef WOLFSSL_TIRTOS
  59094. fdOpenSession(Task_self());
  59095. #endif
  59096. StartTCP();
  59097. InitTcpReady(&ready);
  59098. #if defined(USE_WINDOWS_API)
  59099. /* use RNG to get random port if using windows */
  59100. ready.port = GetRandomPort();
  59101. #endif
  59102. XMEMSET(&client_args, 0, sizeof(func_args));
  59103. XMEMSET(&server_args, 0, sizeof(func_args));
  59104. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  59105. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  59106. server_cbf.method = wolfTLSv1_2_server_method;
  59107. client_cbf.method = wolfTLSv1_2_client_method;
  59108. server_args.callbacks = &server_cbf;
  59109. client_args.callbacks = &client_cbf;
  59110. server_args.signal = &ready;
  59111. client_args.signal = &ready;
  59112. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  59113. wait_tcp_ready(&server_args);
  59114. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  59115. join_thread(clientThread);
  59116. join_thread(serverThread);
  59117. ExpectTrue(client_args.return_code);
  59118. ExpectTrue(server_args.return_code);
  59119. FreeTcpReady(&ready);
  59120. #endif
  59121. return EXPECT_RESULT();
  59122. }
  59123. static int test_wolfSSL_CTX_get_min_proto_version(void)
  59124. {
  59125. EXPECT_DECLS;
  59126. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  59127. WOLFSSL_CTX *ctx = NULL;
  59128. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59129. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  59130. WOLFSSL_SUCCESS);
  59131. #ifdef WOLFSSL_ALLOW_SSLV3
  59132. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  59133. #else
  59134. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  59135. #endif
  59136. wolfSSL_CTX_free(ctx);
  59137. ctx = NULL;
  59138. #ifndef NO_OLD_TLS
  59139. #ifdef WOLFSSL_ALLOW_TLSV10
  59140. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  59141. #else
  59142. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59143. #endif
  59144. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  59145. WOLFSSL_SUCCESS);
  59146. #ifdef WOLFSSL_ALLOW_TLSV10
  59147. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  59148. #else
  59149. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  59150. #endif
  59151. wolfSSL_CTX_free(ctx);
  59152. ctx = NULL;
  59153. #endif
  59154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  59155. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  59156. WOLFSSL_SUCCESS);
  59157. #ifndef NO_OLD_TLS
  59158. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  59159. #else
  59160. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  59161. #endif
  59162. wolfSSL_CTX_free(ctx);
  59163. ctx = NULL;
  59164. #ifndef WOLFSSL_NO_TLS12
  59165. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  59166. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  59167. WOLFSSL_SUCCESS);
  59168. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  59169. wolfSSL_CTX_free(ctx);
  59170. ctx = NULL;
  59171. #endif
  59172. #ifdef WOLFSSL_TLS13
  59173. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  59174. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  59175. WOLFSSL_SUCCESS);
  59176. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  59177. wolfSSL_CTX_free(ctx);
  59178. ctx = NULL;
  59179. #endif
  59180. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  59181. return EXPECT_RESULT();
  59182. }
  59183. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59184. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59185. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59186. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  59187. static int test_wolfSSL_set_SSL_CTX(void)
  59188. {
  59189. EXPECT_DECLS;
  59190. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  59191. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  59192. !defined(NO_RSA)
  59193. WOLFSSL_CTX *ctx1 = NULL;
  59194. WOLFSSL_CTX *ctx2 = NULL;
  59195. WOLFSSL *ssl = NULL;
  59196. const byte *session_id1 = (const byte *)"CTX1";
  59197. const byte *session_id2 = (const byte *)"CTX2";
  59198. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  59199. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  59200. WOLFSSL_FILETYPE_PEM));
  59201. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  59202. WOLFSSL_FILETYPE_PEM));
  59203. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  59204. WOLFSSL_SUCCESS);
  59205. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  59206. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  59207. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  59208. WOLFSSL_SUCCESS);
  59209. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  59210. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  59211. WOLFSSL_FILETYPE_PEM));
  59212. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  59213. WOLFSSL_FILETYPE_PEM));
  59214. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  59215. WOLFSSL_SUCCESS);
  59216. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  59217. WOLFSSL_SUCCESS);
  59218. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  59219. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  59220. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  59221. WOLFSSL_SUCCESS);
  59222. #ifdef HAVE_SESSION_TICKET
  59223. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  59224. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  59225. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  59226. #endif
  59227. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  59228. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  59229. #ifdef WOLFSSL_INT_H
  59230. #ifdef WOLFSSL_SESSION_ID_CTX
  59231. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  59232. #endif
  59233. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  59234. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  59235. #endif
  59236. #ifdef HAVE_SESSION_TICKET
  59237. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  59238. #endif
  59239. /* Set the ctx1 that has TLSv1.3 as max proto version */
  59240. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  59241. /* MUST not change proto versions of ssl */
  59242. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  59243. #ifdef HAVE_SESSION_TICKET
  59244. /* MUST not change */
  59245. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  59246. #endif
  59247. /* MUST change */
  59248. #ifdef WOLFSSL_INT_H
  59249. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  59250. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  59251. #ifdef WOLFSSL_SESSION_ID_CTX
  59252. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  59253. #endif
  59254. #endif
  59255. wolfSSL_free(ssl);
  59256. wolfSSL_CTX_free(ctx1);
  59257. wolfSSL_CTX_free(ctx2);
  59258. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  59259. return EXPECT_RESULT();
  59260. }
  59261. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59262. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59263. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59264. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  59265. static int test_wolfSSL_security_level(void)
  59266. {
  59267. EXPECT_DECLS;
  59268. #if defined(OPENSSL_EXTRA)
  59269. SSL_CTX *ctx = NULL;
  59270. #ifdef WOLFSSL_TLS13
  59271. #ifdef NO_WOLFSSL_SERVER
  59272. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59273. #else
  59274. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59275. #endif
  59276. SSL_CTX_set_security_level(NULL, 1);
  59277. SSL_CTX_set_security_level(ctx, 1);
  59278. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  59279. /* Stub so nothing happens. */
  59280. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  59281. SSL_CTX_free(ctx);
  59282. #else
  59283. (void)ctx;
  59284. #endif
  59285. #endif
  59286. return EXPECT_RESULT();
  59287. }
  59288. static int test_wolfSSL_SSL_in_init(void)
  59289. {
  59290. EXPECT_DECLS;
  59291. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  59292. SSL_CTX* ctx = NULL;
  59293. SSL* ssl = NULL;
  59294. const char* testCertFile;
  59295. const char* testKeyFile;
  59296. #ifdef WOLFSSL_TLS13
  59297. #ifdef NO_WOLFSSL_SERVER
  59298. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59299. #else
  59300. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59301. #endif
  59302. #else
  59303. #ifdef NO_WOLFSSL_SERVER
  59304. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  59305. #else
  59306. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59307. #endif
  59308. #endif
  59309. #ifndef NO_RSA
  59310. testCertFile = svrCertFile;
  59311. testKeyFile = svrKeyFile;
  59312. #elif defined(HAVE_ECC)
  59313. testCertFile = eccCertFile;
  59314. testKeyFile = eccKeyFile;
  59315. #else
  59316. testCertFile = NULL;
  59317. testKeyFile = NULL;
  59318. #endif
  59319. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  59320. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  59321. SSL_FILETYPE_PEM));
  59322. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  59323. SSL_FILETYPE_PEM));
  59324. }
  59325. ExpectNotNull(ssl = SSL_new(ctx));
  59326. ExpectIntEQ(SSL_in_init(ssl), 1);
  59327. SSL_CTX_free(ctx);
  59328. SSL_free(ssl);
  59329. #endif
  59330. return EXPECT_RESULT();
  59331. }
  59332. static int test_wolfSSL_CTX_set_timeout(void)
  59333. {
  59334. EXPECT_DECLS;
  59335. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  59336. int timeout;
  59337. WOLFSSL_CTX* ctx = NULL;
  59338. (void)timeout;
  59339. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59340. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  59341. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  59342. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  59343. */
  59344. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  59345. /* giving 0 as timeout value sets default timeout */
  59346. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  59347. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  59348. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  59349. #else
  59350. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  59351. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  59352. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  59353. #endif
  59354. wolfSSL_CTX_free(ctx);
  59355. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  59356. return EXPECT_RESULT();
  59357. }
  59358. static int test_wolfSSL_OpenSSL_version(void)
  59359. {
  59360. EXPECT_DECLS;
  59361. #if defined(OPENSSL_EXTRA)
  59362. const char* ver;
  59363. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  59364. ExpectNotNull(ver = OpenSSL_version(0));
  59365. #else
  59366. ExpectNotNull(ver = OpenSSL_version());
  59367. #endif
  59368. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  59369. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  59370. #endif
  59371. return EXPECT_RESULT();
  59372. }
  59373. static int test_CONF_CTX_CMDLINE(void)
  59374. {
  59375. EXPECT_DECLS;
  59376. #if defined(OPENSSL_ALL)
  59377. SSL_CTX* ctx = NULL;
  59378. SSL_CONF_CTX* cctx = NULL;
  59379. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  59380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59381. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  59382. /* set flags */
  59383. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  59384. WOLFSSL_CONF_FLAG_CMDLINE);
  59385. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  59386. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  59387. /* cmd invalid command */
  59388. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  59389. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  59390. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  59391. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  59392. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  59393. /* cmd Certificate and Private Key*/
  59394. {
  59395. #if !defined(NO_CERTS) && !defined(NO_RSA)
  59396. const char* ourCert = svrCertFile;
  59397. const char* ourKey = svrKeyFile;
  59398. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  59399. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  59400. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  59401. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  59402. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59403. #endif
  59404. }
  59405. /* cmd curves */
  59406. {
  59407. #if defined(HAVE_ECC)
  59408. const char* curve = "secp256r1";
  59409. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  59410. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  59411. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59412. #endif
  59413. }
  59414. /* cmd CipherString */
  59415. {
  59416. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  59417. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  59418. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  59419. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59420. }
  59421. /* cmd DH parameter */
  59422. {
  59423. #if !defined(NO_DH) && !defined(NO_BIO)
  59424. const char* ourdhcert = "./certs/dh2048.pem";
  59425. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  59426. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  59427. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59428. #endif
  59429. }
  59430. SSL_CTX_free(ctx);
  59431. SSL_CONF_CTX_free(cctx);
  59432. #endif /* OPENSSL_EXTRA */
  59433. return EXPECT_RESULT();
  59434. }
  59435. static int test_CONF_CTX_FILE(void)
  59436. {
  59437. EXPECT_DECLS;
  59438. #if defined(OPENSSL_ALL)
  59439. SSL_CTX* ctx = NULL;
  59440. SSL_CONF_CTX* cctx = NULL;
  59441. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  59442. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59443. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  59444. /* set flags */
  59445. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  59446. WOLFSSL_CONF_FLAG_FILE);
  59447. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  59448. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  59449. /* sanity check */
  59450. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  59451. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  59452. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  59453. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  59454. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  59455. /* cmd Certificate and Private Key*/
  59456. {
  59457. #if !defined(NO_CERTS) && !defined(NO_RSA)
  59458. const char* ourCert = svrCertFile;
  59459. const char* ourKey = svrKeyFile;
  59460. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  59461. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  59462. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  59463. WOLFSSL_SUCCESS);
  59464. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  59465. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59466. #endif
  59467. }
  59468. /* cmd curves */
  59469. {
  59470. #if defined(HAVE_ECC)
  59471. const char* curve = "secp256r1";
  59472. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  59473. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  59474. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59475. #endif
  59476. }
  59477. /* cmd CipherString */
  59478. {
  59479. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  59480. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  59481. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  59482. WOLFSSL_SUCCESS);
  59483. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59484. }
  59485. /* cmd DH parameter */
  59486. {
  59487. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  59488. const char* ourdhcert = "./certs/dh3072.pem";
  59489. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  59490. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  59491. WOLFSSL_SUCCESS);
  59492. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  59493. #endif
  59494. }
  59495. SSL_CTX_free(ctx);
  59496. SSL_CONF_CTX_free(cctx);
  59497. #endif /* OPENSSL_EXTRA */
  59498. return EXPECT_RESULT();
  59499. }
  59500. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  59501. {
  59502. EXPECT_DECLS;
  59503. #ifdef HAVE_EX_DATA
  59504. int idx1, idx2;
  59505. /* test for unsupported class index */
  59506. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  59507. 0,NULL, NULL, NULL, NULL ), -1);
  59508. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  59509. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  59510. 0,NULL, NULL, NULL, NULL ), -1);
  59511. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  59512. 0,NULL, NULL, NULL, NULL ), -1);
  59513. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  59514. 0,NULL, NULL, NULL, NULL ), -1);
  59515. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  59516. 0,NULL, NULL, NULL, NULL ), -1);
  59517. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  59518. 0,NULL, NULL, NULL, NULL ), -1);
  59519. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  59520. 0,NULL, NULL, NULL, NULL ), -1);
  59521. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  59522. 0,NULL, NULL, NULL, NULL ), -1);
  59523. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  59524. 0,NULL, NULL, NULL, NULL ), -1);
  59525. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  59526. 0,NULL, NULL, NULL, NULL ), -1);
  59527. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  59528. 0,NULL, NULL, NULL, NULL ), -1);
  59529. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  59530. 0,NULL, NULL, NULL, NULL ), -1);
  59531. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  59532. 0,NULL, NULL, NULL, NULL ), -1);
  59533. /* test for supported class index */
  59534. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  59535. 0,NULL, NULL, NULL, NULL );
  59536. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  59537. 0,NULL, NULL, NULL, NULL );
  59538. ExpectIntNE(idx1, -1);
  59539. ExpectIntNE(idx2, -1);
  59540. ExpectIntNE(idx1, idx2);
  59541. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  59542. 0,NULL, NULL, NULL, NULL );
  59543. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  59544. 0,NULL, NULL, NULL, NULL );
  59545. ExpectIntNE(idx1, -1);
  59546. ExpectIntNE(idx2, -1);
  59547. ExpectIntNE(idx1, idx2);
  59548. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  59549. 0,NULL, NULL, NULL, NULL );
  59550. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  59551. 0,NULL, NULL, NULL, NULL );
  59552. ExpectIntNE(idx1, -1);
  59553. ExpectIntNE(idx2, -1);
  59554. ExpectIntNE(idx1, idx2);
  59555. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  59556. 0,NULL, NULL, NULL, NULL );
  59557. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  59558. 0,NULL, NULL, NULL, NULL );
  59559. ExpectIntNE(idx1, -1);
  59560. ExpectIntNE(idx2, -1);
  59561. ExpectIntNE(idx1, idx2);
  59562. #endif /* HAVE_EX_DATA */
  59563. return EXPECT_RESULT();
  59564. }
  59565. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  59566. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  59567. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  59568. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  59569. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  59570. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  59571. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  59572. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  59573. #define SESSION_NEW_IDX_PTR "Testing"
  59574. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  59575. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  59576. {
  59577. AssertNotNull(p);
  59578. AssertNull(ptr);
  59579. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  59580. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  59581. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  59582. }
  59583. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  59584. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  59585. void* arg)
  59586. {
  59587. EXPECT_DECLS;
  59588. ExpectNotNull(out);
  59589. ExpectNotNull(in);
  59590. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  59591. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  59592. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  59593. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  59594. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  59595. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  59596. if (EXPECT_SUCCESS()) {
  59597. return SSL_SUCCESS;
  59598. }
  59599. else {
  59600. return SSL_FAILURE;
  59601. }
  59602. }
  59603. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  59604. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  59605. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  59606. {
  59607. EXPECT_DECLS;
  59608. ExpectNotNull(p);
  59609. ExpectNull(ptr);
  59610. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  59611. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  59612. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  59613. if (EXPECT_SUCCESS()) {
  59614. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  59615. }
  59616. }
  59617. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  59618. {
  59619. EXPECT_DECLS;
  59620. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  59621. (void*)SESSION_NEW_IDX_PTR,
  59622. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  59623. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  59624. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  59625. SSL_SESSION* s = SSL_SESSION_new();
  59626. SSL_SESSION* d = NULL;
  59627. ExpectNotNull(s);
  59628. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  59629. ExpectNotNull(d = SSL_SESSION_dup(s));
  59630. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  59631. SSL_SESSION_free(s);
  59632. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  59633. SSL_SESSION_free(d);
  59634. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  59635. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  59636. crypto_ex_cb_ctx_session = NULL;
  59637. return EXPECT_RESULT();
  59638. }
  59639. #else
  59640. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  59641. {
  59642. return TEST_SKIPPED;
  59643. }
  59644. #endif
  59645. static int test_wolfSSL_set_psk_use_session_callback(void)
  59646. {
  59647. EXPECT_DECLS;
  59648. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  59649. SSL_CTX* ctx = NULL;
  59650. SSL* ssl = NULL;
  59651. const char* testCertFile;
  59652. const char* testKeyFile;
  59653. #ifdef WOLFSSL_TLS13
  59654. #ifdef NO_WOLFSSL_SERVER
  59655. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  59656. #else
  59657. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  59658. #endif
  59659. #else
  59660. #ifdef NO_WOLFSSL_SERVER
  59661. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  59662. #else
  59663. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59664. #endif
  59665. #endif
  59666. #ifndef NO_RSA
  59667. testCertFile = svrCertFile;
  59668. testKeyFile = svrKeyFile;
  59669. #elif defined(HAVE_ECC)
  59670. testCertFile = eccCertFile;
  59671. testKeyFile = eccKeyFile;
  59672. #else
  59673. testCertFile = NULL;
  59674. testKeyFile = NULL;
  59675. #endif
  59676. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  59677. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  59678. SSL_FILETYPE_PEM));
  59679. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  59680. SSL_FILETYPE_PEM));
  59681. }
  59682. ExpectNotNull(ssl = SSL_new(ctx));
  59683. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  59684. SSL_CTX_free(ctx);
  59685. SSL_free(ssl);
  59686. #endif
  59687. return EXPECT_RESULT();
  59688. }
  59689. static int test_wolfSSL_ERR_strings(void)
  59690. {
  59691. EXPECT_DECLS;
  59692. #if !defined(NO_ERROR_STRINGS)
  59693. const char* err1 = "unsupported cipher suite";
  59694. const char* err2 = "wolfSSL PEM routines";
  59695. const char* err = NULL;
  59696. (void)err;
  59697. (void)err1;
  59698. (void)err2;
  59699. #if defined(OPENSSL_EXTRA)
  59700. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  59701. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  59702. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  59703. ExpectIntEQ((*err == '\0'), 1);
  59704. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  59705. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  59706. #else
  59707. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  59708. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  59709. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  59710. ExpectIntEQ((*err == '\0'), 1);
  59711. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  59712. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  59713. ExpectIntEQ((*err == '\0'), 1);
  59714. #endif
  59715. #endif
  59716. return EXPECT_RESULT();
  59717. }
  59718. static int test_wolfSSL_EVP_shake128(void)
  59719. {
  59720. EXPECT_DECLS;
  59721. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  59722. defined(WOLFSSL_SHAKE128)
  59723. const EVP_MD* md = NULL;
  59724. ExpectNotNull(md = EVP_shake128());
  59725. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  59726. #endif
  59727. return EXPECT_RESULT();
  59728. }
  59729. static int test_wolfSSL_EVP_shake256(void)
  59730. {
  59731. EXPECT_DECLS;
  59732. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  59733. defined(WOLFSSL_SHAKE256)
  59734. const EVP_MD* md = NULL;
  59735. ExpectNotNull(md = EVP_shake256());
  59736. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  59737. #endif
  59738. return EXPECT_RESULT();
  59739. }
  59740. /*
  59741. * Testing EVP digest API with SM3
  59742. */
  59743. static int test_wolfSSL_EVP_sm3(void)
  59744. {
  59745. int res = TEST_SKIPPED;
  59746. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  59747. EXPECT_DECLS;
  59748. const EVP_MD* md = NULL;
  59749. EVP_MD_CTX* mdCtx = NULL;
  59750. byte data[WC_SM3_BLOCK_SIZE * 4];
  59751. byte hash[WC_SM3_DIGEST_SIZE];
  59752. byte calcHash[WC_SM3_DIGEST_SIZE];
  59753. byte expHash[WC_SM3_DIGEST_SIZE] = {
  59754. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  59755. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  59756. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  59757. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  59758. };
  59759. word32 chunk;
  59760. word32 i;
  59761. unsigned int sz;
  59762. int ret;
  59763. XMEMSET(data, 0, sizeof(data));
  59764. md = EVP_sm3();
  59765. ExpectTrue(md != NULL);
  59766. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  59767. mdCtx = EVP_MD_CTX_new();
  59768. ExpectTrue(mdCtx != NULL);
  59769. /* Invalid Parameters */
  59770. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  59771. /* Valid Parameters */
  59772. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  59773. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  59774. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  59775. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  59776. /* Valid Parameters */
  59777. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  59778. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  59779. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  59780. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  59781. WOLFSSL_SUCCESS);
  59782. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  59783. WOLFSSL_SUCCESS);
  59784. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  59785. WOLFSSL_SUCCESS);
  59786. /* Ensure too many bytes for lengths. */
  59787. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  59788. WOLFSSL_SUCCESS);
  59789. /* Invalid Parameters */
  59790. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  59791. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  59792. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  59793. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  59794. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  59795. /* Valid Parameters */
  59796. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  59797. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  59798. /* Chunk tests. */
  59799. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  59800. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  59801. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  59802. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  59803. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  59804. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  59805. WOLFSSL_SUCCESS);
  59806. }
  59807. if (i < (word32)sizeof(data)) {
  59808. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  59809. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  59810. }
  59811. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  59812. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  59813. }
  59814. /* Not testing when the low 32-bit length overflows. */
  59815. ret = EVP_MD_CTX_cleanup(mdCtx);
  59816. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  59817. wolfSSL_EVP_MD_CTX_free(mdCtx);
  59818. res = EXPECT_RESULT();
  59819. #endif
  59820. return res;
  59821. } /* END test_EVP_sm3 */
  59822. static int test_EVP_blake2(void)
  59823. {
  59824. EXPECT_DECLS;
  59825. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  59826. const EVP_MD* md = NULL;
  59827. (void)md;
  59828. #if defined(HAVE_BLAKE2)
  59829. ExpectNotNull(md = EVP_blake2b512());
  59830. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  59831. #endif
  59832. #if defined(HAVE_BLAKE2S)
  59833. ExpectNotNull(md = EVP_blake2s256());
  59834. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  59835. #endif
  59836. #endif
  59837. return EXPECT_RESULT();
  59838. }
  59839. #if defined(OPENSSL_EXTRA)
  59840. static void list_md_fn(const EVP_MD* m, const char* from,
  59841. const char* to, void* arg)
  59842. {
  59843. const char* mn;
  59844. BIO *bio;
  59845. (void) from;
  59846. (void) to;
  59847. (void) arg;
  59848. (void) mn;
  59849. (void) bio;
  59850. if (!m) {
  59851. /* alias */
  59852. AssertNull(m);
  59853. AssertNotNull(to);
  59854. }
  59855. else {
  59856. AssertNotNull(m);
  59857. AssertNull(to);
  59858. }
  59859. AssertNotNull(from);
  59860. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  59861. mn = EVP_get_digestbyname(from);
  59862. /* print to stderr */
  59863. AssertNotNull(arg);
  59864. bio = BIO_new(BIO_s_file());
  59865. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  59866. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  59867. BIO_free(bio);
  59868. #endif
  59869. }
  59870. #endif
  59871. static int test_EVP_MD_do_all(void)
  59872. {
  59873. int res = TEST_SKIPPED;
  59874. #if defined(OPENSSL_EXTRA)
  59875. EVP_MD_do_all(NULL, stderr);
  59876. EVP_MD_do_all(list_md_fn, stderr);
  59877. res = TEST_SUCCESS;
  59878. #endif
  59879. return res;
  59880. }
  59881. #if defined(OPENSSL_EXTRA)
  59882. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  59883. {
  59884. (void)arg;
  59885. (void)nm;
  59886. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  59887. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  59888. /* print to stderr */
  59889. AssertNotNull(arg);
  59890. BIO *bio = BIO_new(BIO_s_file());
  59891. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  59892. BIO_printf(bio, "%s\n", nm);
  59893. BIO_free(bio);
  59894. #endif
  59895. }
  59896. #endif
  59897. static int test_OBJ_NAME_do_all(void)
  59898. {
  59899. int res = TEST_SKIPPED;
  59900. #if defined(OPENSSL_EXTRA)
  59901. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  59902. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  59903. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  59904. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  59905. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  59906. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  59907. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  59908. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  59909. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  59910. res = TEST_SUCCESS;
  59911. #endif
  59912. return res;
  59913. }
  59914. static int test_SSL_CIPHER_get_xxx(void)
  59915. {
  59916. EXPECT_DECLS;
  59917. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  59918. !defined(NO_FILESYSTEM)
  59919. const SSL_CIPHER* cipher = NULL;
  59920. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  59921. int i, numCiphers = 0;
  59922. SSL_CTX* ctx = NULL;
  59923. SSL* ssl = NULL;
  59924. const char* testCertFile;
  59925. const char* testKeyFile;
  59926. char buf[256] = {0};
  59927. const char* cipher_id = NULL;
  59928. int expect_nid1 = NID_undef;
  59929. int expect_nid2 = NID_undef;
  59930. int expect_nid3 = NID_undef;
  59931. int expect_nid4 = NID_undef;
  59932. int expect_nid5 = 0;
  59933. const char* cipher_id2 = NULL;
  59934. int expect_nid21 = NID_undef;
  59935. int expect_nid22 = NID_undef;
  59936. int expect_nid23 = NID_undef;
  59937. int expect_nid24 = NID_undef;
  59938. int expect_nid25 = 0;
  59939. (void)cipher;
  59940. (void)supportedCiphers;
  59941. (void)i;
  59942. (void)numCiphers;
  59943. (void)ctx;
  59944. (void)ssl;
  59945. (void)testCertFile;
  59946. (void)testKeyFile;
  59947. #if defined(WOLFSSL_TLS13)
  59948. cipher_id = "TLS13-AES128-GCM-SHA256";
  59949. expect_nid1 = NID_auth_rsa;
  59950. expect_nid2 = NID_aes_128_gcm;
  59951. expect_nid3 = NID_sha256;
  59952. expect_nid4 = NID_kx_any;
  59953. expect_nid5 = 1;
  59954. #if !defined(WOLFSSL_NO_TLS12)
  59955. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  59956. expect_nid21 = NID_auth_rsa;
  59957. expect_nid22 = NID_aes_256_gcm;
  59958. expect_nid23 = NID_sha384;
  59959. expect_nid24 = NID_kx_ecdhe;
  59960. expect_nid25 = 1;
  59961. #endif
  59962. #endif
  59963. #ifdef NO_WOLFSSL_SERVER
  59964. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  59965. #else
  59966. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  59967. #endif
  59968. if (cipher_id) {
  59969. #ifndef NO_RSA
  59970. testCertFile = svrCertFile;
  59971. testKeyFile = svrKeyFile;
  59972. #elif defined(HAVE_ECC)
  59973. testCertFile = eccCertFile;
  59974. testKeyFile = eccKeyFile;
  59975. #else
  59976. testCertFile = NULL;
  59977. testKeyFile = NULL;
  59978. #endif
  59979. if (testCertFile != NULL && testKeyFile != NULL) {
  59980. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  59981. SSL_FILETYPE_PEM));
  59982. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  59983. SSL_FILETYPE_PEM));
  59984. }
  59985. ExpectNotNull(ssl = SSL_new(ctx));
  59986. ExpectIntEQ(SSL_in_init(ssl), 1);
  59987. supportedCiphers = SSL_get_ciphers(ssl);
  59988. numCiphers = sk_num(supportedCiphers);
  59989. for (i = 0; i < numCiphers; ++i) {
  59990. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  59991. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  59992. }
  59993. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  59994. break;
  59995. }
  59996. }
  59997. /* test case for */
  59998. if (i != numCiphers) {
  59999. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  60000. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  60001. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  60002. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  60003. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  60004. }
  60005. if (cipher_id2) {
  60006. for (i = 0; i < numCiphers; ++i) {
  60007. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  60008. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  60009. }
  60010. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  60011. break;
  60012. }
  60013. }
  60014. /* test case for */
  60015. if (i != numCiphers) {
  60016. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  60017. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  60018. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  60019. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  60020. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  60021. }
  60022. }
  60023. }
  60024. SSL_CTX_free(ctx);
  60025. SSL_free(ssl);
  60026. #endif
  60027. return EXPECT_RESULT();
  60028. }
  60029. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60030. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  60031. int* keyFormat)
  60032. {
  60033. int ret;
  60034. byte* key_buf = NULL;
  60035. size_t key_sz = 0;
  60036. EncryptedInfo encInfo;
  60037. XMEMSET(&encInfo, 0, sizeof(encInfo));
  60038. ret = load_file(privKeyFile, &key_buf, &key_sz);
  60039. if (ret == 0) {
  60040. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  60041. NULL, &encInfo, keyFormat);
  60042. }
  60043. if (key_buf != NULL) {
  60044. free(key_buf); key_buf = NULL;
  60045. }
  60046. (void)encInfo; /* not used in this test */
  60047. #ifdef DEBUG_WOLFSSL
  60048. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  60049. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  60050. (*pDer)->length);
  60051. #endif
  60052. return ret;
  60053. }
  60054. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  60055. {
  60056. int ret = CRYPTOCB_UNAVAILABLE;
  60057. const char* privKeyFile = (const char*)ctx;
  60058. DerBuffer* pDer = NULL;
  60059. int keyFormat = 0;
  60060. if (info->algo_type == WC_ALGO_TYPE_PK) {
  60061. #ifdef DEBUG_WOLFSSL
  60062. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  60063. #endif
  60064. #ifndef NO_RSA
  60065. if (info->pk.type == WC_PK_TYPE_RSA) {
  60066. switch (info->pk.rsa.type) {
  60067. case RSA_PUBLIC_ENCRYPT:
  60068. case RSA_PUBLIC_DECRYPT:
  60069. /* perform software based RSA public op */
  60070. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  60071. break;
  60072. case RSA_PRIVATE_ENCRYPT:
  60073. case RSA_PRIVATE_DECRYPT:
  60074. {
  60075. RsaKey key;
  60076. /* perform software based RSA private op */
  60077. #ifdef DEBUG_WOLFSSL
  60078. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  60079. #endif
  60080. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  60081. &keyFormat);
  60082. if (ret != 0) {
  60083. return ret;
  60084. }
  60085. ret = wc_InitRsaKey(&key, HEAP_HINT);
  60086. if (ret == 0) {
  60087. word32 keyIdx = 0;
  60088. /* load RSA private key and perform private transform */
  60089. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  60090. &key, pDer->length);
  60091. if (ret == 0) {
  60092. ret = wc_RsaFunction(
  60093. info->pk.rsa.in, info->pk.rsa.inLen,
  60094. info->pk.rsa.out, info->pk.rsa.outLen,
  60095. info->pk.rsa.type, &key, info->pk.rsa.rng);
  60096. }
  60097. else {
  60098. /* if decode fails, then fall-back to software based crypto */
  60099. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  60100. "key decode failed %d, falling back to "
  60101. "software\n", ret);
  60102. ret = CRYPTOCB_UNAVAILABLE;
  60103. }
  60104. wc_FreeRsaKey(&key);
  60105. }
  60106. wc_FreeDer(&pDer); pDer = NULL;
  60107. break;
  60108. }
  60109. }
  60110. #ifdef DEBUG_WOLFSSL
  60111. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  60112. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  60113. #endif
  60114. }
  60115. #endif /* !NO_RSA */
  60116. #ifdef HAVE_ECC
  60117. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  60118. /* mark this key as ephemeral */
  60119. if (info->pk.eckg.key != NULL) {
  60120. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  60121. sizeof(info->pk.eckg.key->label));
  60122. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  60123. }
  60124. }
  60125. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  60126. ecc_key key;
  60127. /* perform software based ECC sign */
  60128. #ifdef DEBUG_WOLFSSL
  60129. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  60130. #endif
  60131. if (info->pk.eccsign.key != NULL &&
  60132. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  60133. /* this is an empheral key */
  60134. #ifdef DEBUG_WOLFSSL
  60135. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  60136. "ephemeral key\n");
  60137. #endif
  60138. return CRYPTOCB_UNAVAILABLE;
  60139. }
  60140. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  60141. if (ret != 0) {
  60142. return ret;
  60143. }
  60144. ret = wc_ecc_init(&key);
  60145. if (ret == 0) {
  60146. word32 keyIdx = 0;
  60147. /* load ECC private key and perform private transform */
  60148. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  60149. &key, pDer->length);
  60150. if (ret == 0) {
  60151. ret = wc_ecc_sign_hash(
  60152. info->pk.eccsign.in, info->pk.eccsign.inlen,
  60153. info->pk.eccsign.out, info->pk.eccsign.outlen,
  60154. info->pk.eccsign.rng, &key);
  60155. }
  60156. else {
  60157. /* if decode fails, then fall-back to software based crypto */
  60158. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  60159. "decode failed %d, falling back to software\n", ret);
  60160. ret = CRYPTOCB_UNAVAILABLE;
  60161. }
  60162. wc_ecc_free(&key);
  60163. }
  60164. wc_FreeDer(&pDer); pDer = NULL;
  60165. #ifdef DEBUG_WOLFSSL
  60166. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  60167. ret, *info->pk.eccsign.outlen);
  60168. #endif
  60169. }
  60170. #endif /* HAVE_ECC */
  60171. #ifdef HAVE_ED25519
  60172. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  60173. ed25519_key key;
  60174. /* perform software based ED25519 sign */
  60175. #ifdef DEBUG_WOLFSSL
  60176. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  60177. #endif
  60178. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  60179. if (ret != 0) {
  60180. return ret;
  60181. }
  60182. ret = wc_ed25519_init(&key);
  60183. if (ret == 0) {
  60184. word32 keyIdx = 0;
  60185. /* load ED25519 private key and perform private transform */
  60186. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  60187. &key, pDer->length);
  60188. if (ret == 0) {
  60189. /* calculate public key */
  60190. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  60191. if (ret == 0) {
  60192. key.pubKeySet = 1;
  60193. ret = wc_ed25519_sign_msg_ex(
  60194. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  60195. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  60196. &key, info->pk.ed25519sign.type,
  60197. info->pk.ed25519sign.context,
  60198. info->pk.ed25519sign.contextLen);
  60199. }
  60200. }
  60201. else {
  60202. /* if decode fails, then fall-back to software based crypto */
  60203. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  60204. "decode failed %d, falling back to software\n", ret);
  60205. ret = CRYPTOCB_UNAVAILABLE;
  60206. }
  60207. wc_ed25519_free(&key);
  60208. }
  60209. wc_FreeDer(&pDer); pDer = NULL;
  60210. #ifdef DEBUG_WOLFSSL
  60211. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  60212. ret, *info->pk.ed25519sign.outLen);
  60213. #endif
  60214. }
  60215. #endif /* HAVE_ED25519 */
  60216. }
  60217. (void)thisDevId;
  60218. (void)keyFormat;
  60219. return ret;
  60220. }
  60221. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  60222. static int test_wc_CryptoCb_TLS(int tlsVer,
  60223. const char* cliCaPemFile, const char* cliCertPemFile,
  60224. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  60225. const char* svrCaPemFile, const char* svrCertPemFile,
  60226. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  60227. {
  60228. EXPECT_DECLS;
  60229. callback_functions client_cbf;
  60230. callback_functions server_cbf;
  60231. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60232. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60233. if (tlsVer == WOLFSSL_TLSV1_3) {
  60234. #ifdef WOLFSSL_TLS13
  60235. server_cbf.method = wolfTLSv1_3_server_method;
  60236. client_cbf.method = wolfTLSv1_3_client_method;
  60237. #endif
  60238. }
  60239. else if (tlsVer == WOLFSSL_TLSV1_2) {
  60240. #ifndef WOLFSSL_NO_TLS12
  60241. server_cbf.method = wolfTLSv1_2_server_method;
  60242. client_cbf.method = wolfTLSv1_2_client_method;
  60243. #endif
  60244. }
  60245. else if (tlsVer == WOLFSSL_TLSV1_1) {
  60246. #ifndef NO_OLD_TLS
  60247. server_cbf.method = wolfTLSv1_1_server_method;
  60248. client_cbf.method = wolfTLSv1_1_client_method;
  60249. #endif
  60250. }
  60251. else if (tlsVer == WOLFSSL_TLSV1) {
  60252. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  60253. server_cbf.method = wolfTLSv1_server_method;
  60254. client_cbf.method = wolfTLSv1_client_method;
  60255. #endif
  60256. }
  60257. else if (tlsVer == WOLFSSL_SSLV3) {
  60258. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  60259. defined(WOLFSSL_STATIC_RSA)
  60260. server_cbf.method = wolfSSLv3_server_method;
  60261. client_cbf.method = wolfSSLv3_client_method;
  60262. #endif
  60263. }
  60264. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  60265. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  60266. server_cbf.method = wolfDTLSv1_2_server_method;
  60267. client_cbf.method = wolfDTLSv1_2_client_method;
  60268. #endif
  60269. }
  60270. else if (tlsVer == WOLFSSL_DTLSV1) {
  60271. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  60272. server_cbf.method = wolfDTLSv1_server_method;
  60273. client_cbf.method = wolfDTLSv1_client_method;
  60274. #endif
  60275. }
  60276. if (server_cbf.method == NULL) {
  60277. /* not enabled */
  60278. return TEST_SUCCESS;
  60279. }
  60280. /* Setup the keys for the TLS test */
  60281. client_cbf.certPemFile = cliCertPemFile;
  60282. client_cbf.keyPemFile = cliPubKeyPemFile;
  60283. client_cbf.caPemFile = cliCaPemFile;
  60284. server_cbf.certPemFile = svrCertPemFile;
  60285. server_cbf.keyPemFile = svrPubKeyPemFile;
  60286. server_cbf.caPemFile = svrCaPemFile;
  60287. /* Setup a crypto callback with pointer to private key file for testing */
  60288. client_cbf.devId = 1;
  60289. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  60290. (void*)cliPrivKeyPemFile);
  60291. server_cbf.devId = 2;
  60292. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  60293. (void*)svrPrivKeyPemFile);
  60294. /* Perform TLS server and client test */
  60295. /* First test is at WOLFSSL_CTX level */
  60296. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60297. /* Check for success */
  60298. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60299. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60300. if (EXPECT_SUCCESS()) {
  60301. /* Second test is a WOLFSSL object level */
  60302. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  60303. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60304. }
  60305. /* Check for success */
  60306. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60307. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60308. /* Un register the devId's */
  60309. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  60310. client_cbf.devId = INVALID_DEVID;
  60311. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  60312. server_cbf.devId = INVALID_DEVID;
  60313. return EXPECT_RESULT();
  60314. }
  60315. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  60316. static int test_wc_CryptoCb(void)
  60317. {
  60318. EXPECT_DECLS;
  60319. #ifdef WOLF_CRYPTO_CB
  60320. /* TODO: Add crypto callback API tests */
  60321. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60322. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  60323. int tlsVer;
  60324. #endif
  60325. #ifndef NO_RSA
  60326. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60327. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60328. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  60329. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  60330. TEST_SUCCESS);
  60331. }
  60332. #endif
  60333. #ifdef HAVE_ECC
  60334. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60335. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60336. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  60337. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  60338. TEST_SUCCESS);
  60339. }
  60340. #endif
  60341. #ifdef HAVE_ED25519
  60342. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  60343. if (tlsVer == WOLFSSL_DTLSV1) continue;
  60344. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  60345. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  60346. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  60347. TEST_SUCCESS);
  60348. }
  60349. #endif
  60350. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  60351. #endif /* WOLF_CRYPTO_CB */
  60352. return EXPECT_RESULT();
  60353. }
  60354. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  60355. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  60356. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  60357. const char* cliCaPemFile, const char* cliCertPemFile,
  60358. const char* cliPrivKeyPemFile,
  60359. const char* svrCaPemFile, const char* svrCertPemFile,
  60360. const char* svrPrivKeyPemFile,
  60361. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  60362. {
  60363. EXPECT_DECLS;
  60364. callback_functions client_cbf;
  60365. callback_functions server_cbf;
  60366. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60367. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60368. if (tlsVer == WOLFSSL_TLSV1_3) {
  60369. #ifdef WOLFSSL_TLS13
  60370. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  60371. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  60372. #endif
  60373. }
  60374. else if (tlsVer == WOLFSSL_TLSV1_2) {
  60375. #ifndef WOLFSSL_NO_TLS12
  60376. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  60377. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  60378. #endif
  60379. }
  60380. else if (tlsVer == WOLFSSL_TLSV1_1) {
  60381. #ifndef NO_OLD_TLS
  60382. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  60383. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  60384. #endif
  60385. }
  60386. else if (tlsVer == WOLFSSL_TLSV1) {
  60387. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  60388. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  60389. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  60390. #endif
  60391. }
  60392. else if (tlsVer == WOLFSSL_SSLV3) {
  60393. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  60394. defined(WOLFSSL_STATIC_RSA)
  60395. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  60396. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  60397. #endif
  60398. }
  60399. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  60400. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  60401. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  60402. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  60403. #endif
  60404. }
  60405. else if (tlsVer == WOLFSSL_DTLSV1) {
  60406. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  60407. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  60408. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  60409. #endif
  60410. }
  60411. if (server_cbf.method_ex == NULL) {
  60412. /* not enabled */
  60413. return TEST_SUCCESS;
  60414. }
  60415. /* Setup the keys for the TLS test */
  60416. client_cbf.certPemFile = cliCertPemFile;
  60417. client_cbf.keyPemFile = cliPrivKeyPemFile;
  60418. client_cbf.caPemFile = cliCaPemFile;
  60419. server_cbf.certPemFile = svrCertPemFile;
  60420. server_cbf.keyPemFile = svrPrivKeyPemFile;
  60421. server_cbf.caPemFile = svrCaPemFile;
  60422. client_cbf.mem = cliMem;
  60423. client_cbf.memSz = cliMemSz;
  60424. server_cbf.mem = svrMem;
  60425. server_cbf.memSz = svrMemSz;
  60426. client_cbf.devId = INVALID_DEVID;
  60427. server_cbf.devId = INVALID_DEVID;
  60428. /* Perform TLS server and client test */
  60429. /* First test is at WOLFSSL_CTX level */
  60430. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60431. /* Check for success */
  60432. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60433. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60434. if (EXPECT_SUCCESS()) {
  60435. /* Second test is a WOLFSSL object level */
  60436. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  60437. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  60438. }
  60439. /* Check for success */
  60440. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  60441. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  60442. return EXPECT_RESULT();
  60443. }
  60444. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  60445. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  60446. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  60447. {
  60448. EXPECT_DECLS;
  60449. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  60450. WOLFSSL_MEM_STATS mem_stats;
  60451. WOLFSSL_MEM_CONN_STATS ssl_stats;
  60452. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  60453. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  60454. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60455. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  60456. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60457. #endif
  60458. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  60459. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  60460. /* this should fail because kMaxCtxClients == 2 */
  60461. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  60462. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  60463. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  60464. wolfSSL_PrintStatsConn(&ssl_stats);
  60465. #endif
  60466. (void)ssl_stats;
  60467. }
  60468. /* display collected statistics */
  60469. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  60470. #if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_STATIC_MEMORY_LEAN)
  60471. wolfSSL_PrintStats(&mem_stats);
  60472. #endif
  60473. (void)mem_stats;
  60474. }
  60475. wolfSSL_free(ssl1);
  60476. wolfSSL_free(ssl2);
  60477. return EXPECT_RESULT();
  60478. }
  60479. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  60480. static int test_wolfSSL_CTX_StaticMemory(void)
  60481. {
  60482. EXPECT_DECLS;
  60483. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  60484. wolfSSL_method_func method_func;
  60485. WOLFSSL_CTX* ctx;
  60486. const int kMaxCtxClients = 2;
  60487. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60488. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  60489. int tlsVer;
  60490. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  60491. #endif
  60492. #endif
  60493. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  60494. #ifndef NO_WOLFSSL_SERVER
  60495. #ifndef WOLFSSL_NO_TLS12
  60496. method_func = wolfTLSv1_2_server_method_ex;
  60497. #else
  60498. method_func = wolfTLSv1_3_server_method_ex;
  60499. #endif
  60500. #else
  60501. #ifndef WOLFSSL_NO_TLS12
  60502. method_func = wolfTLSv1_2_client_method_ex;
  60503. #else
  60504. method_func = wolfTLSv1_3_client_method_ex;
  60505. #endif
  60506. #endif
  60507. /* Test creating CTX directly from static memory pool */
  60508. ctx = NULL;
  60509. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  60510. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  60511. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  60512. wolfSSL_CTX_free(ctx);
  60513. ctx = NULL;
  60514. /* Test for heap allocated CTX, then assigning static pool to it */
  60515. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  60516. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  60517. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  60518. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  60519. wolfSSL_CTX_free(ctx);
  60520. /* TLS Level Tests using static memory */
  60521. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  60522. #ifndef NO_RSA
  60523. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60524. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60525. svrCertFile, cliCertFile, cliKeyFile,
  60526. cliCertFile, svrCertFile, svrKeyFile,
  60527. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60528. TEST_SUCCESS);
  60529. }
  60530. #endif
  60531. #ifdef HAVE_ECC
  60532. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  60533. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60534. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  60535. cliEccCertFile, eccCertFile, eccKeyFile,
  60536. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60537. TEST_SUCCESS);
  60538. }
  60539. #endif
  60540. #ifdef HAVE_ED25519
  60541. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  60542. if (tlsVer == WOLFSSL_DTLSV1) continue;
  60543. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  60544. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  60545. cliEdCertFile, edCertFile, edKeyFile,
  60546. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  60547. TEST_SUCCESS);
  60548. }
  60549. #endif
  60550. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  60551. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  60552. return EXPECT_RESULT();
  60553. }
  60554. static int test_openssl_FIPS_drbg(void)
  60555. {
  60556. EXPECT_DECLS;
  60557. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  60558. DRBG_CTX* dctx = NULL;
  60559. byte data1[32], data2[32], zeroData[32];
  60560. byte testSeed[16];
  60561. size_t dlen = sizeof(data1);
  60562. int i;
  60563. XMEMSET(data1, 0, dlen);
  60564. XMEMSET(data2, 0, dlen);
  60565. XMEMSET(zeroData, 0, sizeof(zeroData));
  60566. for (i = 0; i < (int)sizeof(testSeed); i++) {
  60567. testSeed[i] = (byte)i;
  60568. }
  60569. ExpectNotNull(dctx = FIPS_get_default_drbg());
  60570. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  60571. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  60572. WOLFSSL_SUCCESS);
  60573. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  60574. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  60575. WOLFSSL_SUCCESS);
  60576. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  60577. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  60578. WOLFSSL_SUCCESS);
  60579. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  60580. WOLFSSL_SUCCESS);
  60581. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  60582. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  60583. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  60584. #ifndef HAVE_GLOBAL_RNG
  60585. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  60586. wolfSSL_FIPS_drbg_free(dctx);
  60587. #endif
  60588. #endif
  60589. return EXPECT_RESULT();
  60590. }
  60591. static int test_wolfSSL_FIPS_mode(void)
  60592. {
  60593. EXPECT_DECLS;
  60594. #if defined(OPENSSL_ALL)
  60595. #ifdef HAVE_FIPS
  60596. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  60597. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  60598. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  60599. #else
  60600. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  60601. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  60602. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  60603. #endif
  60604. #endif
  60605. return EXPECT_RESULT();
  60606. }
  60607. #ifdef WOLFSSL_DTLS
  60608. /* Prints out the current window */
  60609. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  60610. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  60611. int i;
  60612. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  60613. word32 b = w[i];
  60614. int j;
  60615. /* Prints out a 32 bit binary number in big endian order */
  60616. for (j = 0; j < 32; j++, b <<= 1) {
  60617. if (b & (((word32)1) << 31))
  60618. fprintf(stderr, "1");
  60619. else
  60620. fprintf(stderr, "0");
  60621. }
  60622. fprintf(stderr, " ");
  60623. }
  60624. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  60625. #else
  60626. (void)h;
  60627. (void)l;
  60628. (void)w;
  60629. #endif
  60630. }
  60631. /* a - cur_hi
  60632. * b - cur_lo
  60633. * c - next_hi
  60634. * d - next_lo
  60635. * e - window
  60636. * f - expected next_hi
  60637. * g - expected next_lo
  60638. * h - expected window[1]
  60639. * i - expected window[0]
  60640. */
  60641. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  60642. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  60643. DUW_TEST_print_window_binary((a), (b), (e)); \
  60644. ExpectIntEQ((c), (f)); \
  60645. ExpectIntEQ((d), (g)); \
  60646. ExpectIntEQ((e)[1], (h)); \
  60647. ExpectIntEQ((e)[0], (i)); \
  60648. } while (0)
  60649. static int test_wolfSSL_DtlsUpdateWindow(void)
  60650. {
  60651. EXPECT_DECLS;
  60652. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  60653. word32 next_lo = 0;
  60654. word16 next_hi = 0;
  60655. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  60656. fprintf(stderr, "\n");
  60657. #endif
  60658. XMEMSET(window, 0, sizeof window);
  60659. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  60660. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  60661. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  60662. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  60663. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  60664. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  60665. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  60666. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  60667. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  60668. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  60669. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  60670. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  60671. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  60672. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  60673. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  60674. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  60675. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  60676. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  60677. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  60678. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  60679. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  60680. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  60681. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  60682. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  60683. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  60684. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  60685. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  60686. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60687. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60688. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  60689. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  60690. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  60691. return EXPECT_RESULT();
  60692. }
  60693. #endif /* WOLFSSL_DTLS */
  60694. #ifdef WOLFSSL_DTLS
  60695. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  60696. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  60697. {
  60698. DtlsMsg* cur;
  60699. static byte msg[100];
  60700. static byte msgInit = 0;
  60701. if (!msgInit) {
  60702. int i;
  60703. for (i = 0; i < 100; i++)
  60704. msg[i] = i + 1;
  60705. msgInit = 1;
  60706. }
  60707. /* Sanitize test parameters */
  60708. if (len > sizeof(msg))
  60709. return -1;
  60710. if (f_offset + f_len > sizeof(msg))
  60711. return -1;
  60712. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  60713. if (ssl->dtls_rx_msg_list == NULL)
  60714. return -100;
  60715. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  60716. return -200;
  60717. if (cur->fragBucketListCount != f_count)
  60718. return -300;
  60719. if (cur->ready != ready)
  60720. return -400;
  60721. if (cur->bytesReceived != bytesReceived)
  60722. return -500;
  60723. if (ready) {
  60724. if (cur->fragBucketList != NULL)
  60725. return -600;
  60726. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  60727. return -700;
  60728. }
  60729. else {
  60730. DtlsFragBucket* fb;
  60731. if (cur->fragBucketList == NULL)
  60732. return -800;
  60733. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  60734. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  60735. return -900;
  60736. }
  60737. }
  60738. return 0;
  60739. }
  60740. static int test_wolfSSL_DTLS_fragment_buckets(void)
  60741. {
  60742. EXPECT_DECLS;
  60743. WOLFSSL ssl[1];
  60744. XMEMSET(ssl, 0, sizeof(*ssl));
  60745. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  60746. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60747. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  60748. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  60749. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  60750. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  60751. /* Test all permutations of 3 regions */
  60752. /* 1 2 3 */
  60753. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60754. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  60755. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  60756. /* 1 3 2 */
  60757. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60758. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  60759. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  60760. /* 2 1 3 */
  60761. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  60762. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  60763. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  60764. /* 2 3 1 */
  60765. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  60766. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  60767. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  60768. /* 3 1 2 */
  60769. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  60770. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  60771. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  60772. /* 3 2 1 */
  60773. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  60774. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  60775. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  60776. /* Test overlapping regions */
  60777. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  60778. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  60779. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  60780. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  60781. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  60782. /* Test overlapping multiple regions */
  60783. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60784. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  60785. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  60786. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  60787. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  60788. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  60789. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  60790. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  60791. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  60792. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  60793. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  60794. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  60795. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  60796. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  60797. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  60798. ssl->dtls_rx_msg_list = NULL;
  60799. ssl->dtls_rx_msg_list_sz = 0;
  60800. return EXPECT_RESULT();
  60801. }
  60802. #endif
  60803. #if !defined(NO_FILESYSTEM) && \
  60804. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60805. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  60806. static int test_wolfSSL_dtls_stateless2(void)
  60807. {
  60808. EXPECT_DECLS;
  60809. WOLFSSL *ssl_c = NULL;
  60810. WOLFSSL *ssl_c2 = NULL;
  60811. WOLFSSL *ssl_s = NULL;
  60812. struct test_memio_ctx test_ctx;
  60813. WOLFSSL_CTX *ctx_c = NULL;
  60814. WOLFSSL_CTX *ctx_s = NULL;
  60815. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60816. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60817. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60818. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  60819. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  60820. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  60821. /* send CH */
  60822. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  60823. (ssl_c2->error == WANT_READ));
  60824. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60825. (ssl_s->error == WANT_READ));
  60826. ExpectIntNE(test_ctx.c_len, 0);
  60827. /* consume HRR */
  60828. test_ctx.c_len = 0;
  60829. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60830. wolfSSL_free(ssl_c2);
  60831. wolfSSL_free(ssl_c);
  60832. wolfSSL_free(ssl_s);
  60833. wolfSSL_CTX_free(ctx_c);
  60834. wolfSSL_CTX_free(ctx_s);
  60835. return EXPECT_RESULT();
  60836. }
  60837. #ifdef HAVE_MAX_FRAGMENT
  60838. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  60839. {
  60840. EXPECT_DECLS;
  60841. WOLFSSL *ssl_c = NULL;
  60842. WOLFSSL *ssl_c2 = NULL;
  60843. WOLFSSL *ssl_s = NULL;
  60844. struct test_memio_ctx test_ctx;
  60845. WOLFSSL_CTX *ctx_c = NULL;
  60846. WOLFSSL_CTX *ctx_s = NULL;
  60847. word16 max_fragment = 0;
  60848. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60849. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60850. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60851. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  60852. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  60853. WOLFSSL_SUCCESS);
  60854. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  60855. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  60856. if (ssl_s != NULL) {
  60857. max_fragment = ssl_s->max_fragment;
  60858. }
  60859. /* send CH */
  60860. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  60861. (ssl_c2->error == WANT_READ));
  60862. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60863. (ssl_s->error == WANT_READ));
  60864. /* CH without cookie shouldn't change state */
  60865. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  60866. ExpectIntNE(test_ctx.c_len, 0);
  60867. /* consume HRR from buffer */
  60868. test_ctx.c_len = 0;
  60869. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60870. wolfSSL_free(ssl_c2);
  60871. wolfSSL_free(ssl_c);
  60872. wolfSSL_free(ssl_s);
  60873. wolfSSL_CTX_free(ctx_c);
  60874. wolfSSL_CTX_free(ctx_s);
  60875. return EXPECT_RESULT();
  60876. }
  60877. #endif /* HAVE_MAX_FRAGMENT */
  60878. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  60879. #define ROUNDS_WITH_HVR 4
  60880. #define ROUNDS_WITHOUT_HVR 2
  60881. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  60882. static int buf_is_hvr(const byte *data, int len)
  60883. {
  60884. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  60885. return 0;
  60886. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  60887. }
  60888. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  60889. {
  60890. EXPECT_DECLS;
  60891. struct test_memio_ctx test_ctx;
  60892. WOLFSSL_CTX *ctx_c = NULL;
  60893. WOLFSSL_CTX *ctx_s = NULL;
  60894. WOLFSSL *ssl_c = NULL;
  60895. WOLFSSL *ssl_s = NULL;
  60896. WOLFSSL_SESSION *sess = NULL;
  60897. int round_trips;
  60898. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60899. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  60900. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60901. #ifdef HAVE_SESSION_TICKET
  60902. if (useticket) {
  60903. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  60904. }
  60905. #endif
  60906. round_trips = ROUNDS_WITH_HVR;
  60907. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  60908. &round_trips), 0);
  60909. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  60910. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  60911. wolfSSL_shutdown(ssl_c);
  60912. wolfSSL_shutdown(ssl_s);
  60913. wolfSSL_free(ssl_c);
  60914. ssl_c = NULL;
  60915. wolfSSL_free(ssl_s);
  60916. ssl_s = NULL;
  60917. test_ctx.c_len = test_ctx.s_len = 0;
  60918. /* make resumption invalid */
  60919. if (bad && (sess != NULL)) {
  60920. if (useticket) {
  60921. #ifdef HAVE_SESSION_TICKET
  60922. if (sess->ticket != NULL) {
  60923. sess->ticket[0] = !sess->ticket[0];
  60924. }
  60925. #endif /* HAVE_SESSION_TICKET */
  60926. }
  60927. else {
  60928. sess->sessionID[0] = !sess->sessionID[0];
  60929. }
  60930. }
  60931. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60932. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60933. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  60934. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  60935. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  60936. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  60937. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  60938. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  60939. (ssl_c->error == WANT_READ));
  60940. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60941. (ssl_s->error == WANT_READ));
  60942. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  60943. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  60944. if (!useticket) {
  60945. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  60946. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  60947. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  60948. }
  60949. wolfSSL_SESSION_free(sess);
  60950. wolfSSL_free(ssl_c);
  60951. wolfSSL_free(ssl_s);
  60952. wolfSSL_CTX_free(ctx_c);
  60953. wolfSSL_CTX_free(ctx_s);
  60954. return EXPECT_RESULT();
  60955. }
  60956. static int test_wolfSSL_dtls_stateless_resume(void)
  60957. {
  60958. EXPECT_DECLS;
  60959. #ifdef HAVE_SESSION_TICKET
  60960. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  60961. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  60962. #endif /* HAVE_SESION_TICKET */
  60963. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  60964. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  60965. return EXPECT_RESULT();
  60966. }
  60967. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  60968. #if !defined(NO_OLD_TLS)
  60969. static int test_wolfSSL_dtls_stateless_downgrade(void)
  60970. {
  60971. EXPECT_DECLS;
  60972. WOLFSSL_CTX *ctx_c = NULL;
  60973. WOLFSSL_CTX *ctx_c2 = NULL;
  60974. WOLFSSL_CTX *ctx_s = NULL;
  60975. WOLFSSL *ssl_c = NULL;
  60976. WOLFSSL *ssl_c2 = NULL;
  60977. WOLFSSL *ssl_s = NULL;
  60978. struct test_memio_ctx test_ctx;
  60979. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60980. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60981. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  60982. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  60983. WOLFSSL_SUCCESS);
  60984. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  60985. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  60986. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  60987. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  60988. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  60989. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  60990. /* send CH */
  60991. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  60992. (ssl_c2->error == WANT_READ));
  60993. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  60994. (ssl_s->error == WANT_READ));
  60995. ExpectIntNE(test_ctx.c_len, 0);
  60996. /* consume HRR */
  60997. test_ctx.c_len = 0;
  60998. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60999. wolfSSL_free(ssl_c2);
  61000. wolfSSL_free(ssl_c);
  61001. wolfSSL_free(ssl_s);
  61002. wolfSSL_CTX_free(ctx_c);
  61003. wolfSSL_CTX_free(ctx_c2);
  61004. wolfSSL_CTX_free(ctx_s);
  61005. return EXPECT_RESULT();
  61006. }
  61007. #endif /* !defined(NO_OLD_TLS) */
  61008. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61009. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  61010. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61011. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61012. static int test_WOLFSSL_dtls_version_alert(void)
  61013. {
  61014. EXPECT_DECLS;
  61015. struct test_memio_ctx test_ctx;
  61016. WOLFSSL_CTX *ctx_c = NULL;
  61017. WOLFSSL_CTX *ctx_s = NULL;
  61018. WOLFSSL *ssl_c = NULL;
  61019. WOLFSSL *ssl_s = NULL;
  61020. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61021. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61022. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  61023. /* client hello */
  61024. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61025. (ssl_c->error == WANT_READ));
  61026. /* hrr */
  61027. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61028. (ssl_s->error == WANT_READ));
  61029. /* client hello 1 */
  61030. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61031. (ssl_c->error == WANT_READ));
  61032. /* server hello */
  61033. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61034. (ssl_s->error == WANT_READ));
  61035. /* should fail */
  61036. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  61037. (ssl_c->error == VERSION_ERROR));
  61038. /* shuould fail */
  61039. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  61040. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  61041. wolfSSL_free(ssl_c);
  61042. wolfSSL_free(ssl_s);
  61043. wolfSSL_CTX_free(ctx_c);
  61044. wolfSSL_CTX_free(ctx_s);
  61045. return EXPECT_RESULT();
  61046. }
  61047. #else
  61048. static int test_WOLFSSL_dtls_version_alert(void)
  61049. {
  61050. return TEST_SKIPPED;
  61051. }
  61052. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  61053. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  61054. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  61055. */
  61056. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  61057. && defined(WOLFSSL_TLS13) && \
  61058. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  61059. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61060. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  61061. {
  61062. struct test_memio_ctx *test_ctx;
  61063. byte buf[2048];
  61064. int idx, sz;
  61065. word32 tmp;
  61066. int ret;
  61067. idx = 5; /* space for record header */
  61068. buf[idx] = session_ticket; /* type */
  61069. idx++;
  61070. tmp = OPAQUE32_LEN +
  61071. OPAQUE32_LEN +
  61072. OPAQUE8_LEN + nonceLength +
  61073. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  61074. c32to24(tmp, buf + idx);
  61075. idx += OPAQUE24_LEN;
  61076. c32toa((word32)12345, buf+idx); /* lifetime */
  61077. idx += OPAQUE32_LEN;
  61078. c32toa((word32)12345, buf+idx); /* add */
  61079. idx += OPAQUE32_LEN;
  61080. buf[idx] = nonceLength; /* nonce length */
  61081. idx++;
  61082. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  61083. idx += nonceLength;
  61084. tmp = 1; /* ticket len */
  61085. c16toa((word16)tmp, buf+idx);
  61086. idx += 2;
  61087. buf[idx] = 0xFF; /* ticket */
  61088. idx++;
  61089. tmp = 0; /* ext len */
  61090. c16toa((word16)tmp, buf+idx);
  61091. idx += 2;
  61092. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  61093. handshake, 0, 0, 0);
  61094. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  61095. AssertNotNull(test_ctx);
  61096. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  61097. return !(ret == sz);
  61098. }
  61099. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  61100. {
  61101. int ret = 0;
  61102. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  61103. ret = -1;
  61104. }
  61105. else {
  61106. int i;
  61107. for (i = 0; i < len; i++) {
  61108. if (sess->ticketNonce.data[i] != len) {
  61109. ret = -1;
  61110. break;
  61111. }
  61112. }
  61113. }
  61114. return ret;
  61115. }
  61116. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  61117. {
  61118. EXPECT_DECLS;
  61119. char *buf[1024];
  61120. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  61121. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  61122. (ssl_c->error == WANT_READ));
  61123. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  61124. return EXPECT_RESULT();
  61125. }
  61126. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  61127. {
  61128. EXPECT_DECLS;
  61129. WOLFSSL_SESSION *sess = NULL;
  61130. WOLFSSL_SESSION *cached = NULL;
  61131. WOLFSSL_CTX *ctx = ssl_c->ctx;
  61132. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  61133. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61134. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  61135. NULL, ssl_c->options.side, 1,NULL), 0);
  61136. ExpectNotNull(cached = wolfSSL_SESSION_new());
  61137. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  61138. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  61139. wolfSSL_SESSION_free(cached);
  61140. wolfSSL_SESSION_free(sess);
  61141. return EXPECT_RESULT();
  61142. }
  61143. static int test_ticket_nonce_malloc(void)
  61144. {
  61145. EXPECT_DECLS;
  61146. struct test_memio_ctx test_ctx;
  61147. WOLFSSL_CTX *ctx_c = NULL;
  61148. WOLFSSL_CTX *ctx_s = NULL;
  61149. WOLFSSL *ssl_c = NULL;
  61150. WOLFSSL *ssl_s = NULL;
  61151. byte small;
  61152. byte medium;
  61153. byte big;
  61154. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61155. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61156. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61157. /* will send ticket manually */
  61158. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  61159. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  61160. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  61161. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  61162. (ssl_s->options.handShakeDone == 0)) {
  61163. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  61164. (ssl_c->error == WANT_READ));
  61165. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  61166. (ssl_s->error == WANT_READ));
  61167. }
  61168. small = TLS13_TICKET_NONCE_STATIC_SZ;
  61169. medium = small + 20 <= 255 ? small + 20 : 255;
  61170. big = medium + 20 <= 255 ? small + 20 : 255;
  61171. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  61172. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  61173. ssl_c->session->ticketNonce.dataStatic);
  61174. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  61175. TEST_SUCCESS);
  61176. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  61177. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  61178. TEST_SUCCESS);
  61179. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  61180. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  61181. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  61182. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  61183. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  61184. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  61185. wolfSSL_free(ssl_c);
  61186. wolfSSL_free(ssl_s);
  61187. wolfSSL_CTX_free(ctx_c);
  61188. wolfSSL_CTX_free(ctx_s);
  61189. return EXPECT_RESULT();
  61190. }
  61191. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  61192. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  61193. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  61194. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61195. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  61196. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61197. static int test_ticket_ret_create(void)
  61198. {
  61199. EXPECT_DECLS;
  61200. WOLFSSL_CTX *ctx_c = NULL;
  61201. WOLFSSL_CTX *ctx_s = NULL;
  61202. WOLFSSL *ssl_c = NULL;
  61203. WOLFSSL *ssl_s = NULL;
  61204. byte ticket[SESSION_TICKET_LEN];
  61205. struct test_memio_ctx test_ctx;
  61206. WOLFSSL_SESSION *sess = NULL;
  61207. word16 ticketLen = 0;
  61208. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61209. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61210. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61211. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  61212. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  61213. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  61214. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61215. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61216. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  61217. if (sess != NULL) {
  61218. ticketLen = sess->ticketLen;
  61219. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  61220. }
  61221. wolfSSL_free(ssl_c);
  61222. ssl_c = NULL;
  61223. wolfSSL_free(ssl_s);
  61224. ssl_s = NULL;
  61225. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  61226. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  61227. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  61228. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  61229. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  61230. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  61231. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  61232. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61233. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  61234. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  61235. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  61236. wolfSSL_SESSION_free(sess);
  61237. wolfSSL_free(ssl_c);
  61238. wolfSSL_free(ssl_s);
  61239. wolfSSL_CTX_free(ctx_c);
  61240. wolfSSL_CTX_free(ctx_s);
  61241. return EXPECT_RESULT();
  61242. }
  61243. #else
  61244. static int test_ticket_ret_create(void)
  61245. {
  61246. return TEST_SKIPPED;
  61247. }
  61248. #endif
  61249. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  61250. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  61251. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61252. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61253. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61254. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  61255. {
  61256. int ret;
  61257. WOLFSSL_SESSION* session = NULL;
  61258. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  61259. if (!wolfSSL_is_server(ssl)) {
  61260. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  61261. AssertNotNull(session);
  61262. }
  61263. do {
  61264. ret = wolfSSL_shutdown(ssl);
  61265. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  61266. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  61267. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  61268. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  61269. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  61270. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  61271. #endif
  61272. if (!wolfSSL_is_server(ssl)) {
  61273. /* client */
  61274. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61275. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61276. wolfSSL_set_session(ssl, session);
  61277. wolfSSL_SESSION_free(session);
  61278. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  61279. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  61280. }
  61281. else {
  61282. /* server */
  61283. /* Different ciphersuite so that the ticket will be invalidated based on
  61284. * the ciphersuite */
  61285. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  61286. WOLFSSL_SUCCESS);
  61287. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  61288. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  61289. }
  61290. }
  61291. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  61292. {
  61293. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  61294. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61295. WOLFSSL_SUCCESS);
  61296. }
  61297. static int test_ticket_and_psk_mixing(void)
  61298. {
  61299. EXPECT_DECLS;
  61300. /* Test mixing tickets and regular PSK */
  61301. callback_functions client_cbs, server_cbs;
  61302. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61303. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61304. client_cbs.method = wolfTLSv1_3_client_method;
  61305. server_cbs.method = wolfTLSv1_3_server_method;
  61306. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  61307. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  61308. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  61309. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  61310. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  61311. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  61312. return EXPECT_RESULT();
  61313. }
  61314. #else
  61315. static int test_ticket_and_psk_mixing(void)
  61316. {
  61317. return TEST_SKIPPED;
  61318. }
  61319. #endif
  61320. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  61321. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  61322. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61323. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  61324. static int test_prioritize_psk_cb_called = FALSE;
  61325. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  61326. const char* identity, unsigned char* key, unsigned int key_max_len,
  61327. const char** ciphersuite)
  61328. {
  61329. test_prioritize_psk_cb_called = TRUE;
  61330. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  61331. }
  61332. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  61333. {
  61334. int ret;
  61335. WOLFSSL_SESSION* session = NULL;
  61336. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  61337. if (!wolfSSL_is_server(ssl)) {
  61338. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  61339. AssertNotNull(session);
  61340. }
  61341. do {
  61342. ret = wolfSSL_shutdown(ssl);
  61343. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  61344. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  61345. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  61346. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  61347. * important. */
  61348. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61349. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61350. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  61351. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  61352. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  61353. #endif
  61354. if (!wolfSSL_is_server(ssl)) {
  61355. /* client */
  61356. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  61357. wolfSSL_set_session(ssl, session);
  61358. wolfSSL_SESSION_free(session);
  61359. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  61360. }
  61361. else {
  61362. /* server */
  61363. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  61364. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  61365. #ifdef WOLFSSL_PRIORITIZE_PSK
  61366. /* The ticket should be first tried with all ciphersuites and chosen */
  61367. AssertFalse(test_prioritize_psk_cb_called);
  61368. #else
  61369. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  61370. * callback that sets this var. */
  61371. AssertTrue(test_prioritize_psk_cb_called);
  61372. #endif
  61373. }
  61374. }
  61375. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  61376. {
  61377. if (!wolfSSL_is_server(ssl))
  61378. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61379. WOLFSSL_SUCCESS);
  61380. else
  61381. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61382. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  61383. }
  61384. static int test_prioritize_psk(void)
  61385. {
  61386. EXPECT_DECLS;
  61387. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  61388. * of the PSK's will be followed instead of the ciphersuite. */
  61389. callback_functions client_cbs, server_cbs;
  61390. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61391. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61392. client_cbs.method = wolfTLSv1_3_client_method;
  61393. server_cbs.method = wolfTLSv1_3_server_method;
  61394. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  61395. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  61396. client_cbs.on_result = test_prioritize_psk_on_result;
  61397. server_cbs.on_result = test_prioritize_psk_on_result;
  61398. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  61399. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  61400. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  61401. return EXPECT_RESULT();
  61402. }
  61403. #else
  61404. static int test_prioritize_psk(void)
  61405. {
  61406. return TEST_SKIPPED;
  61407. }
  61408. #endif
  61409. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  61410. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61411. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61412. !defined(WOLFSSL_NO_TLS12)
  61413. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  61414. {
  61415. EXPECT_DECLS;
  61416. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  61417. /* Set TLS 1.3 specific suite */
  61418. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  61419. return EXPECT_RESULT();
  61420. }
  61421. static int test_wolfSSL_CTX_set_ciphersuites(void)
  61422. {
  61423. EXPECT_DECLS;
  61424. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  61425. * do a 1.2 connection. */
  61426. test_ssl_cbf client_cbs, server_cbs;
  61427. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61428. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61429. client_cbs.method = wolfTLSv1_2_client_method;
  61430. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  61431. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  61432. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61433. &server_cbs, NULL), TEST_SUCCESS);
  61434. return EXPECT_RESULT();
  61435. }
  61436. #else
  61437. static int test_wolfSSL_CTX_set_ciphersuites(void)
  61438. {
  61439. return TEST_SKIPPED;
  61440. }
  61441. #endif
  61442. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  61443. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61444. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  61445. {
  61446. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  61447. return TEST_SUCCESS;
  61448. }
  61449. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  61450. {
  61451. EXPECT_DECLS;
  61452. WOLFSSL_ALERT_HISTORY h;
  61453. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  61454. ExpectIntEQ(h.last_rx.level, alert_fatal);
  61455. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  61456. return EXPECT_RESULT();
  61457. }
  61458. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  61459. {
  61460. EXPECT_DECLS;
  61461. test_ssl_cbf client_cbs, server_cbs;
  61462. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61463. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61464. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  61465. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  61466. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  61467. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  61468. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  61469. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61470. &server_cbs, NULL), TEST_FAIL);
  61471. return EXPECT_RESULT();
  61472. }
  61473. #else
  61474. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  61475. {
  61476. return TEST_SKIPPED;
  61477. }
  61478. #endif
  61479. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  61480. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  61481. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  61482. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61483. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61484. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  61485. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  61486. static int test_TLS_13_ticket_different_ciphers_run = 0;
  61487. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  61488. {
  61489. EXPECT_DECLS;
  61490. switch (test_TLS_13_ticket_different_ciphers_run) {
  61491. case 0:
  61492. /* First run */
  61493. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  61494. WOLFSSL_SUCCESS);
  61495. if (wolfSSL_is_server(ssl)) {
  61496. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  61497. wolfSSL_get_SSL_CTX(ssl));
  61498. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  61499. test_TLS_13_ticket_different_ciphers_ctx));
  61500. }
  61501. break;
  61502. case 1:
  61503. /* Second run */
  61504. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  61505. "TLS13-AES128-GCM-SHA256"),
  61506. WOLFSSL_SUCCESS);
  61507. if (!wolfSSL_is_server(ssl)) {
  61508. ExpectIntEQ(wolfSSL_set_session(ssl,
  61509. test_TLS_13_ticket_different_ciphers_session),
  61510. WOLFSSL_SUCCESS);
  61511. }
  61512. break;
  61513. default:
  61514. /* Bad state? */
  61515. Fail(("Should not enter here"), ("Should not enter here"));
  61516. }
  61517. return EXPECT_RESULT();
  61518. }
  61519. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  61520. {
  61521. EXPECT_DECLS;
  61522. switch (test_TLS_13_ticket_different_ciphers_run) {
  61523. case 0:
  61524. /* First run */
  61525. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  61526. wolfSSL_get1_session(ssl));
  61527. break;
  61528. case 1:
  61529. /* Second run */
  61530. ExpectTrue(wolfSSL_session_reused(ssl));
  61531. break;
  61532. default:
  61533. /* Bad state? */
  61534. Fail(("Should not enter here"), ("Should not enter here"));
  61535. }
  61536. return EXPECT_RESULT();
  61537. }
  61538. static int test_TLS_13_ticket_different_ciphers(void)
  61539. {
  61540. EXPECT_DECLS;
  61541. /* Check that we handle the connection when the ticket doesn't match
  61542. * the first ciphersuite. */
  61543. test_ssl_cbf client_cbs, server_cbs;
  61544. struct test_params {
  61545. method_provider client_meth;
  61546. method_provider server_meth;
  61547. int doUdp;
  61548. } params[] = {
  61549. #ifdef WOLFSSL_DTLS13
  61550. /* Test that the stateless code handles sessions correctly */
  61551. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  61552. #endif
  61553. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  61554. };
  61555. size_t i;
  61556. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  61557. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61558. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61559. test_TLS_13_ticket_different_ciphers_run = 0;
  61560. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  61561. client_cbs.method = params[i].client_meth;
  61562. server_cbs.method = params[i].server_meth;
  61563. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  61564. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  61565. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  61566. server_cbs.ticNoInit = 1;
  61567. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61568. &server_cbs, NULL), TEST_SUCCESS);
  61569. test_TLS_13_ticket_different_ciphers_run++;
  61570. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  61571. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  61572. &server_cbs, NULL), TEST_SUCCESS);
  61573. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  61574. test_TLS_13_ticket_different_ciphers_session = NULL;
  61575. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  61576. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  61577. }
  61578. return EXPECT_RESULT();
  61579. }
  61580. #else
  61581. static int test_TLS_13_ticket_different_ciphers(void)
  61582. {
  61583. return TEST_SKIPPED;
  61584. }
  61585. #endif
  61586. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  61587. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61588. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  61589. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  61590. byte test_extra_alerts_wrong_cs_sh[] = {
  61591. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  61592. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  61593. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  61594. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  61595. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  61596. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  61597. 0xbb, 0x07, 0x54, 0x1c,
  61598. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  61599. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  61600. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  61601. };
  61602. static int test_extra_alerts_wrong_cs(void)
  61603. {
  61604. EXPECT_DECLS;
  61605. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  61606. struct test_memio_ctx test_ctx;
  61607. WOLFSSL_CTX *ctx_c = NULL;
  61608. WOLFSSL_ALERT_HISTORY h;
  61609. WOLFSSL *ssl_c = NULL;
  61610. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61611. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61612. wolfTLSv1_2_client_method, NULL), 0);
  61613. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  61614. WOLFSSL_SUCCESS);
  61615. /* CH */
  61616. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61617. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61618. WOLFSSL_ERROR_WANT_READ);
  61619. /* consume CH */
  61620. test_ctx.s_len = 0;
  61621. /* inject SH */
  61622. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  61623. sizeof(test_extra_alerts_wrong_cs_sh));
  61624. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  61625. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61626. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61627. WOLFSSL_ERROR_WANT_READ);
  61628. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61629. ExpectIntEQ(h.last_tx.code, handshake_failure);
  61630. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61631. wolfSSL_free(ssl_c);
  61632. wolfSSL_CTX_free(ctx_c);
  61633. #endif
  61634. return EXPECT_RESULT();
  61635. }
  61636. #else
  61637. static int test_extra_alerts_wrong_cs(void)
  61638. {
  61639. return TEST_SKIPPED;
  61640. }
  61641. #endif
  61642. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  61643. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  61644. #define TEST_CS_DOWNGRADE_CLIENT "ECDHE-RSA-AES256-GCM-SHA384"
  61645. byte test_wrong_cs_downgrade_sh[] = {
  61646. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0x10,
  61647. 0x2c, 0x88, 0xd9, 0x7a, 0x23, 0xc9, 0xbd, 0x11, 0x3b, 0x64, 0x24, 0xab,
  61648. 0x5b, 0x45, 0x33, 0xf6, 0x2c, 0x34, 0xe4, 0xcf, 0xf4, 0x78, 0xc8, 0x62,
  61649. 0x06, 0xc7, 0xe5, 0x30, 0x39, 0xbf, 0xa1, 0x20, 0xa3, 0x06, 0x74, 0xc3,
  61650. 0xa9, 0x74, 0x52, 0x8a, 0xfb, 0xae, 0xf0, 0xd8, 0x6f, 0xb2, 0x9d, 0xfe,
  61651. 0x78, 0xf0, 0x3f, 0x51, 0x8f, 0x9c, 0xcf, 0xbe, 0x61, 0x43, 0x9d, 0xf8,
  61652. 0x85, 0xe5, 0x2f, 0x54,
  61653. 0xc0, 0x2f, /* ECDHE-RSA-AES128-GCM-SHA256 */
  61654. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  61655. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  61656. };
  61657. static int test_wrong_cs_downgrade(void)
  61658. {
  61659. EXPECT_DECLS;
  61660. #ifdef BUILD_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
  61661. struct test_memio_ctx test_ctx;
  61662. WOLFSSL_CTX *ctx_c = NULL;
  61663. WOLFSSL *ssl_c = NULL;
  61664. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61665. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61666. wolfSSLv23_client_method, NULL), 0);
  61667. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_CS_DOWNGRADE_CLIENT),
  61668. WOLFSSL_SUCCESS);
  61669. /* CH */
  61670. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61671. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61672. WOLFSSL_ERROR_WANT_READ);
  61673. /* consume CH */
  61674. test_ctx.s_len = 0;
  61675. /* inject SH */
  61676. XMEMCPY(test_ctx.c_buff, test_wrong_cs_downgrade_sh,
  61677. sizeof(test_wrong_cs_downgrade_sh));
  61678. test_ctx.c_len = sizeof(test_wrong_cs_downgrade_sh);
  61679. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61680. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61681. MATCH_SUITE_ERROR);
  61682. wolfSSL_free(ssl_c);
  61683. wolfSSL_CTX_free(ctx_c);
  61684. #endif
  61685. return EXPECT_RESULT();
  61686. }
  61687. #else
  61688. static int test_wrong_cs_downgrade(void)
  61689. {
  61690. return TEST_SKIPPED;
  61691. }
  61692. #endif
  61693. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  61694. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  61695. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  61696. {
  61697. tail_len -= msg_length;
  61698. XMEMMOVE(msg, msg + msg_length, tail_len);
  61699. *len = *len - msg_length;
  61700. }
  61701. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  61702. byte *found)
  61703. {
  61704. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  61705. const unsigned int _RECORD_HEADER_SZ = 5;
  61706. const int _change_cipher_hs = 55;
  61707. const int _change_cipher = 20;
  61708. const int _handshake = 22;
  61709. unsigned int tail_len;
  61710. byte *idx, *curr;
  61711. word8 currType;
  61712. word16 rLength;
  61713. word32 hLength;
  61714. idx = buf;
  61715. tail_len = (unsigned int)*len;
  61716. *found = 0;
  61717. while (tail_len > _RECORD_HEADER_SZ) {
  61718. curr = idx;
  61719. currType = *idx;
  61720. ato16(idx + 3, &rLength);
  61721. idx += _RECORD_HEADER_SZ;
  61722. tail_len -= _RECORD_HEADER_SZ;
  61723. if (tail_len < rLength)
  61724. return -1;
  61725. if (type == _change_cipher_hs && currType == _change_cipher) {
  61726. if (rLength != 1)
  61727. return -1;
  61728. /* match */
  61729. test_remove_msg(curr, *len - (int)(curr - buf),
  61730. len, _RECORD_HEADER_SZ + 1);
  61731. *found = 1;
  61732. return 0;
  61733. }
  61734. if (currType != _handshake) {
  61735. idx += rLength;
  61736. tail_len -= rLength;
  61737. continue;
  61738. }
  61739. if (rLength < _HANDSHAKE_HEADER_SZ)
  61740. return -1;
  61741. currType = *idx;
  61742. ato24(idx+1, &hLength);
  61743. hLength += _HANDSHAKE_HEADER_SZ;
  61744. if (tail_len < hLength)
  61745. return -1;
  61746. if (currType != type) {
  61747. idx += hLength;
  61748. tail_len -= hLength;
  61749. continue;
  61750. }
  61751. /* match */
  61752. test_remove_msg(curr, *len - (int)(curr - buf), len,
  61753. hLength + _RECORD_HEADER_SZ);
  61754. *found = 1;
  61755. return 0;
  61756. }
  61757. /* not found */
  61758. return 0;
  61759. }
  61760. static int test_remove_hs_message(byte hs_message_type,
  61761. int extra_round, byte alert_type)
  61762. {
  61763. EXPECT_DECLS;
  61764. WOLFSSL_CTX *ctx_c = NULL;
  61765. WOLFSSL_CTX *ctx_s = NULL;
  61766. WOLFSSL *ssl_c = NULL;
  61767. WOLFSSL *ssl_s = NULL;
  61768. struct test_memio_ctx test_ctx;
  61769. WOLFSSL_ALERT_HISTORY h;
  61770. byte found = 0;
  61771. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61772. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61773. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61774. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61775. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61776. WOLFSSL_ERROR_WANT_READ);
  61777. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61778. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61779. WOLFSSL_ERROR_WANT_READ);
  61780. if (extra_round) {
  61781. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61782. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61783. WOLFSSL_ERROR_WANT_READ);
  61784. /* this will complete handshake from server side */
  61785. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61786. }
  61787. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  61788. &test_ctx.c_len, hs_message_type, &found), 0);
  61789. if (!found) {
  61790. wolfSSL_free(ssl_c);
  61791. wolfSSL_CTX_free(ctx_c);
  61792. wolfSSL_free(ssl_s);
  61793. wolfSSL_CTX_free(ctx_s);
  61794. return TEST_SKIPPED;
  61795. }
  61796. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61797. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61798. WOLFSSL_ERROR_WANT_READ);
  61799. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61800. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  61801. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61802. wolfSSL_free(ssl_c);
  61803. wolfSSL_CTX_free(ctx_c);
  61804. wolfSSL_free(ssl_s);
  61805. wolfSSL_CTX_free(ctx_s);
  61806. return EXPECT_RESULT();
  61807. }
  61808. static int test_extra_alerts_skip_hs(void)
  61809. {
  61810. EXPECT_DECLS;
  61811. const byte _server_key_exchange = 12;
  61812. const byte _server_hello = 2;
  61813. const byte _certificate = 11;
  61814. /* server_hello */
  61815. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  61816. unexpected_message), TEST_FAIL);
  61817. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  61818. 0xff), TEST_FAIL);
  61819. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  61820. unexpected_message), TEST_FAIL);
  61821. return EXPECT_RESULT();
  61822. }
  61823. #else
  61824. static int test_extra_alerts_skip_hs(void)
  61825. {
  61826. return TEST_SKIPPED;
  61827. }
  61828. #endif
  61829. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61830. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  61831. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  61832. unsigned char* key, unsigned int key_max_len)
  61833. {
  61834. (void)ssl;
  61835. (void)id;
  61836. (void)key_max_len;
  61837. /* zero means error */
  61838. key[0] = 0x10;
  61839. return 1;
  61840. }
  61841. static int test_extra_alerts_bad_psk(void)
  61842. {
  61843. EXPECT_DECLS;
  61844. WOLFSSL_CTX *ctx_c = NULL;
  61845. WOLFSSL_CTX *ctx_s = NULL;
  61846. WOLFSSL *ssl_c = NULL;
  61847. WOLFSSL *ssl_s = NULL;
  61848. struct test_memio_ctx test_ctx;
  61849. WOLFSSL_ALERT_HISTORY h;
  61850. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61851. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61852. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61853. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  61854. WOLFSSL_SUCCESS);
  61855. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  61856. WOLFSSL_SUCCESS);
  61857. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  61858. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61859. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61860. WOLFSSL_ERROR_WANT_READ);
  61861. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61862. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61863. WOLFSSL_ERROR_WANT_READ);
  61864. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61865. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61866. WOLFSSL_ERROR_WANT_READ);
  61867. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61868. ExpectIntEQ(h.last_tx.code, handshake_failure);
  61869. ExpectIntEQ(h.last_tx.level, alert_fatal);
  61870. wolfSSL_free(ssl_c);
  61871. wolfSSL_CTX_free(ctx_c);
  61872. wolfSSL_free(ssl_s);
  61873. wolfSSL_CTX_free(ctx_s);
  61874. return EXPECT_RESULT();
  61875. }
  61876. #else
  61877. static int test_extra_alerts_bad_psk(void)
  61878. {
  61879. return TEST_SKIPPED;
  61880. }
  61881. #endif
  61882. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61883. && !defined(NO_PSK)
  61884. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  61885. const char* hint, char* identity, unsigned int id_max_len,
  61886. unsigned char* key, unsigned int key_max_len)
  61887. {
  61888. (void)ssl;
  61889. (void)hint;
  61890. (void)key_max_len;
  61891. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  61892. XSTRNCPY(identity, "Client_identity", id_max_len);
  61893. key[0] = 0x20;
  61894. return 1;
  61895. }
  61896. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  61897. const char* id, unsigned char* key, unsigned int key_max_len)
  61898. {
  61899. (void)ssl;
  61900. (void)id;
  61901. (void)key_max_len;
  61902. /* zero means error */
  61903. key[0] = 0x10;
  61904. return 1;
  61905. }
  61906. #endif
  61907. static int test_tls13_bad_psk_binder(void)
  61908. {
  61909. EXPECT_DECLS;
  61910. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  61911. && !defined(NO_PSK)
  61912. WOLFSSL_CTX *ctx_c = NULL;
  61913. WOLFSSL_CTX *ctx_s = NULL;
  61914. WOLFSSL *ssl_c = NULL;
  61915. WOLFSSL *ssl_s = NULL;
  61916. struct test_memio_ctx test_ctx;
  61917. WOLFSSL_ALERT_HISTORY h;
  61918. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61919. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61920. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61921. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  61922. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  61923. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61924. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61925. WOLFSSL_ERROR_WANT_READ);
  61926. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61927. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61928. BAD_BINDER);
  61929. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61930. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61931. FATAL_ERROR);
  61932. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  61933. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  61934. ExpectIntEQ(h.last_rx.level, alert_fatal);
  61935. wolfSSL_free(ssl_c);
  61936. wolfSSL_CTX_free(ctx_c);
  61937. wolfSSL_free(ssl_s);
  61938. wolfSSL_CTX_free(ctx_s);
  61939. #endif
  61940. return EXPECT_RESULT();
  61941. }
  61942. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  61943. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61944. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  61945. int sz, void *ctx)
  61946. {
  61947. static int sentServerHello = FALSE;
  61948. if (!sentServerHello) {
  61949. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  61950. size_t i;
  61951. if (sz < (int)sizeof(renegExt))
  61952. return WOLFSSL_CBIO_ERR_GENERAL;
  61953. /* Remove SCR from ServerHello */
  61954. for (i = 0; i < sz - sizeof(renegExt); i++) {
  61955. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  61956. /* Found the extension. Change it to something unrecognized. */
  61957. buf[i+1] = 0x11;
  61958. break;
  61959. }
  61960. }
  61961. sentServerHello = TRUE;
  61962. }
  61963. return EmbedSend(ssl, buf, sz, ctx);
  61964. }
  61965. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  61966. {
  61967. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  61968. }
  61969. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  61970. {
  61971. WOLFSSL_ALERT_HISTORY h;
  61972. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  61973. AssertIntEQ(h.last_rx.code, handshake_failure);
  61974. AssertIntEQ(h.last_rx.level, alert_fatal);
  61975. }
  61976. static int test_harden_no_secure_renegotiation(void)
  61977. {
  61978. EXPECT_DECLS;
  61979. callback_functions client_cbs, server_cbs;
  61980. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  61981. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  61982. client_cbs.method = wolfTLSv1_2_client_method;
  61983. server_cbs.method = wolfTLSv1_2_server_method;
  61984. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  61985. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  61986. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  61987. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  61988. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  61989. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  61990. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  61991. server_cbs.last_err == FATAL_ERROR);
  61992. return EXPECT_RESULT();
  61993. }
  61994. #else
  61995. static int test_harden_no_secure_renegotiation(void)
  61996. {
  61997. return TEST_SKIPPED;
  61998. }
  61999. #endif
  62000. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62001. static int test_override_alt_cert_chain_cert_cb(int preverify,
  62002. WOLFSSL_X509_STORE_CTX* store)
  62003. {
  62004. fprintf(stderr, "preverify: %d\n", preverify);
  62005. fprintf(stderr, "store->error: %d\n", store->error);
  62006. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  62007. if (store->error == OCSP_INVALID_STATUS) {
  62008. fprintf(stderr, "Overriding OCSP error\n");
  62009. return 1;
  62010. }
  62011. #ifndef WOLFSSL_ALT_CERT_CHAINS
  62012. else if ((store->error == ASN_NO_SIGNER_E ||
  62013. store->error == ASN_SELF_SIGNED_E
  62014. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  62015. defined(HAVE_WEBSERVER)
  62016. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  62017. #endif
  62018. ) && store->error_depth == store->totalCerts - 1) {
  62019. fprintf(stderr, "Overriding no signer error only for root cert\n");
  62020. return 1;
  62021. }
  62022. #endif
  62023. else
  62024. return preverify;
  62025. }
  62026. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  62027. int urlSz, unsigned char* request, int requestSz,
  62028. unsigned char** response)
  62029. {
  62030. (void)ioCtx;
  62031. (void)url;
  62032. (void)urlSz;
  62033. (void)request;
  62034. (void)requestSz;
  62035. (void)response;
  62036. return -1;
  62037. }
  62038. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  62039. {
  62040. EXPECT_DECLS;
  62041. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  62042. test_override_alt_cert_chain_cert_cb);
  62043. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  62044. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  62045. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  62046. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  62047. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  62048. WOLFSSL_SUCCESS);
  62049. return EXPECT_RESULT();
  62050. }
  62051. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  62052. {
  62053. EXPECT_DECLS;
  62054. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  62055. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  62056. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  62057. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  62058. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  62059. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  62060. WOLFSSL_SUCCESS);
  62061. return EXPECT_RESULT();
  62062. }
  62063. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  62064. {
  62065. EXPECT_DECLS;
  62066. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  62067. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  62068. return EXPECT_RESULT();
  62069. }
  62070. static int test_override_alt_cert_chain(void)
  62071. {
  62072. EXPECT_DECLS;
  62073. size_t i;
  62074. struct test_params {
  62075. ctx_cb client_ctx_cb;
  62076. ctx_cb server_ctx_cb;
  62077. int result;
  62078. } params[] = {
  62079. {test_override_alt_cert_chain_client_ctx_ready,
  62080. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  62081. {test_override_alt_cert_chain_client_ctx_ready2,
  62082. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  62083. };
  62084. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  62085. test_ssl_cbf client_cbs, server_cbs;
  62086. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  62087. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  62088. fprintf(stderr, "test config: %d\n", (int)i);
  62089. client_cbs.ctx_ready = params[i].client_ctx_cb;
  62090. server_cbs.ctx_ready = params[i].server_ctx_cb;
  62091. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  62092. &server_cbs, NULL), params[i].result);
  62093. ExpectIntEQ(client_cbs.return_code, params[i].result);
  62094. ExpectIntEQ(server_cbs.return_code, params[i].result);
  62095. }
  62096. return EXPECT_RESULT();
  62097. }
  62098. #else
  62099. static int test_override_alt_cert_chain(void)
  62100. {
  62101. return TEST_SKIPPED;
  62102. }
  62103. #endif
  62104. #if defined(HAVE_RPK)
  62105. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  62106. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  62107. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  62108. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  62109. {
  62110. int ret = WOLFSSL_SUCCESS;
  62111. (void)mode;
  62112. (void)strctx;
  62113. WOLFSSL_ENTER("MyRpkVerifyCb");
  62114. return ret;
  62115. }
  62116. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  62117. static WC_INLINE int test_rpk_memio_setup(
  62118. struct test_memio_ctx *ctx,
  62119. WOLFSSL_CTX **ctx_c,
  62120. WOLFSSL_CTX **ctx_s,
  62121. WOLFSSL **ssl_c,
  62122. WOLFSSL **ssl_s,
  62123. method_provider method_c,
  62124. method_provider method_s,
  62125. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  62126. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  62127. const char* pkey_c, int fmt_kc, /* client private key and format */
  62128. const char* pkey_s, int fmt_ks /* server private key and format */
  62129. )
  62130. {
  62131. int ret;
  62132. if (ctx_c != NULL && *ctx_c == NULL) {
  62133. *ctx_c = wolfSSL_CTX_new(method_c());
  62134. if (*ctx_c == NULL) {
  62135. return -1;
  62136. }
  62137. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  62138. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  62139. if (ret != WOLFSSL_SUCCESS) {
  62140. return -1;
  62141. }
  62142. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  62143. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  62144. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  62145. if (ret != WOLFSSL_SUCCESS) {
  62146. return -1;
  62147. }
  62148. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  62149. if (ret != WOLFSSL_SUCCESS) {
  62150. return -1;
  62151. }
  62152. }
  62153. if (ctx_s != NULL && *ctx_s == NULL) {
  62154. *ctx_s = wolfSSL_CTX_new(method_s());
  62155. if (*ctx_s == NULL) {
  62156. return -1;
  62157. }
  62158. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  62159. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  62160. if (ret != WOLFSSL_SUCCESS) {
  62161. return -1;
  62162. }
  62163. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  62164. if (ret != WOLFSSL_SUCCESS) {
  62165. return -1;
  62166. }
  62167. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  62168. if (ret != WOLFSSL_SUCCESS) {
  62169. return -1;
  62170. }
  62171. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  62172. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  62173. if (ctx->s_ciphers != NULL) {
  62174. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  62175. if (ret != WOLFSSL_SUCCESS) {
  62176. return -1;
  62177. }
  62178. }
  62179. }
  62180. if (ctx_c != NULL && ssl_c != NULL) {
  62181. *ssl_c = wolfSSL_new(*ctx_c);
  62182. if (*ssl_c == NULL) {
  62183. return -1;
  62184. }
  62185. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  62186. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  62187. }
  62188. if (ctx_s != NULL && ssl_s != NULL) {
  62189. *ssl_s = wolfSSL_new(*ctx_s);
  62190. if (*ssl_s == NULL) {
  62191. return -1;
  62192. }
  62193. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  62194. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  62195. #if !defined(NO_DH)
  62196. SetDH(*ssl_s);
  62197. #endif
  62198. }
  62199. return 0;
  62200. }
  62201. #endif /* HAVE_RPK */
  62202. static int test_rpk_set_xxx_cert_type(void)
  62203. {
  62204. EXPECT_DECLS;
  62205. #if defined(HAVE_RPK)
  62206. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  62207. WOLFSSL_CTX* ctx = NULL;
  62208. WOLFSSL* ssl = NULL;
  62209. int tp;
  62210. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  62211. ExpectNotNull(ctx);
  62212. ssl = wolfSSL_new(ctx);
  62213. ExpectNotNull(ssl);
  62214. /*--------------------------------------------*/
  62215. /* tests for wolfSSL_CTX_set_client_cert_type */
  62216. /*--------------------------------------------*/
  62217. /* illegal parameter test caces */
  62218. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  62219. MAX_CLIENT_CERT_TYPE_CNT),
  62220. BAD_FUNC_ARG);
  62221. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62222. sizeof(ctype)),
  62223. BAD_FUNC_ARG);
  62224. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62225. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62226. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62227. MAX_CLIENT_CERT_TYPE_CNT),
  62228. BAD_FUNC_ARG);
  62229. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62230. ctype[1] = 10; /* set unknown cert type */
  62231. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62232. MAX_CLIENT_CERT_TYPE_CNT),
  62233. BAD_FUNC_ARG);
  62234. /* pass larger type count */
  62235. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62236. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62237. ctype[2] = 1; /* pass unacceptable type count */
  62238. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62239. MAX_CLIENT_CERT_TYPE_CNT + 1),
  62240. BAD_FUNC_ARG);
  62241. /* should accept NULL for type buffer */
  62242. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  62243. MAX_CLIENT_CERT_TYPE_CNT),
  62244. WOLFSSL_SUCCESS);
  62245. /* should accept zero for type count */
  62246. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62247. 0),
  62248. WOLFSSL_SUCCESS);
  62249. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  62250. MAX_CLIENT_CERT_TYPE_CNT),
  62251. WOLFSSL_SUCCESS);
  62252. /*--------------------------------------------*/
  62253. /* tests for wolfSSL_CTX_set_server_cert_type */
  62254. /*--------------------------------------------*/
  62255. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  62256. MAX_SERVER_CERT_TYPE_CNT),
  62257. BAD_FUNC_ARG);
  62258. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62259. sizeof(ctype)),
  62260. BAD_FUNC_ARG);
  62261. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62262. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62263. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62264. MAX_SERVER_CERT_TYPE_CNT),
  62265. BAD_FUNC_ARG);
  62266. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62267. ctype[1] = 10; /* set unknown cert type */
  62268. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62269. MAX_SERVER_CERT_TYPE_CNT),
  62270. BAD_FUNC_ARG);
  62271. /* pass larger type count */
  62272. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62273. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62274. ctype[2] = 1; /* pass unacceptable type count */
  62275. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62276. MAX_SERVER_CERT_TYPE_CNT + 1),
  62277. BAD_FUNC_ARG);
  62278. /* should accept NULL for type buffer */
  62279. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  62280. MAX_SERVER_CERT_TYPE_CNT),
  62281. WOLFSSL_SUCCESS);
  62282. /* should accept zero for type count */
  62283. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62284. 0),
  62285. WOLFSSL_SUCCESS);
  62286. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  62287. MAX_CLIENT_CERT_TYPE_CNT),
  62288. WOLFSSL_SUCCESS);
  62289. /*--------------------------------------------*/
  62290. /* tests for wolfSSL_set_client_cert_type */
  62291. /*--------------------------------------------*/
  62292. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  62293. MAX_CLIENT_CERT_TYPE_CNT),
  62294. BAD_FUNC_ARG);
  62295. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62296. sizeof(ctype)),
  62297. BAD_FUNC_ARG);
  62298. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62299. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62300. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62301. MAX_CLIENT_CERT_TYPE_CNT),
  62302. BAD_FUNC_ARG);
  62303. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62304. ctype[1] = 10; /* set unknown cert type */
  62305. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62306. MAX_CLIENT_CERT_TYPE_CNT),
  62307. BAD_FUNC_ARG);
  62308. /* pass larger type count */
  62309. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62310. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62311. ctype[2] = 1; /* pass unacceptable type count */
  62312. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62313. MAX_CLIENT_CERT_TYPE_CNT + 1),
  62314. BAD_FUNC_ARG);
  62315. /* should accept NULL for type buffer */
  62316. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  62317. MAX_CLIENT_CERT_TYPE_CNT),
  62318. WOLFSSL_SUCCESS);
  62319. /* should accept zero for type count */
  62320. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62321. 0),
  62322. WOLFSSL_SUCCESS);
  62323. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  62324. MAX_CLIENT_CERT_TYPE_CNT),
  62325. WOLFSSL_SUCCESS);
  62326. /*--------------------------------------------*/
  62327. /* tests for wolfSSL_CTX_set_server_cert_type */
  62328. /*--------------------------------------------*/
  62329. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  62330. MAX_SERVER_CERT_TYPE_CNT),
  62331. BAD_FUNC_ARG);
  62332. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62333. sizeof(ctype)),
  62334. BAD_FUNC_ARG);
  62335. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  62336. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  62337. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62338. MAX_SERVER_CERT_TYPE_CNT),
  62339. BAD_FUNC_ARG);
  62340. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  62341. ctype[1] = 10; /* set unknown cert type */
  62342. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62343. MAX_SERVER_CERT_TYPE_CNT),
  62344. BAD_FUNC_ARG);
  62345. /* pass larger type count */
  62346. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  62347. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  62348. ctype[2] = 1; /* pass unacceptable type count */
  62349. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62350. MAX_SERVER_CERT_TYPE_CNT + 1),
  62351. BAD_FUNC_ARG);
  62352. /* should accept NULL for type buffer */
  62353. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  62354. MAX_SERVER_CERT_TYPE_CNT),
  62355. WOLFSSL_SUCCESS);
  62356. /* should accept zero for type count */
  62357. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62358. 0),
  62359. WOLFSSL_SUCCESS);
  62360. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  62361. MAX_SERVER_CERT_TYPE_CNT),
  62362. WOLFSSL_SUCCESS);
  62363. /*------------------------------------------------*/
  62364. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  62365. /*------------------------------------------------*/
  62366. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  62367. BAD_FUNC_ARG);
  62368. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  62369. BAD_FUNC_ARG);
  62370. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  62371. BAD_FUNC_ARG);
  62372. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  62373. BAD_FUNC_ARG);
  62374. /* clean up */
  62375. wolfSSL_free(ssl);
  62376. wolfSSL_CTX_free(ctx);
  62377. #endif
  62378. return EXPECT_RESULT();
  62379. }
  62380. static int test_tls13_rpk_handshake(void)
  62381. {
  62382. EXPECT_DECLS;
  62383. #if defined(HAVE_RPK)
  62384. int ret = 0;
  62385. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62386. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62387. struct test_memio_ctx test_ctx;
  62388. int err;
  62389. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  62390. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  62391. int typeCnt_c;
  62392. int typeCnt_s;
  62393. int tp = 0;
  62394. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  62395. int isServer;
  62396. #endif
  62397. (void)err;
  62398. (void)typeCnt_c;
  62399. (void)typeCnt_s;
  62400. (void)certType_c;
  62401. (void)certType_s;
  62402. /* TLS1.2
  62403. * Both client and server load x509 cert and start handshaking.
  62404. * Check no negotiation occurred.
  62405. */
  62406. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62407. ExpectIntEQ(
  62408. test_rpk_memio_setup(
  62409. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62410. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62411. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62412. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62413. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62414. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  62415. , 0);
  62416. /* set client certificate type in client end */
  62417. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62418. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62419. typeCnt_c = 2;
  62420. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62421. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62422. typeCnt_s = 2;
  62423. /* both client and server do not call client/server_cert_type APIs,
  62424. * expecting default settings works and no negotiation performed.
  62425. */
  62426. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62427. return TEST_FAIL;
  62428. /* confirm no negotiation occurred */
  62429. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62430. WOLFSSL_SUCCESS);
  62431. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62432. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62433. WOLFSSL_SUCCESS);
  62434. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62435. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62436. WOLFSSL_SUCCESS);
  62437. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62438. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62439. WOLFSSL_SUCCESS);
  62440. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62441. (void)typeCnt_c;
  62442. (void)typeCnt_s;
  62443. wolfSSL_free(ssl_c);
  62444. wolfSSL_CTX_free(ctx_c);
  62445. wolfSSL_free(ssl_s);
  62446. wolfSSL_CTX_free(ctx_s);
  62447. ssl_c = ssl_s = NULL;
  62448. ctx_c = ctx_s = NULL;
  62449. /* Both client and server load x509 cert and start handshaking.
  62450. * Check no negotiation occurred.
  62451. */
  62452. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62453. ExpectIntEQ(
  62454. test_rpk_memio_setup(
  62455. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62456. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62457. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62458. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62459. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62460. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62461. , 0);
  62462. /* set client certificate type in client end */
  62463. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62464. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62465. typeCnt_c = 2;
  62466. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62467. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62468. typeCnt_s = 2;
  62469. /* both client and server do not call client/server_cert_type APIs,
  62470. * expecting default settings works and no negotiation performed.
  62471. */
  62472. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62473. return TEST_FAIL;
  62474. /* confirm no negotiation occurred */
  62475. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62476. WOLFSSL_SUCCESS);
  62477. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62478. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62479. WOLFSSL_SUCCESS);
  62480. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62481. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62482. WOLFSSL_SUCCESS);
  62483. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62484. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62485. WOLFSSL_SUCCESS);
  62486. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62487. (void)typeCnt_c;
  62488. (void)typeCnt_s;
  62489. wolfSSL_free(ssl_c);
  62490. wolfSSL_CTX_free(ctx_c);
  62491. wolfSSL_free(ssl_s);
  62492. wolfSSL_CTX_free(ctx_s);
  62493. ssl_c = ssl_s = NULL;
  62494. ctx_c = ctx_s = NULL;
  62495. /* Both client and server load RPK cert and start handshaking.
  62496. * Confirm negotiated cert types match as expected.
  62497. */
  62498. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62499. ExpectIntEQ(
  62500. test_rpk_memio_setup(
  62501. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62502. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62503. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62504. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62505. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62506. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62507. , 0);
  62508. /* set client certificate type in client end */
  62509. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62510. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62511. typeCnt_c = 2;
  62512. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62513. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62514. typeCnt_s = 2;
  62515. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62516. WOLFSSL_SUCCESS);
  62517. /* set server certificate type in client end */
  62518. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62519. WOLFSSL_SUCCESS);
  62520. /* set client certificate type in server end */
  62521. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62522. WOLFSSL_SUCCESS);
  62523. /* set server certificate type in server end */
  62524. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62525. WOLFSSL_SUCCESS);
  62526. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62527. return TEST_FAIL;
  62528. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62529. WOLFSSL_SUCCESS);
  62530. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62531. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62532. WOLFSSL_SUCCESS);
  62533. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62534. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62535. WOLFSSL_SUCCESS);
  62536. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62537. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62538. WOLFSSL_SUCCESS);
  62539. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62540. wolfSSL_free(ssl_c);
  62541. wolfSSL_CTX_free(ctx_c);
  62542. wolfSSL_free(ssl_s);
  62543. wolfSSL_CTX_free(ctx_s);
  62544. ssl_c = ssl_s = NULL;
  62545. ctx_c = ctx_s = NULL;
  62546. /* TLS1.2
  62547. * Both client and server load RPK cert and start handshaking.
  62548. * Confirm negotiated cert types match as expected.
  62549. */
  62550. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62551. ExpectIntEQ(
  62552. test_rpk_memio_setup(
  62553. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62554. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62555. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62556. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62557. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62558. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62559. , 0);
  62560. /* set client certificate type in client end */
  62561. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62562. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62563. typeCnt_c = 2;
  62564. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62565. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62566. typeCnt_s = 2;
  62567. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62568. WOLFSSL_SUCCESS);
  62569. /* set server certificate type in client end */
  62570. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62571. WOLFSSL_SUCCESS);
  62572. /* set client certificate type in server end */
  62573. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62574. WOLFSSL_SUCCESS);
  62575. /* set server certificate type in server end */
  62576. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62577. WOLFSSL_SUCCESS);
  62578. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62579. return TEST_FAIL;
  62580. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62581. WOLFSSL_SUCCESS);
  62582. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62583. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62584. WOLFSSL_SUCCESS);
  62585. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62586. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62587. WOLFSSL_SUCCESS);
  62588. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62589. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62590. WOLFSSL_SUCCESS);
  62591. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62592. wolfSSL_free(ssl_c);
  62593. wolfSSL_CTX_free(ctx_c);
  62594. wolfSSL_free(ssl_s);
  62595. wolfSSL_CTX_free(ctx_s);
  62596. ssl_c = ssl_s = NULL;
  62597. ctx_c = ctx_s = NULL;
  62598. /* Both client and server load x509 cert.
  62599. * Have client call set_client_cert_type with both RPK and x509.
  62600. * This doesn't makes client add client cert type extension to ClientHello,
  62601. * since it does not load RPK cert actually.
  62602. */
  62603. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62604. ExpectIntEQ(
  62605. test_rpk_memio_setup(
  62606. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62607. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62608. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62609. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62610. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62611. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62612. , 0);
  62613. /* set client certificate type in client end
  62614. *
  62615. * client indicates both RPK and x509 certs are available but loaded RPK
  62616. * cert only. It does not have client add client-cert-type extension in CH.
  62617. */
  62618. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62619. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62620. typeCnt_c = 2;
  62621. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62622. WOLFSSL_SUCCESS);
  62623. /* client indicates both RPK and x509 certs are acceptable */
  62624. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62625. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62626. typeCnt_s = 2;
  62627. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62628. WOLFSSL_SUCCESS);
  62629. /* server indicates both RPK and x509 certs are acceptable */
  62630. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62631. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62632. typeCnt_c = 2;
  62633. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62634. WOLFSSL_SUCCESS);
  62635. /* server should indicate only RPK cert is available */
  62636. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62637. certType_s[1] = -1;
  62638. typeCnt_s = 1;
  62639. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62640. WOLFSSL_SUCCESS);
  62641. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62642. return TEST_FAIL;
  62643. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  62644. * be returned as cert type.
  62645. */
  62646. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62647. WOLFSSL_SUCCESS);
  62648. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62649. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62650. WOLFSSL_SUCCESS);
  62651. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62652. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62653. WOLFSSL_SUCCESS);
  62654. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62655. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62656. WOLFSSL_SUCCESS);
  62657. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62658. wolfSSL_free(ssl_c);
  62659. wolfSSL_CTX_free(ctx_c);
  62660. wolfSSL_free(ssl_s);
  62661. wolfSSL_CTX_free(ctx_s);
  62662. ssl_c = ssl_s = NULL;
  62663. ctx_c = ctx_s = NULL;
  62664. /* Have client load RPK cert and have server load x509 cert.
  62665. * Check the negotiation result from both ends.
  62666. */
  62667. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62668. ExpectIntEQ(
  62669. test_rpk_memio_setup(
  62670. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62671. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62672. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62673. svrCertFile, WOLFSSL_FILETYPE_PEM,
  62674. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62675. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62676. , 0);
  62677. /* have client tell to use RPK cert */
  62678. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62679. certType_c[1] = -1;
  62680. typeCnt_c = 1;
  62681. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62682. WOLFSSL_SUCCESS);
  62683. /* have client tell to accept both RPK and x509 cert */
  62684. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62685. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62686. typeCnt_s = 2;
  62687. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62688. WOLFSSL_SUCCESS);
  62689. /* have server accept to both RPK and x509 cert */
  62690. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62691. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62692. typeCnt_c = 2;
  62693. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62694. WOLFSSL_SUCCESS);
  62695. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  62696. * end, expecting the default setting works.
  62697. */
  62698. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  62699. return TEST_FAIL;
  62700. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62701. WOLFSSL_SUCCESS);
  62702. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62703. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62704. WOLFSSL_SUCCESS);
  62705. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62706. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62707. WOLFSSL_SUCCESS);
  62708. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62709. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62710. WOLFSSL_SUCCESS);
  62711. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62712. wolfSSL_free(ssl_c);
  62713. wolfSSL_CTX_free(ctx_c);
  62714. wolfSSL_free(ssl_s);
  62715. wolfSSL_CTX_free(ctx_s);
  62716. ssl_c = ssl_s = NULL;
  62717. ctx_c = ctx_s = NULL;
  62718. /* Have both client and server load RPK cert, however, have server
  62719. * indicate its cert type x509.
  62720. * Client is expected to detect the cert type mismatch then to send alert
  62721. * with "unsupported_certificate".
  62722. */
  62723. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62724. ExpectIntEQ(
  62725. test_rpk_memio_setup(
  62726. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62727. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62728. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62729. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  62730. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62731. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62732. , 0);
  62733. /* have client tell to use RPK cert */
  62734. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62735. certType_c[1] = -1;
  62736. typeCnt_c = 1;
  62737. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62738. WOLFSSL_SUCCESS);
  62739. /* have client tell to accept both RPK and x509 cert */
  62740. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62741. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62742. typeCnt_s = 2;
  62743. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62744. WOLFSSL_SUCCESS);
  62745. /* have server accept to both RPK and x509 cert */
  62746. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62747. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62748. typeCnt_c = 2;
  62749. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62750. WOLFSSL_SUCCESS);
  62751. /* have server tell to use x509 cert intentionally. This will bring
  62752. * certificate type mismatch in client side.
  62753. */
  62754. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62755. certType_s[1] = -1;
  62756. typeCnt_s = 1;
  62757. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62758. WOLFSSL_SUCCESS);
  62759. /* expect client detect cert type mismatch then send Alert */
  62760. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62761. if (ret != -1)
  62762. return TEST_FAIL;
  62763. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  62764. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62765. WOLFSSL_SUCCESS);
  62766. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62767. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62768. WOLFSSL_SUCCESS);
  62769. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62770. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62771. WOLFSSL_SUCCESS);
  62772. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62773. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62774. WOLFSSL_SUCCESS);
  62775. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62776. wolfSSL_free(ssl_c);
  62777. wolfSSL_CTX_free(ctx_c);
  62778. wolfSSL_free(ssl_s);
  62779. wolfSSL_CTX_free(ctx_s);
  62780. ssl_c = ssl_s = NULL;
  62781. ctx_c = ctx_s = NULL;
  62782. /* Have client load x509 cert and server load RPK cert,
  62783. * however, have client indicate its cert type RPK.
  62784. * Server is expected to detect the cert type mismatch then to send alert
  62785. * with "unsupported_certificate".
  62786. */
  62787. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62788. ExpectIntEQ(
  62789. test_rpk_memio_setup(
  62790. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62791. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62792. cliCertFile, WOLFSSL_FILETYPE_PEM,
  62793. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62794. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62795. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62796. , 0);
  62797. /* have client tell to use RPK cert intentionally */
  62798. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62799. certType_c[1] = -1;
  62800. typeCnt_c = 1;
  62801. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62802. WOLFSSL_SUCCESS);
  62803. /* have client tell to accept both RPK and x509 cert */
  62804. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62805. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  62806. typeCnt_s = 2;
  62807. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62808. WOLFSSL_SUCCESS);
  62809. /* have server accept to both RPK and x509 cert */
  62810. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  62811. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  62812. typeCnt_c = 2;
  62813. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62814. WOLFSSL_SUCCESS);
  62815. /* have server tell to use x509 cert intentionally. This will bring
  62816. * certificate type mismatch in client side.
  62817. */
  62818. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  62819. certType_s[1] = -1;
  62820. typeCnt_s = 1;
  62821. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62822. WOLFSSL_SUCCESS);
  62823. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62824. /* expect server detect cert type mismatch then send Alert */
  62825. ExpectIntNE(ret, 0);
  62826. err = wolfSSL_get_error(ssl_c, ret);
  62827. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  62828. /* client did not load RPK cert actually, so negotiation did not happen */
  62829. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62830. WOLFSSL_SUCCESS);
  62831. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62832. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62833. WOLFSSL_SUCCESS);
  62834. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62835. /* client did not load RPK cert actually, so negotiation did not happen */
  62836. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62837. WOLFSSL_SUCCESS);
  62838. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  62839. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62840. WOLFSSL_SUCCESS);
  62841. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  62842. wolfSSL_free(ssl_c);
  62843. wolfSSL_CTX_free(ctx_c);
  62844. wolfSSL_free(ssl_s);
  62845. wolfSSL_CTX_free(ctx_s);
  62846. ssl_c = ssl_s = NULL;
  62847. ctx_c = ctx_s = NULL;
  62848. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  62849. /* Both client and server load RPK cert and set certificate verify
  62850. * callbacks then start handshaking.
  62851. * Confirm both side can refer the peer's cert.
  62852. */
  62853. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62854. ExpectIntEQ(
  62855. test_rpk_memio_setup(
  62856. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62857. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62858. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62859. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  62860. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  62861. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  62862. , 0);
  62863. /* set client certificate type in client end */
  62864. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  62865. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  62866. typeCnt_c = 2;
  62867. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  62868. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  62869. typeCnt_s = 2;
  62870. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  62871. WOLFSSL_SUCCESS);
  62872. /* set server certificate type in client end */
  62873. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  62874. WOLFSSL_SUCCESS);
  62875. /* set client certificate type in server end */
  62876. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  62877. WOLFSSL_SUCCESS);
  62878. /* set server certificate type in server end */
  62879. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  62880. WOLFSSL_SUCCESS);
  62881. /* set certificate verify callback to both client and server */
  62882. isServer = 0;
  62883. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  62884. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  62885. isServer = 1;
  62886. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  62887. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  62888. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  62889. if (ret != 0)
  62890. return TEST_FAIL;
  62891. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  62892. WOLFSSL_SUCCESS);
  62893. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62894. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  62895. WOLFSSL_SUCCESS);
  62896. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62897. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  62898. WOLFSSL_SUCCESS);
  62899. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62900. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  62901. WOLFSSL_SUCCESS);
  62902. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  62903. wolfSSL_free(ssl_c);
  62904. wolfSSL_CTX_free(ctx_c);
  62905. wolfSSL_free(ssl_s);
  62906. wolfSSL_CTX_free(ctx_s);
  62907. ssl_c = ssl_s = NULL;
  62908. ctx_c = ctx_s = NULL;
  62909. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  62910. #endif /* HAVE_RPK */
  62911. return EXPECT_RESULT();
  62912. }
  62913. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62914. static int test_dtls13_bad_epoch_ch(void)
  62915. {
  62916. EXPECT_DECLS;
  62917. WOLFSSL_CTX *ctx_c = NULL;
  62918. WOLFSSL_CTX *ctx_s = NULL;
  62919. WOLFSSL *ssl_c = NULL;
  62920. WOLFSSL *ssl_s = NULL;
  62921. struct test_memio_ctx test_ctx;
  62922. const int EPOCH_OFF = 3;
  62923. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62924. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62925. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62926. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  62927. * with just one message */
  62928. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62929. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62930. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62931. WOLFSSL_ERROR_WANT_READ);
  62932. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  62933. /* first CH should use epoch 0x0 */
  62934. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  62935. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  62936. /* change epoch to 2 */
  62937. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  62938. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  62939. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  62940. WOLFSSL_ERROR_WANT_READ);
  62941. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  62942. /* resend the CH */
  62943. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62944. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62945. wolfSSL_free(ssl_c);
  62946. wolfSSL_CTX_free(ctx_c);
  62947. wolfSSL_free(ssl_s);
  62948. wolfSSL_CTX_free(ctx_s);
  62949. return EXPECT_RESULT();
  62950. }
  62951. #else
  62952. static int test_dtls13_bad_epoch_ch(void)
  62953. {
  62954. return TEST_SKIPPED;
  62955. }
  62956. #endif
  62957. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  62958. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  62959. {
  62960. EXPECT_DECLS;
  62961. WOLFSSL_SESSION *sess = NULL;
  62962. /* Setup the session to avoid errors */
  62963. ssl->session->timeout = -1;
  62964. ssl->session->side = WOLFSSL_CLIENT_END;
  62965. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  62966. defined(HAVE_SESSION_TICKET))
  62967. ssl->session->version = ssl->version;
  62968. #endif
  62969. /* Force a short session ID to be sent */
  62970. ssl->session->sessionIDSz = 4;
  62971. #ifndef NO_SESSION_CACHE_REF
  62972. /* Allow the client cache to be used */
  62973. ssl->session->idLen = 4;
  62974. #endif
  62975. ssl->session->isSetup = 1;
  62976. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  62977. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  62978. return EXPECT_RESULT();
  62979. }
  62980. static int test_short_session_id(void)
  62981. {
  62982. EXPECT_DECLS;
  62983. test_ssl_cbf client_cbf;
  62984. test_ssl_cbf server_cbf;
  62985. size_t i;
  62986. struct {
  62987. method_provider client_meth;
  62988. method_provider server_meth;
  62989. const char* tls_version;
  62990. } params[] = {
  62991. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  62992. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  62993. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  62994. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  62995. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  62996. #ifdef WOLFSSL_DTLS13
  62997. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  62998. #endif
  62999. #endif
  63000. #ifndef WOLFSSL_NO_TLS12
  63001. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  63002. #ifdef WOLFSSL_DTLS
  63003. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  63004. #endif
  63005. #endif
  63006. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  63007. !defined(NO_DES3))
  63008. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  63009. #ifdef WOLFSSL_DTLS
  63010. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  63011. #endif
  63012. #endif
  63013. };
  63014. fprintf(stderr, "\n");
  63015. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  63016. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63017. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63018. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  63019. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  63020. client_cbf.method = params[i].client_meth;
  63021. server_cbf.method = params[i].server_meth;
  63022. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63023. &server_cbf, NULL), TEST_SUCCESS);
  63024. }
  63025. return EXPECT_RESULT();
  63026. }
  63027. #else
  63028. static int test_short_session_id(void)
  63029. {
  63030. return TEST_SKIPPED;
  63031. }
  63032. #endif
  63033. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  63034. && defined(WOLFSSL_DTLS13)
  63035. static byte* test_find_string(const char *string,
  63036. byte *buf, int buf_size)
  63037. {
  63038. int string_size, i;
  63039. string_size = (int)XSTRLEN(string);
  63040. for (i = 0; i < buf_size - string_size - 1; i++) {
  63041. if (XSTRCMP((char*)&buf[i], string) == 0)
  63042. return &buf[i];
  63043. }
  63044. return NULL;
  63045. }
  63046. static int test_wolfSSL_dtls13_null_cipher(void)
  63047. {
  63048. EXPECT_DECLS;
  63049. WOLFSSL_CTX *ctx_c = NULL;
  63050. WOLFSSL_CTX *ctx_s = NULL;
  63051. WOLFSSL *ssl_c = NULL;
  63052. WOLFSSL *ssl_s = NULL;
  63053. struct test_memio_ctx test_ctx;
  63054. const char *test_str = "test";
  63055. int test_str_size;
  63056. byte buf[255], *ptr = NULL;
  63057. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63058. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  63059. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63060. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  63061. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63062. test_str_size = XSTRLEN("test") + 1;
  63063. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  63064. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  63065. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  63066. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  63067. /* check that the packet was sent cleartext */
  63068. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  63069. test_ctx.s_len));
  63070. if (ptr != NULL) {
  63071. /* modify the message */
  63072. *ptr = 'H';
  63073. /* bad messages should be ignored in DTLS */
  63074. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  63075. ExpectIntEQ(ssl_s->error, WANT_READ);
  63076. }
  63077. wolfSSL_free(ssl_c);
  63078. wolfSSL_free(ssl_s);
  63079. wolfSSL_CTX_free(ctx_c);
  63080. wolfSSL_CTX_free(ctx_s);
  63081. return TEST_SUCCESS;
  63082. }
  63083. #else
  63084. static int test_wolfSSL_dtls13_null_cipher(void)
  63085. {
  63086. return TEST_SKIPPED;
  63087. }
  63088. #endif
  63089. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63090. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63091. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  63092. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  63093. {
  63094. SOCKADDR_S peer;
  63095. XSOCKLENT len;
  63096. int ret;
  63097. XMEMSET((byte*)&peer, 0, sizeof(peer));
  63098. len = sizeof(peer);
  63099. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  63100. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  63101. return -1;
  63102. switch (peer.ss_family) {
  63103. #ifdef WOLFSSL_IPV6
  63104. case WOLFSSL_IP6: {
  63105. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  63106. break;
  63107. }
  63108. #endif /* WOLFSSL_IPV6 */
  63109. case WOLFSSL_IP4:
  63110. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  63111. break;
  63112. default:
  63113. return -1;
  63114. }
  63115. return 0;
  63116. }
  63117. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  63118. {
  63119. char buf[1] = {'t'};
  63120. SOCKADDR_IN_T addr;
  63121. int sock_fd;
  63122. word16 port;
  63123. int err;
  63124. (void)ssl;
  63125. (void)ctx;
  63126. if (ssl == NULL)
  63127. return -1;
  63128. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  63129. if (err != 0)
  63130. return -1;
  63131. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  63132. if (sock_fd == -1)
  63133. return -1;
  63134. build_addr(&addr, wolfSSLIP, port, 1, 0);
  63135. /* send a packet to the server. Being another socket, the kernel will ensure
  63136. * the source port will be different. */
  63137. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  63138. sizeof(addr));
  63139. close(sock_fd);
  63140. if (err == -1)
  63141. return -1;
  63142. return 0;
  63143. }
  63144. /* setup a SSL session but just after the handshake send a packet to the server
  63145. * with a source address different than the one of the connected client. The I/O
  63146. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  63147. * is done in test_dtls_msg_from_other_peer_cb */
  63148. static int test_dtls_msg_from_other_peer(void)
  63149. {
  63150. EXPECT_DECLS;
  63151. callback_functions client_cbs;
  63152. callback_functions server_cbs;
  63153. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  63154. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  63155. client_cbs.method = wolfDTLSv1_2_client_method;
  63156. server_cbs.method = wolfDTLSv1_2_server_method;
  63157. client_cbs.doUdp = 1;
  63158. server_cbs.doUdp = 1;
  63159. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  63160. test_dtls_msg_from_other_peer_cb);
  63161. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  63162. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  63163. return EXPECT_RESULT();
  63164. }
  63165. #else
  63166. static int test_dtls_msg_from_other_peer(void)
  63167. {
  63168. return TEST_SKIPPED;
  63169. }
  63170. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63171. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63172. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  63173. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  63174. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63175. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  63176. static int test_dtls_ipv6_check(void)
  63177. {
  63178. EXPECT_DECLS;
  63179. WOLFSSL_CTX *ctx_c = NULL;
  63180. WOLFSSL_CTX *ctx_s = NULL;
  63181. WOLFSSL *ssl_c = NULL;
  63182. WOLFSSL *ssl_s = NULL;
  63183. SOCKADDR_IN fake_addr6;
  63184. int sockfd = -1;
  63185. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  63186. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  63187. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  63188. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  63189. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  63190. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  63191. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  63192. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  63193. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  63194. /* mimic a sockaddr_in6 struct, this way we can't test without
  63195. * WOLFSSL_IPV6 */
  63196. fake_addr6.sin_family = WOLFSSL_IP6;
  63197. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  63198. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  63199. /* can't return error here, as the peer is opaque for wolfssl library at
  63200. * this point */
  63201. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  63202. WOLFSSL_SUCCESS);
  63203. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  63204. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  63205. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  63206. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  63207. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  63208. WOLFSSL_SUCCESS);
  63209. /* reuse the socket */
  63210. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  63211. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  63212. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  63213. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  63214. if (sockfd != -1)
  63215. close(sockfd);
  63216. wolfSSL_free(ssl_c);
  63217. wolfSSL_CTX_free(ctx_c);
  63218. wolfSSL_free(ssl_s);
  63219. wolfSSL_CTX_free(ctx_s);
  63220. return EXPECT_RESULT();
  63221. }
  63222. #else
  63223. static int test_dtls_ipv6_check(void)
  63224. {
  63225. return TEST_SKIPPED;
  63226. }
  63227. #endif
  63228. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63229. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63230. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  63231. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  63232. {
  63233. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63234. }
  63235. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  63236. {
  63237. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  63238. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  63239. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  63240. }
  63241. else {
  63242. char testMsg[] = "Message after SCR";
  63243. char msgBuf[sizeof(testMsg)];
  63244. int ret;
  63245. if (!wolfSSL_is_server(ssl)) {
  63246. AssertIntEQ(WOLFSSL_SUCCESS,
  63247. wolfSSL_set_session(ssl,
  63248. test_wolfSSL_SCR_after_resumption_session));
  63249. }
  63250. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63251. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63252. sizeof(testMsg));
  63253. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  63254. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  63255. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  63256. AssertIntEQ(ret, sizeof(msgBuf));
  63257. }
  63258. }
  63259. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  63260. {
  63261. AssertIntEQ(WOLFSSL_SUCCESS,
  63262. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  63263. }
  63264. static int test_wolfSSL_SCR_after_resumption(void)
  63265. {
  63266. EXPECT_DECLS;
  63267. callback_functions func_cb_client;
  63268. callback_functions func_cb_server;
  63269. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63270. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63271. func_cb_client.method = wolfTLSv1_2_client_method;
  63272. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  63273. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  63274. func_cb_server.method = wolfTLSv1_2_server_method;
  63275. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  63276. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63277. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63278. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63279. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  63280. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  63281. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63282. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63283. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63284. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  63285. return EXPECT_RESULT();
  63286. }
  63287. #else
  63288. static int test_wolfSSL_SCR_after_resumption(void)
  63289. {
  63290. return TEST_SKIPPED;
  63291. }
  63292. #endif
  63293. static int test_wolfSSL_configure_args(void)
  63294. {
  63295. EXPECT_DECLS;
  63296. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  63297. ExpectNotNull(wolfSSL_configure_args());
  63298. #endif
  63299. return EXPECT_RESULT();
  63300. }
  63301. static int test_dtls_no_extensions(void)
  63302. {
  63303. EXPECT_DECLS;
  63304. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  63305. !defined(WOLFSSL_NO_TLS12)
  63306. WOLFSSL *ssl_s = NULL;
  63307. WOLFSSL_CTX *ctx_s = NULL;
  63308. struct test_memio_ctx test_ctx;
  63309. const byte chNoExtensions[] = {
  63310. /* Handshake type */
  63311. 0x16,
  63312. /* Version */
  63313. 0xfe, 0xff,
  63314. /* Epoch */
  63315. 0x00, 0x00,
  63316. /* Seq number */
  63317. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63318. /* Length */
  63319. 0x00, 0x40,
  63320. /* CH type */
  63321. 0x01,
  63322. /* Length */
  63323. 0x00, 0x00, 0x34,
  63324. /* Msg Seq */
  63325. 0x00, 0x00,
  63326. /* Frag offset */
  63327. 0x00, 0x00, 0x00,
  63328. /* Frag length */
  63329. 0x00, 0x00, 0x34,
  63330. /* Version */
  63331. 0xfe, 0xff,
  63332. /* Random */
  63333. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  63334. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  63335. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  63336. /* Session Length */
  63337. 0x00,
  63338. /* Cookie Length */
  63339. 0x00,
  63340. /* CS Length */
  63341. 0x00, 0x0c,
  63342. /* CS */
  63343. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  63344. /* Comp Meths Length */
  63345. 0x01,
  63346. /* Comp Meths */
  63347. 0x00
  63348. /* And finally... no extensions */
  63349. };
  63350. int i;
  63351. #ifdef OPENSSL_EXTRA
  63352. int repeats = 2;
  63353. #else
  63354. int repeats = 1;
  63355. #endif
  63356. for (i = 0; i < repeats; i++) {
  63357. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63358. ssl_s = NULL;
  63359. ctx_s = NULL;
  63360. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  63361. NULL, wolfDTLS_server_method), 0);
  63362. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  63363. test_ctx.s_len = sizeof(chNoExtensions);
  63364. #ifdef OPENSSL_EXTRA
  63365. if (i > 0) {
  63366. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  63367. WOLFSSL_SUCCESS);
  63368. }
  63369. #endif
  63370. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  63371. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63372. /* Expecting a handshake msg. Either HVR or SH. */
  63373. ExpectIntGT(test_ctx.c_len, 0);
  63374. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  63375. wolfSSL_free(ssl_s);
  63376. wolfSSL_CTX_free(ctx_s);
  63377. }
  63378. #endif
  63379. return EXPECT_RESULT();
  63380. }
  63381. static int test_TLSX_CA_NAMES_bad_extension(void)
  63382. {
  63383. EXPECT_DECLS;
  63384. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63385. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  63386. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  63387. defined(HAVE_NULL_CIPHER)
  63388. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  63389. * parse the CA Names extension. Otherwise it will return other non-related
  63390. * errors. If CA Names will be parsed in more configurations, that should
  63391. * be reflected in the macro guard above. */
  63392. WOLFSSL *ssl_c = NULL;
  63393. WOLFSSL_CTX *ctx_c = NULL;
  63394. struct test_memio_ctx test_ctx;
  63395. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  63396. const byte shBadCaNamesExt[] = {
  63397. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  63398. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  63399. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  63400. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  63401. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  63402. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  63403. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  63404. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  63405. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  63406. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  63407. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  63408. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  63409. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  63410. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  63411. };
  63412. const byte shBadCaNamesExt2[] = {
  63413. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  63414. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  63415. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  63416. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  63417. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  63418. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  63419. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  63420. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  63421. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  63422. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  63423. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  63424. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  63425. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  63426. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  63427. };
  63428. int i = 0;
  63429. for (i = 0; i < 2; i++) {
  63430. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63431. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  63432. wolfTLSv1_3_client_method, NULL), 0);
  63433. switch (i) {
  63434. case 0:
  63435. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  63436. sizeof(shBadCaNamesExt));
  63437. test_ctx.c_len = sizeof(shBadCaNamesExt);
  63438. break;
  63439. case 1:
  63440. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  63441. sizeof(shBadCaNamesExt2));
  63442. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  63443. break;
  63444. }
  63445. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63446. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  63447. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  63448. #else
  63449. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  63450. #endif
  63451. wolfSSL_free(ssl_c);
  63452. ssl_c = NULL;
  63453. wolfSSL_CTX_free(ctx_c);
  63454. ctx_c = NULL;
  63455. }
  63456. #endif
  63457. return EXPECT_RESULT();
  63458. }
  63459. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63460. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63461. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  63462. {
  63463. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63464. WOLFSSL_SUCCESS);
  63465. }
  63466. static int test_dtls_1_0_hvr_downgrade(void)
  63467. {
  63468. EXPECT_DECLS;
  63469. callback_functions func_cb_client;
  63470. callback_functions func_cb_server;
  63471. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63472. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63473. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63474. func_cb_client.method = wolfDTLS_client_method;
  63475. func_cb_server.method = wolfDTLSv1_2_server_method;
  63476. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  63477. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63478. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63479. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63480. return EXPECT_RESULT();
  63481. }
  63482. #else
  63483. static int test_dtls_1_0_hvr_downgrade(void)
  63484. {
  63485. EXPECT_DECLS;
  63486. return EXPECT_RESULT();
  63487. }
  63488. #endif
  63489. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  63490. defined(HAVE_SESSION_TICKET)
  63491. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  63492. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  63493. {
  63494. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  63495. AssertNotNull(test_session_ticket_no_id_session);
  63496. }
  63497. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  63498. {
  63499. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  63500. }
  63501. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  63502. {
  63503. test_session_ticket_no_id_session->sessionIDSz = 0;
  63504. AssertIntEQ(WOLFSSL_SUCCESS,
  63505. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  63506. }
  63507. static int test_session_ticket_no_id(void)
  63508. {
  63509. /* We are testing an expired (invalid crypto context in out case since the
  63510. * ctx changes) session ticket being sent with the session ID being 0
  63511. * length. */
  63512. EXPECT_DECLS;
  63513. callback_functions func_cb_client;
  63514. callback_functions func_cb_server;
  63515. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63516. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63517. func_cb_client.method = wolfTLSv1_2_client_method;
  63518. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63519. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  63520. func_cb_server.method = wolfTLSv1_2_server_method;
  63521. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63522. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63523. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63524. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63525. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  63526. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  63527. func_cb_client.method = wolfTLSv1_2_client_method;
  63528. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63529. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  63530. func_cb_server.method = wolfTLSv1_2_server_method;
  63531. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  63532. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63533. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63534. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63535. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  63536. return EXPECT_RESULT();
  63537. }
  63538. #else
  63539. static int test_session_ticket_no_id(void)
  63540. {
  63541. EXPECT_DECLS;
  63542. return EXPECT_RESULT();
  63543. }
  63544. #endif
  63545. static int test_session_ticket_hs_update(void)
  63546. {
  63547. EXPECT_DECLS;
  63548. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  63549. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  63550. struct test_memio_ctx test_ctx;
  63551. struct test_memio_ctx test_ctx2;
  63552. struct test_memio_ctx test_ctx3;
  63553. WOLFSSL_CTX *ctx_c = NULL;
  63554. WOLFSSL_CTX *ctx_s = NULL;
  63555. WOLFSSL *ssl_c = NULL;
  63556. WOLFSSL *ssl_c2 = NULL;
  63557. WOLFSSL *ssl_c3 = NULL;
  63558. WOLFSSL *ssl_s = NULL;
  63559. WOLFSSL *ssl_s2 = NULL;
  63560. WOLFSSL *ssl_s3 = NULL;
  63561. WOLFSSL_SESSION *sess = NULL;
  63562. byte read_data[1];
  63563. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63564. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  63565. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  63566. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63567. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63568. /* Generate tickets */
  63569. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63570. wolfSSL_SetLoggingPrefix("client");
  63571. /* Read the ticket msg */
  63572. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  63573. WOLFSSL_FATAL_ERROR);
  63574. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  63575. WOLFSSL_ERROR_WANT_READ);
  63576. wolfSSL_SetLoggingPrefix(NULL);
  63577. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  63578. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63579. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  63580. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  63581. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  63582. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  63583. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  63584. wolfSSL_SetLoggingPrefix("client");
  63585. /* Exchange initial flights for the second connection */
  63586. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  63587. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  63588. WOLFSSL_ERROR_WANT_READ);
  63589. wolfSSL_SetLoggingPrefix(NULL);
  63590. wolfSSL_SetLoggingPrefix("server");
  63591. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  63592. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  63593. WOLFSSL_ERROR_WANT_READ);
  63594. wolfSSL_SetLoggingPrefix(NULL);
  63595. /* Complete third connection so that new tickets are exchanged */
  63596. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  63597. /* Read the ticket msg */
  63598. wolfSSL_SetLoggingPrefix("client");
  63599. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  63600. WOLFSSL_FATAL_ERROR);
  63601. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  63602. WOLFSSL_ERROR_WANT_READ);
  63603. wolfSSL_SetLoggingPrefix(NULL);
  63604. /* Complete second connection */
  63605. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  63606. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  63607. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  63608. wolfSSL_free(ssl_c);
  63609. wolfSSL_free(ssl_c2);
  63610. wolfSSL_free(ssl_c3);
  63611. wolfSSL_free(ssl_s);
  63612. wolfSSL_free(ssl_s2);
  63613. wolfSSL_free(ssl_s3);
  63614. wolfSSL_CTX_free(ctx_c);
  63615. wolfSSL_CTX_free(ctx_s);
  63616. wolfSSL_SESSION_free(sess);
  63617. #endif
  63618. return EXPECT_RESULT();
  63619. }
  63620. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63621. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63622. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  63623. {
  63624. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63625. WOLFSSL_SUCCESS);
  63626. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63627. }
  63628. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  63629. {
  63630. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63631. }
  63632. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  63633. {
  63634. char testMsg[] = "Message after SCR";
  63635. char msgBuf[sizeof(testMsg)];
  63636. if (wolfSSL_is_server(ssl)) {
  63637. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  63638. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  63639. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63640. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63641. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63642. sizeof(testMsg));
  63643. }
  63644. else {
  63645. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63646. sizeof(testMsg));
  63647. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63648. }
  63649. }
  63650. static int test_dtls_downgrade_scr_server(void)
  63651. {
  63652. EXPECT_DECLS;
  63653. callback_functions func_cb_client;
  63654. callback_functions func_cb_server;
  63655. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63656. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63657. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63658. func_cb_client.method = wolfDTLSv1_2_client_method;
  63659. func_cb_server.method = wolfDTLS_server_method;
  63660. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  63661. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  63662. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  63663. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  63664. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63665. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63666. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63667. return EXPECT_RESULT();
  63668. }
  63669. #else
  63670. static int test_dtls_downgrade_scr_server(void)
  63671. {
  63672. EXPECT_DECLS;
  63673. return EXPECT_RESULT();
  63674. }
  63675. #endif
  63676. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63677. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  63678. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  63679. {
  63680. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  63681. WOLFSSL_SUCCESS);
  63682. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  63683. }
  63684. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  63685. {
  63686. char testMsg[] = "Message after SCR";
  63687. char msgBuf[sizeof(testMsg)];
  63688. if (wolfSSL_is_server(ssl)) {
  63689. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  63690. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  63691. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63692. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  63693. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63694. sizeof(testMsg));
  63695. }
  63696. else {
  63697. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  63698. sizeof(testMsg));
  63699. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  63700. }
  63701. }
  63702. static int test_dtls_downgrade_scr(void)
  63703. {
  63704. EXPECT_DECLS;
  63705. callback_functions func_cb_client;
  63706. callback_functions func_cb_server;
  63707. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  63708. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  63709. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  63710. func_cb_client.method = wolfDTLS_client_method;
  63711. func_cb_server.method = wolfDTLSv1_2_server_method;
  63712. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  63713. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  63714. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  63715. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  63716. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  63717. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  63718. return EXPECT_RESULT();
  63719. }
  63720. #else
  63721. static int test_dtls_downgrade_scr(void)
  63722. {
  63723. EXPECT_DECLS;
  63724. return EXPECT_RESULT();
  63725. }
  63726. #endif
  63727. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  63728. && !defined(WOLFSSL_NO_TLS12)
  63729. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  63730. char *data, int sz, void *ctx)
  63731. {
  63732. static int call_counter = 0;
  63733. call_counter++;
  63734. (void)ssl;
  63735. (void)data;
  63736. (void)sz;
  63737. (void)ctx;
  63738. switch (call_counter) {
  63739. case 1:
  63740. case 2:
  63741. return WOLFSSL_CBIO_ERR_TIMEOUT;
  63742. case 3:
  63743. return WOLFSSL_CBIO_ERR_WANT_READ;
  63744. default:
  63745. AssertIntLE(call_counter, 3);
  63746. return -1;
  63747. }
  63748. }
  63749. #endif
  63750. /* Make sure we don't send acks before getting a server hello */
  63751. static int test_dtls_client_hello_timeout_downgrade(void)
  63752. {
  63753. EXPECT_DECLS;
  63754. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  63755. && !defined(WOLFSSL_NO_TLS12)
  63756. WOLFSSL_CTX *ctx_c = NULL;
  63757. WOLFSSL_CTX *ctx_s = NULL;
  63758. WOLFSSL *ssl_c = NULL;
  63759. WOLFSSL *ssl_s = NULL;
  63760. struct test_memio_ctx test_ctx;
  63761. DtlsRecordLayerHeader* dtlsRH;
  63762. size_t len;
  63763. byte sequence_number[8];
  63764. int i;
  63765. for (i = 0; i < 2; i++) {
  63766. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63767. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63768. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  63769. if (i == 0) {
  63770. /* First time simulate timeout in IO layer */
  63771. /* CH1 */
  63772. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63773. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63774. /* HVR */
  63775. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63776. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63777. /* CH2 */
  63778. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63779. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63780. /* SH flight */
  63781. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63782. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63783. /* Drop the SH */
  63784. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  63785. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63786. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  63787. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  63788. (sizeof(DtlsRecordLayerHeader) + len));
  63789. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  63790. /* Read the remainder of the flight */
  63791. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63792. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63793. wolfSSL_SSLSetIORecv(ssl_c,
  63794. test_dtls_client_hello_timeout_downgrade_read_cb);
  63795. /* CH3 */
  63796. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63797. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63798. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  63799. }
  63800. else {
  63801. /* Second time call wolfSSL_dtls_got_timeout */
  63802. /* CH1 */
  63803. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63804. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63805. /* HVR */
  63806. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63807. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63808. /* CH2 */
  63809. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63810. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63811. /* SH flight */
  63812. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63813. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63814. /* Drop the SH */
  63815. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  63816. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63817. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  63818. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  63819. (sizeof(DtlsRecordLayerHeader) + len));
  63820. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  63821. /* Read the remainder of the flight */
  63822. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63823. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63824. /* Quick timeout should be set as we received at least one msg */
  63825. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  63826. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63827. /* Quick timeout should be cleared after a quick timeout */
  63828. /* CH3 */
  63829. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  63830. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63831. }
  63832. /* Parse out to make sure we got exactly one ClientHello message */
  63833. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63834. /* Second ClientHello after HVR */
  63835. sequence_number[7] = 2;
  63836. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  63837. ExpectIntEQ(dtlsRH->type, handshake);
  63838. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63839. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63840. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63841. sizeof(sequence_number)), 0);
  63842. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63843. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63844. /* Connection should be able to continue */
  63845. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  63846. wolfSSL_free(ssl_c);
  63847. wolfSSL_free(ssl_s);
  63848. wolfSSL_CTX_free(ctx_c);
  63849. wolfSSL_CTX_free(ctx_s);
  63850. ssl_c = NULL;
  63851. ssl_s = NULL;
  63852. ctx_c = NULL;
  63853. ctx_s = NULL;
  63854. if (!EXPECT_SUCCESS())
  63855. break;
  63856. }
  63857. #endif
  63858. return EXPECT_RESULT();
  63859. }
  63860. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63861. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  63862. int sz, void *ctx)
  63863. {
  63864. static int call_counter = 0;
  63865. call_counter++;
  63866. (void)ssl;
  63867. (void)data;
  63868. (void)sz;
  63869. (void)ctx;
  63870. switch (call_counter) {
  63871. case 1:
  63872. return WOLFSSL_CBIO_ERR_TIMEOUT;
  63873. case 2:
  63874. return WOLFSSL_CBIO_ERR_WANT_READ;
  63875. default:
  63876. AssertIntLE(call_counter, 2);
  63877. return -1;
  63878. }
  63879. }
  63880. #endif
  63881. /* Make sure we don't send acks before getting a server hello */
  63882. static int test_dtls_client_hello_timeout(void)
  63883. {
  63884. EXPECT_DECLS;
  63885. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  63886. WOLFSSL *ssl_c = NULL;
  63887. WOLFSSL_CTX *ctx_c = NULL;
  63888. struct test_memio_ctx test_ctx;
  63889. DtlsRecordLayerHeader* dtlsRH;
  63890. size_t idx;
  63891. size_t len;
  63892. byte sequence_number[8];
  63893. int i;
  63894. for (i = 0; i < 2; i++) {
  63895. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63896. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  63897. wolfDTLSv1_3_client_method, NULL), 0);
  63898. if (i == 0) {
  63899. /* First time simulate timeout in IO layer */
  63900. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  63901. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63902. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63903. }
  63904. else {
  63905. /* Second time call wolfSSL_dtls_got_timeout */
  63906. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  63907. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63908. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63909. }
  63910. /* Parse out to make sure we got exactly two ClientHello messages */
  63911. idx = 0;
  63912. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  63913. /* First ClientHello */
  63914. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  63915. ExpectIntEQ(dtlsRH->type, handshake);
  63916. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63917. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63918. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63919. sizeof(sequence_number)), 0);
  63920. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63921. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63922. idx += sizeof(DtlsRecordLayerHeader) + len;
  63923. /* Second ClientHello */
  63924. sequence_number[7] = 1;
  63925. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  63926. ExpectIntEQ(dtlsRH->type, handshake);
  63927. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  63928. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  63929. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  63930. sizeof(sequence_number)), 0);
  63931. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63932. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  63933. wolfSSL_free(ssl_c);
  63934. wolfSSL_CTX_free(ctx_c);
  63935. ssl_c = NULL;
  63936. ctx_c = NULL;
  63937. if (!EXPECT_SUCCESS())
  63938. break;
  63939. }
  63940. #endif
  63941. return EXPECT_RESULT();
  63942. }
  63943. /* DTLS test when dropping the changed cipher spec message */
  63944. static int test_dtls_dropped_ccs(void)
  63945. {
  63946. EXPECT_DECLS;
  63947. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  63948. && !defined(WOLFSSL_NO_TLS12)
  63949. WOLFSSL_CTX *ctx_c = NULL;
  63950. WOLFSSL_CTX *ctx_s = NULL;
  63951. WOLFSSL *ssl_c = NULL;
  63952. WOLFSSL *ssl_s = NULL;
  63953. struct test_memio_ctx test_ctx;
  63954. DtlsRecordLayerHeader* dtlsRH;
  63955. size_t len;
  63956. byte data[1];
  63957. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  63958. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  63959. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  63960. /* CH1 */
  63961. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63962. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63963. /* HVR */
  63964. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63965. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63966. /* CH2 */
  63967. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63968. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63969. /* Server first flight */
  63970. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  63971. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63972. /* Client flight */
  63973. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63974. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63975. /* Server ccs + finished */
  63976. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  63977. /* Drop the ccs */
  63978. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  63979. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  63980. ExpectIntEQ(len, 1);
  63981. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  63982. if (EXPECT_SUCCESS()) {
  63983. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  63984. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  63985. (sizeof(DtlsRecordLayerHeader) + len));
  63986. }
  63987. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  63988. /* Client rtx flight */
  63989. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  63990. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  63991. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  63992. /* Server ccs + finished rtx */
  63993. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  63994. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  63995. /* Client processes finished */
  63996. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  63997. wolfSSL_free(ssl_c);
  63998. wolfSSL_free(ssl_s);
  63999. wolfSSL_CTX_free(ctx_c);
  64000. wolfSSL_CTX_free(ctx_s);
  64001. #endif
  64002. return EXPECT_RESULT();
  64003. }
  64004. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64005. && !defined(WOLFSSL_NO_TLS12)
  64006. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  64007. byte seq_num)
  64008. {
  64009. EXPECT_DECLS;
  64010. DtlsRecordLayerHeader* dtlsRH;
  64011. byte sequence_number[8];
  64012. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  64013. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  64014. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  64015. ExpectIntEQ(dtlsRH->type, handshake);
  64016. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  64017. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  64018. sequence_number[7] = seq_num;
  64019. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  64020. sizeof(sequence_number)), 0);
  64021. return EXPECT_RESULT();
  64022. }
  64023. #endif
  64024. /*
  64025. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  64026. * and after a HelloRetryRequest. This is testing the server side as it is
  64027. * operating statelessly and should copy the sequence number of the ClientHello.
  64028. */
  64029. static int test_dtls_seq_num_downgrade(void)
  64030. {
  64031. EXPECT_DECLS;
  64032. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64033. && !defined(WOLFSSL_NO_TLS12)
  64034. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64035. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64036. struct test_memio_ctx test_ctx;
  64037. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64038. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64039. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  64040. /* CH1 */
  64041. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64042. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64043. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  64044. test_ctx.s_len, 0), TEST_SUCCESS);
  64045. /* HVR */
  64046. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64047. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64048. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  64049. test_ctx.c_len, 0), TEST_SUCCESS);
  64050. /* CH2 */
  64051. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64052. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64053. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  64054. test_ctx.s_len, 1), TEST_SUCCESS);
  64055. /* Server first flight */
  64056. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64057. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64058. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  64059. test_ctx.c_len, 1), TEST_SUCCESS);
  64060. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64061. wolfSSL_free(ssl_c);
  64062. wolfSSL_CTX_free(ctx_c);
  64063. wolfSSL_free(ssl_s);
  64064. wolfSSL_CTX_free(ctx_s);
  64065. #endif
  64066. return EXPECT_RESULT();
  64067. }
  64068. /**
  64069. * Make sure we don't send RSA Signature Hash Algorithms in the
  64070. * CertificateRequest when we don't have any such ciphers set.
  64071. * @return EXPECT_RESULT()
  64072. */
  64073. static int test_certreq_sighash_algos(void)
  64074. {
  64075. EXPECT_DECLS;
  64076. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  64077. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  64078. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  64079. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  64080. WOLFSSL_CTX *ctx_c = NULL;
  64081. WOLFSSL_CTX *ctx_s = NULL;
  64082. WOLFSSL *ssl_c = NULL;
  64083. WOLFSSL *ssl_s = NULL;
  64084. struct test_memio_ctx test_ctx;
  64085. int idx = 0;
  64086. int maxIdx = 0;
  64087. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64088. test_ctx.c_ciphers = test_ctx.s_ciphers =
  64089. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  64090. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64091. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  64092. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  64093. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  64094. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  64095. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  64096. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64097. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  64098. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64099. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  64100. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  64101. WOLFSSL_ERROR_WANT_READ);
  64102. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  64103. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  64104. WOLFSSL_ERROR_WANT_READ);
  64105. /* Find the CertificateRequest message */
  64106. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  64107. word16 len;
  64108. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  64109. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  64110. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  64111. ato16(test_ctx.c_buff + idx, &len);
  64112. idx += OPAQUE16_LEN;
  64113. if (test_ctx.c_buff[idx] == certificate_request) {
  64114. idx++;
  64115. /* length */
  64116. idx += OPAQUE24_LEN;
  64117. /* cert types */
  64118. idx += 1 + test_ctx.c_buff[idx];
  64119. /* Sig algos */
  64120. ato16(test_ctx.c_buff + idx, &len);
  64121. idx += OPAQUE16_LEN;
  64122. maxIdx = idx + (int)len;
  64123. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  64124. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  64125. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  64126. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  64127. else
  64128. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  64129. }
  64130. break;
  64131. }
  64132. else {
  64133. idx += (int)len;
  64134. }
  64135. }
  64136. ExpectIntLT(idx, test_ctx.c_len);
  64137. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64138. wolfSSL_free(ssl_c);
  64139. wolfSSL_free(ssl_s);
  64140. wolfSSL_CTX_free(ctx_c);
  64141. wolfSSL_CTX_free(ctx_s);
  64142. #endif
  64143. return EXPECT_RESULT();
  64144. }
  64145. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  64146. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  64147. {
  64148. EXPECT_DECLS;
  64149. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  64150. myVerifyAction = VERIFY_USE_PREVERFIY;
  64151. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64152. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64153. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64154. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64155. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  64156. WOLFSSL_SUCCESS);
  64157. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64158. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  64159. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64160. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64161. "./certs/crl/ca-int.pem",
  64162. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64163. return EXPECT_RESULT();
  64164. }
  64165. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  64166. {
  64167. EXPECT_DECLS;
  64168. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  64169. myVerifyAction = VERIFY_USE_PREVERFIY;
  64170. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64171. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64172. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64173. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64174. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  64175. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  64176. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  64177. WOLFSSL_SUCCESS);
  64178. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64179. "./certs/crl/ca-int2.pem",
  64180. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64181. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64182. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  64183. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64184. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  64185. "./certs/crl/ca-int.pem",
  64186. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  64187. return EXPECT_RESULT();
  64188. }
  64189. #endif
  64190. static int test_revoked_loaded_int_cert(void)
  64191. {
  64192. EXPECT_DECLS;
  64193. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  64194. test_ssl_cbf client_cbf;
  64195. test_ssl_cbf server_cbf;
  64196. struct {
  64197. const char* certPemFile;
  64198. const char* keyPemFile;
  64199. ctx_cb client_ctx_ready;
  64200. } test_params[] = {
  64201. {"./certs/intermediate/ca-int2-cert.pem",
  64202. "./certs/intermediate/ca-int2-key.pem",
  64203. test_revoked_loaded_int_cert_ctx_ready1},
  64204. {"./certs/intermediate/server-chain.pem",
  64205. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  64206. {"./certs/intermediate/server-chain-short.pem",
  64207. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  64208. };
  64209. size_t i;
  64210. printf("\n");
  64211. for (i = 0; i < XELEM_CNT(test_params); i++) {
  64212. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  64213. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  64214. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  64215. server_cbf.certPemFile = test_params[i].certPemFile;
  64216. server_cbf.keyPemFile = test_params[i].keyPemFile;
  64217. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  64218. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  64219. &server_cbf, NULL), TEST_FAIL);
  64220. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  64221. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  64222. if (!EXPECT_SUCCESS())
  64223. break;
  64224. printf("\t%s passed\n", test_params[i].certPemFile);
  64225. }
  64226. #endif
  64227. return EXPECT_RESULT();
  64228. }
  64229. static int test_dtls13_frag_ch_pq(void)
  64230. {
  64231. EXPECT_DECLS;
  64232. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  64233. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  64234. WOLFSSL_CTX *ctx_c = NULL;
  64235. WOLFSSL_CTX *ctx_s = NULL;
  64236. WOLFSSL *ssl_c = NULL;
  64237. WOLFSSL *ssl_s = NULL;
  64238. struct test_memio_ctx test_ctx;
  64239. const char *test_str = "test";
  64240. int test_str_size;
  64241. byte buf[255];
  64242. int group = WOLFSSL_KYBER_LEVEL5;
  64243. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64244. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64245. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  64246. /* Add in a large post-quantum key share to make the CH long. */
  64247. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  64248. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  64249. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  64250. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64251. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  64252. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  64253. test_str_size = XSTRLEN("test") + 1;
  64254. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  64255. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  64256. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  64257. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  64258. wolfSSL_free(ssl_c);
  64259. wolfSSL_free(ssl_s);
  64260. wolfSSL_CTX_free(ctx_c);
  64261. wolfSSL_CTX_free(ctx_s);
  64262. #endif
  64263. return EXPECT_RESULT();
  64264. }
  64265. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  64266. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  64267. static int test_dtls_frag_ch_count_records(byte* b, int len)
  64268. {
  64269. DtlsRecordLayerHeader* dtlsRH;
  64270. int records = 0;
  64271. size_t recordLen;
  64272. while (len > 0) {
  64273. records++;
  64274. dtlsRH = (DtlsRecordLayerHeader*)b;
  64275. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  64276. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  64277. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  64278. }
  64279. return records;
  64280. }
  64281. #endif
  64282. static int test_dtls_frag_ch(void)
  64283. {
  64284. EXPECT_DECLS;
  64285. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  64286. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  64287. WOLFSSL_CTX *ctx_c = NULL;
  64288. WOLFSSL_CTX *ctx_s = NULL;
  64289. WOLFSSL *ssl_c = NULL;
  64290. WOLFSSL *ssl_s = NULL;
  64291. struct test_memio_ctx test_ctx;
  64292. static unsigned int DUMMY_MTU = 256;
  64293. unsigned char four_frag_CH[] = {
  64294. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64295. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64296. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  64297. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  64298. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  64299. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  64300. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  64301. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  64302. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  64303. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  64304. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  64305. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  64306. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  64307. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  64308. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  64309. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  64310. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  64311. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  64312. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  64313. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64314. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  64315. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  64316. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  64317. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  64318. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  64319. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  64320. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  64321. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  64322. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  64323. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  64324. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  64325. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  64326. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  64327. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  64328. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  64329. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  64330. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  64331. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  64332. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  64333. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  64334. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  64335. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  64336. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  64337. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  64338. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  64339. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  64340. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  64341. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  64342. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  64343. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  64344. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  64345. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  64346. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  64347. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  64348. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  64349. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  64350. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  64351. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  64352. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  64353. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  64354. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  64355. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  64356. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  64357. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  64358. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  64359. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  64360. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  64361. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  64362. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  64363. 0xa5, 0xcb, 0x68, 0xf3
  64364. };
  64365. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64366. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64367. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  64368. /* Fragment msgs */
  64369. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  64370. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  64371. /* Add in some key shares to make the CH long */
  64372. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  64373. WOLFSSL_SUCCESS);
  64374. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  64375. WOLFSSL_SUCCESS);
  64376. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  64377. WOLFSSL_SUCCESS);
  64378. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  64379. WOLFSSL_SUCCESS);
  64380. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  64381. /* Reject fragmented first CH */
  64382. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  64383. sizeof(four_frag_CH)), 4);
  64384. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  64385. test_ctx.s_len = sizeof(four_frag_CH);
  64386. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  64387. int s_len = test_ctx.s_len;
  64388. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64389. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64390. /* Fail if we didn't advance the buffer to avoid infinite loops */
  64391. ExpectIntLT(test_ctx.s_len, s_len);
  64392. }
  64393. /* Expect all fragments read */
  64394. ExpectIntEQ(test_ctx.s_len, 0);
  64395. /* Expect quietly dropping fragmented first CH */
  64396. ExpectIntEQ(test_ctx.c_len, 0);
  64397. /* CH1 */
  64398. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64399. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64400. /* Count records. Expect 1 unfragmented CH */
  64401. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  64402. test_ctx.s_len), 1);
  64403. /* HRR */
  64404. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64405. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64406. /* CH2 */
  64407. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64408. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64409. /* Count records. Expect fragmented CH */
  64410. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  64411. test_ctx.s_len), 1);
  64412. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64413. wolfSSL_free(ssl_c);
  64414. wolfSSL_free(ssl_s);
  64415. wolfSSL_CTX_free(ctx_c);
  64416. wolfSSL_CTX_free(ctx_s);
  64417. ssl_c = ssl_s = NULL;
  64418. ctx_c = ctx_s = NULL;
  64419. #endif
  64420. return EXPECT_RESULT();
  64421. }
  64422. static int test_dtls_empty_keyshare_with_cookie(void)
  64423. {
  64424. EXPECT_DECLS;
  64425. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  64426. WOLFSSL_CTX *ctx_s = NULL;
  64427. WOLFSSL *ssl_s = NULL;
  64428. struct test_memio_ctx test_ctx;
  64429. unsigned char ch_empty_keyshare_with_cookie[] = {
  64430. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  64431. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  64432. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  64433. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  64434. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  64435. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  64436. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  64437. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  64438. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  64439. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  64440. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  64441. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  64442. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  64443. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  64444. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  64445. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  64446. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  64447. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  64448. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  64449. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  64450. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  64451. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  64452. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  64453. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  64454. };
  64455. DtlsRecordLayerHeader* dtlsRH;
  64456. byte sequence_number[8];
  64457. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  64458. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64459. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  64460. sizeof(ch_empty_keyshare_with_cookie));
  64461. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  64462. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  64463. NULL, wolfDTLSv1_3_server_method), 0);
  64464. /* CH1 */
  64465. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64466. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64467. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  64468. ExpectIntEQ(test_ctx.c_len, 15);
  64469. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  64470. ExpectIntEQ(dtlsRH->type, alert);
  64471. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  64472. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  64473. sequence_number[7] = 1;
  64474. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  64475. sizeof(sequence_number)), 0);
  64476. ExpectIntEQ(dtlsRH->length[0], 0);
  64477. ExpectIntEQ(dtlsRH->length[1], 2);
  64478. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  64479. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  64480. wolfSSL_free(ssl_s);
  64481. wolfSSL_CTX_free(ctx_s);
  64482. #endif
  64483. return EXPECT_RESULT();
  64484. }
  64485. static int test_dtls_old_seq_number(void)
  64486. {
  64487. EXPECT_DECLS;
  64488. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  64489. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64490. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64491. struct test_memio_ctx test_ctx;
  64492. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64493. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64494. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  64495. /* CH1 */
  64496. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64497. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64498. /* HVR */
  64499. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64500. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64501. /* CH2 */
  64502. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64503. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64504. /* Server first flight */
  64505. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64506. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64507. /* Client second flight */
  64508. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  64509. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64510. /* Modify the sequence number */
  64511. {
  64512. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  64513. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  64514. }
  64515. /* Server second flight */
  64516. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  64517. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64518. /* Server should not do anything as a pkt was dropped */
  64519. ExpectIntEQ(test_ctx.c_len, 0);
  64520. ExpectIntEQ(test_ctx.s_len, 0);
  64521. /* Trigger rtx */
  64522. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  64523. /* Complete connection */
  64524. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64525. wolfSSL_free(ssl_c);
  64526. wolfSSL_CTX_free(ctx_c);
  64527. wolfSSL_free(ssl_s);
  64528. wolfSSL_CTX_free(ctx_s);
  64529. #endif
  64530. return EXPECT_RESULT();
  64531. }
  64532. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  64533. defined(HAVE_LIBOQS)
  64534. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  64535. {
  64536. int group = WOLFSSL_KYBER_LEVEL5;
  64537. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  64538. }
  64539. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  64540. {
  64541. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  64542. }
  64543. #endif
  64544. static int test_tls13_pq_groups(void)
  64545. {
  64546. EXPECT_DECLS;
  64547. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  64548. defined(HAVE_LIBOQS)
  64549. callback_functions func_cb_client;
  64550. callback_functions func_cb_server;
  64551. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  64552. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  64553. func_cb_client.method = wolfTLSv1_3_client_method;
  64554. func_cb_server.method = wolfTLSv1_3_server_method;
  64555. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  64556. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  64557. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  64558. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  64559. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  64560. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  64561. #endif
  64562. return EXPECT_RESULT();
  64563. }
  64564. static int test_tls13_early_data(void)
  64565. {
  64566. EXPECT_DECLS;
  64567. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  64568. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  64569. int written = 0;
  64570. int read = 0;
  64571. size_t i;
  64572. int splitEarlyData;
  64573. char msg[] = "This is early data";
  64574. char msg2[] = "This is client data";
  64575. char msg3[] = "This is server data";
  64576. char msg4[] = "This is server immediate data";
  64577. char msgBuf[50];
  64578. struct {
  64579. method_provider client_meth;
  64580. method_provider server_meth;
  64581. const char* tls_version;
  64582. int isUdp;
  64583. } params[] = {
  64584. #ifdef WOLFSSL_TLS13
  64585. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  64586. "TLS 1.3", 0 },
  64587. #endif
  64588. #ifdef WOLFSSL_DTLS13
  64589. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  64590. "DTLS 1.3", 1 },
  64591. #endif
  64592. };
  64593. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  64594. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  64595. struct test_memio_ctx test_ctx;
  64596. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64597. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64598. WOLFSSL_SESSION *sess = NULL;
  64599. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64600. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  64601. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  64602. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  64603. /* Get a ticket so that we can do 0-RTT on the next connection */
  64604. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64605. /* Make sure we read the ticket */
  64606. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  64607. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64608. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  64609. wolfSSL_free(ssl_c);
  64610. ssl_c = NULL;
  64611. wolfSSL_free(ssl_s);
  64612. ssl_s = NULL;
  64613. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64614. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64615. params[i].client_meth, params[i].server_meth), 0);
  64616. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  64617. #ifdef WOLFSSL_DTLS13
  64618. if (params[i].isUdp) {
  64619. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  64620. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  64621. #else
  64622. /* Let's test this but we generally don't recommend turning off the
  64623. * cookie exchange */
  64624. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  64625. #endif
  64626. }
  64627. #endif
  64628. /* Test 0-RTT data */
  64629. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  64630. &written), sizeof(msg));
  64631. ExpectIntEQ(written, sizeof(msg));
  64632. if (splitEarlyData) {
  64633. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  64634. &written), sizeof(msg));
  64635. ExpectIntEQ(written, sizeof(msg));
  64636. }
  64637. /* Read first 0-RTT data (if split otherwise entire data) */
  64638. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64639. &read), sizeof(msg));
  64640. ExpectIntEQ(read, sizeof(msg));
  64641. ExpectStrEQ(msg, msgBuf);
  64642. /* Test 0.5-RTT data */
  64643. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  64644. if (splitEarlyData) {
  64645. /* Read second 0-RTT data */
  64646. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64647. &read), sizeof(msg));
  64648. ExpectIntEQ(read, sizeof(msg));
  64649. ExpectStrEQ(msg, msgBuf);
  64650. }
  64651. if (params[i].isUdp) {
  64652. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64653. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  64654. /* Read server 0.5-RTT data */
  64655. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  64656. ExpectStrEQ(msg4, msgBuf);
  64657. /* Complete handshake */
  64658. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64659. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64660. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  64661. * a user would loop until it is true but here we control both sides so we
  64662. * just assert the expected value. wolfSSL_read_early_data does not provide
  64663. * handshake status to us with non-blocking IO and we can't use
  64664. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  64665. * parsing logic. */
  64666. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  64667. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64668. &read), 0);
  64669. ExpectIntEQ(read, 0);
  64670. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  64671. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  64672. }
  64673. else {
  64674. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  64675. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  64676. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  64677. &read), 0);
  64678. ExpectIntEQ(read, 0);
  64679. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  64680. /* Read server 0.5-RTT data */
  64681. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  64682. ExpectStrEQ(msg4, msgBuf);
  64683. }
  64684. /* Test bi-directional write */
  64685. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  64686. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  64687. ExpectStrEQ(msg2, msgBuf);
  64688. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  64689. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  64690. ExpectStrEQ(msg3, msgBuf);
  64691. ExpectTrue(wolfSSL_session_reused(ssl_c));
  64692. ExpectTrue(wolfSSL_session_reused(ssl_s));
  64693. wolfSSL_SESSION_free(sess);
  64694. wolfSSL_free(ssl_c);
  64695. wolfSSL_free(ssl_s);
  64696. wolfSSL_CTX_free(ctx_c);
  64697. wolfSSL_CTX_free(ctx_s);
  64698. }
  64699. }
  64700. #endif
  64701. return EXPECT_RESULT();
  64702. }
  64703. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  64704. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  64705. {
  64706. EXPECT_DECLS;
  64707. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64708. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  64709. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  64710. return EXPECT_RESULT();
  64711. }
  64712. #endif
  64713. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  64714. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  64715. {
  64716. EXPECT_DECLS;
  64717. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64718. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  64719. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  64720. return EXPECT_RESULT();
  64721. }
  64722. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  64723. {
  64724. EXPECT_DECLS;
  64725. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64726. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  64727. 0), 1);
  64728. return EXPECT_RESULT();
  64729. }
  64730. #endif
  64731. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  64732. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  64733. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  64734. {
  64735. EXPECT_DECLS;
  64736. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  64737. return EXPECT_RESULT();
  64738. }
  64739. #endif
  64740. static int test_self_signed_stapling(void)
  64741. {
  64742. EXPECT_DECLS;
  64743. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  64744. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  64745. test_ssl_cbf client_cbf;
  64746. test_ssl_cbf server_cbf;
  64747. size_t i;
  64748. struct {
  64749. method_provider client_meth;
  64750. method_provider server_meth;
  64751. ctx_cb client_ctx;
  64752. const char* tls_version;
  64753. } params[] = {
  64754. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  64755. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  64756. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  64757. #endif
  64758. #ifndef WOLFSSL_NO_TLS12
  64759. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  64760. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64761. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  64762. #endif
  64763. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  64764. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64765. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  64766. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  64767. test_self_signed_stapling_client_v2_multi_ctx_ready,
  64768. "TLSv1_2 v2 multi" },
  64769. #endif
  64770. #endif
  64771. };
  64772. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  64773. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  64774. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  64775. printf("\nTesting self-signed cert with status request: %s\n",
  64776. params[i].tls_version);
  64777. client_cbf.method = params[i].client_meth;
  64778. client_cbf.ctx_ready = params[i].client_ctx;
  64779. server_cbf.method = params[i].server_meth;
  64780. server_cbf.certPemFile = "certs/ca-cert.pem";
  64781. server_cbf.keyPemFile = "certs/ca-key.pem";
  64782. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  64783. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  64784. &server_cbf, NULL), TEST_SUCCESS);
  64785. }
  64786. #endif
  64787. return EXPECT_RESULT();
  64788. }
  64789. static int test_tls_multi_handshakes_one_record(void)
  64790. {
  64791. EXPECT_DECLS;
  64792. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  64793. struct test_memio_ctx test_ctx;
  64794. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64795. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64796. RecordLayerHeader* rh = NULL;
  64797. byte *len ;
  64798. int newRecIdx = RECORD_HEADER_SZ;
  64799. int idx = 0;
  64800. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64801. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64802. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  64803. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64804. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64805. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  64806. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64807. /* Combine server handshake msgs into one record */
  64808. while (idx < test_ctx.c_len) {
  64809. word16 recLen;
  64810. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  64811. len = &rh->length[0];
  64812. ato16((const byte*)len, &recLen);
  64813. idx += RECORD_HEADER_SZ;
  64814. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  64815. (size_t)recLen);
  64816. newRecIdx += recLen;
  64817. idx += recLen;
  64818. }
  64819. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  64820. len = &rh->length[0];
  64821. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  64822. test_ctx.c_len = newRecIdx;
  64823. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  64824. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64825. wolfSSL_free(ssl_c);
  64826. wolfSSL_free(ssl_s);
  64827. wolfSSL_CTX_free(ctx_c);
  64828. wolfSSL_CTX_free(ctx_s);
  64829. #endif
  64830. return EXPECT_RESULT();
  64831. }
  64832. static int test_write_dup(void)
  64833. {
  64834. EXPECT_DECLS;
  64835. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  64836. size_t i, j;
  64837. char hiWorld[] = "dup message";
  64838. char readData[sizeof(hiWorld) + 5];
  64839. struct {
  64840. method_provider client_meth;
  64841. method_provider server_meth;
  64842. const char* version_name;
  64843. int version;
  64844. } methods[] = {
  64845. #ifndef WOLFSSL_NO_TLS12
  64846. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  64847. #endif
  64848. #ifdef WOLFSSL_TLS13
  64849. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  64850. #endif
  64851. };
  64852. struct {
  64853. const char* cipher;
  64854. int version;
  64855. } ciphers[] = {
  64856. /* For simplicity the macros are copied from internal.h */
  64857. /* TLS 1.2 */
  64858. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  64859. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  64860. #ifndef NO_RSA
  64861. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  64862. #endif
  64863. #endif
  64864. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  64865. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  64866. #endif
  64867. #endif
  64868. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  64869. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  64870. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64871. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  64872. #endif
  64873. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64874. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  64875. #endif
  64876. #endif
  64877. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  64878. && !defined(NO_TLS) && !defined(NO_AES)
  64879. #ifdef HAVE_AESGCM
  64880. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64881. #ifndef NO_RSA
  64882. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  64883. #endif
  64884. #endif
  64885. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64886. #ifndef NO_RSA
  64887. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  64888. #endif
  64889. #endif
  64890. #endif
  64891. #endif
  64892. /* TLS 1.3 */
  64893. #ifdef WOLFSSL_TLS13
  64894. #ifdef HAVE_AESGCM
  64895. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64896. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  64897. #endif
  64898. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  64899. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  64900. #endif
  64901. #endif
  64902. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  64903. #ifndef NO_SHA256
  64904. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  64905. #endif
  64906. #endif
  64907. #ifdef HAVE_AESCCM
  64908. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  64909. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  64910. #endif
  64911. #endif
  64912. #endif
  64913. };
  64914. for (i = 0; i < XELEM_CNT(methods); i++) {
  64915. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  64916. struct test_memio_ctx test_ctx;
  64917. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  64918. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  64919. WOLFSSL *ssl_c2 = NULL;
  64920. if (methods[i].version != ciphers[j].version)
  64921. continue;
  64922. if (i == 0 && j == 0)
  64923. printf("\n");
  64924. printf("Testing %s with %s... ", methods[i].version_name,
  64925. ciphers[j].cipher);
  64926. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64927. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  64928. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64929. methods[i].client_meth, methods[i].server_meth), 0);
  64930. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  64931. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  64932. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  64933. WRITE_DUP_WRITE_E);
  64934. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  64935. sizeof(hiWorld));
  64936. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  64937. sizeof(hiWorld));
  64938. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  64939. sizeof(hiWorld));
  64940. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  64941. WRITE_DUP_READ_E);
  64942. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  64943. sizeof(hiWorld));
  64944. if (EXPECT_SUCCESS())
  64945. printf("ok\n");
  64946. else
  64947. printf("failed\n");
  64948. wolfSSL_free(ssl_c);
  64949. wolfSSL_free(ssl_c2);
  64950. wolfSSL_free(ssl_s);
  64951. wolfSSL_CTX_free(ctx_c);
  64952. wolfSSL_CTX_free(ctx_s);
  64953. }
  64954. }
  64955. #endif
  64956. return EXPECT_RESULT();
  64957. }
  64958. static int test_read_write_hs(void)
  64959. {
  64960. EXPECT_DECLS;
  64961. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  64962. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  64963. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  64964. struct test_memio_ctx test_ctx;
  64965. byte test_buffer[16];
  64966. unsigned int test;
  64967. /* test == 0 : client writes, server reads */
  64968. /* test == 1 : server writes, client reads */
  64969. for (test = 0; test < 2; test++) {
  64970. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  64971. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  64972. wolfTLSv1_2_client_method,
  64973. wolfTLSv1_2_server_method), 0);
  64974. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  64975. /* CH -> */
  64976. if (test == 0) {
  64977. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  64978. } else {
  64979. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  64980. sizeof(test_buffer)), -1);
  64981. }
  64982. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64983. /* <- SH + SKE + SHD */
  64984. if (test == 0) {
  64985. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  64986. sizeof(test_buffer)), -1);
  64987. } else {
  64988. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  64989. }
  64990. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  64991. /* -> CKE + CLIENT FINISHED */
  64992. if (test == 0) {
  64993. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  64994. } else {
  64995. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  64996. sizeof(test_buffer)), -1);
  64997. }
  64998. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  64999. /* abide clang static analyzer */
  65000. if (ssl_s != NULL) {
  65001. /* disable group message to separate sending of ChangeCipherspec
  65002. * from Finished */
  65003. ssl_s->options.groupMessages = 0;
  65004. }
  65005. /* allow writing of CS, but not FINISHED */
  65006. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  65007. /* <- CS */
  65008. if (test == 0) {
  65009. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65010. sizeof(test_buffer)), -1);
  65011. } else {
  65012. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  65013. }
  65014. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  65015. /* move CS message where the client can read it */
  65016. memmove(test_ctx.c_buff,
  65017. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  65018. test_ctx.c_len = 6;
  65019. /* read CS */
  65020. if (test == 0) {
  65021. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  65022. } else {
  65023. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65024. sizeof(test_buffer)), -1);
  65025. }
  65026. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65027. ExpectIntEQ(test_ctx.c_len, 0);
  65028. if (test == 0) {
  65029. /* send SERVER FINISHED */
  65030. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65031. sizeof(test_buffer)), -1);
  65032. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  65033. WOLFSSL_ERROR_WANT_READ);
  65034. } else {
  65035. /* send SERVER FINISHED + App Data */
  65036. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  65037. }
  65038. ExpectIntGT(test_ctx.c_len, 0);
  65039. /* Send and receive the data */
  65040. if (test == 0) {
  65041. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  65042. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  65043. sizeof(test_buffer)), 5);
  65044. } else {
  65045. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  65046. sizeof(test_buffer)), 5);
  65047. }
  65048. ExpectBufEQ(test_buffer, "hello", 5);
  65049. wolfSSL_free(ssl_c);
  65050. wolfSSL_free(ssl_s);
  65051. wolfSSL_CTX_free(ctx_c);
  65052. wolfSSL_CTX_free(ctx_s);
  65053. ssl_c = ssl_s = NULL;
  65054. ctx_c = ctx_s = NULL;
  65055. }
  65056. #endif
  65057. return EXPECT_RESULT();
  65058. }
  65059. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  65060. static const char* test_get_signature_nid_siglag;
  65061. static int test_get_signature_nid_sig;
  65062. static int test_get_signature_nid_hash;
  65063. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  65064. {
  65065. EXPECT_DECLS;
  65066. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  65067. if (!wolfSSL_is_server(ssl)) {
  65068. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  65069. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  65070. }
  65071. return EXPECT_RESULT();
  65072. }
  65073. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65074. {
  65075. EXPECT_DECLS;
  65076. int nid = 0;
  65077. (void)ctx;
  65078. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  65079. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65080. ExpectIntEQ(nid, test_get_signature_nid_sig);
  65081. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65082. ExpectIntEQ(nid, test_get_signature_nid_hash);
  65083. }
  65084. else /* No sigalg info on static ciphersuite */
  65085. return TEST_SUCCESS;
  65086. return EXPECT_RESULT();
  65087. }
  65088. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65089. {
  65090. EXPECT_DECLS;
  65091. int nid = 0;
  65092. (void)ctx;
  65093. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65094. ExpectIntEQ(nid, test_get_signature_nid_sig);
  65095. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  65096. ExpectIntEQ(nid, test_get_signature_nid_hash);
  65097. return EXPECT_RESULT();
  65098. }
  65099. #endif
  65100. static int test_get_signature_nid(void)
  65101. {
  65102. EXPECT_DECLS;
  65103. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  65104. test_ssl_cbf client_cbf;
  65105. test_ssl_cbf server_cbf;
  65106. size_t i;
  65107. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  65108. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  65109. caCertFile }
  65110. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  65111. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  65112. caEccCertFile }
  65113. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  65114. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  65115. caCertFile }
  65116. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  65117. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  65118. caEccCertFile }
  65119. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  65120. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  65121. caEdCertFile }
  65122. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  65123. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  65124. caEd448CertFile }
  65125. struct {
  65126. const char* siglag;
  65127. int sig_nid;
  65128. int hash_nid;
  65129. int tls_ver;
  65130. const char* server_cert;
  65131. const char* server_key;
  65132. const char* client_ca;
  65133. } params[] = {
  65134. #ifndef NO_RSA
  65135. #ifndef NO_SHA256
  65136. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  65137. #ifdef WC_RSA_PSS
  65138. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  65139. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  65140. #endif
  65141. #endif
  65142. #ifdef WOLFSSL_SHA512
  65143. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  65144. #ifdef WC_RSA_PSS
  65145. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  65146. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  65147. #endif
  65148. #endif
  65149. #endif
  65150. #ifdef HAVE_ECC
  65151. #ifndef NO_SHA256
  65152. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  65153. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  65154. #endif
  65155. #endif
  65156. #ifdef HAVE_ED25519
  65157. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  65158. #endif
  65159. #ifdef HAVE_ED448
  65160. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  65161. #endif
  65162. };
  65163. printf("\n");
  65164. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  65165. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  65166. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  65167. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  65168. params[i].siglag);
  65169. switch (params[i].tls_ver) {
  65170. #ifndef WOLFSSL_NO_TLS12
  65171. case WOLFSSL_TLSV1_2:
  65172. client_cbf.method = wolfTLSv1_2_client_method;
  65173. server_cbf.method = wolfTLSv1_2_server_method;
  65174. break;
  65175. #endif
  65176. #ifdef WOLFSSL_TLS13
  65177. case WOLFSSL_TLSV1_3:
  65178. client_cbf.method = wolfTLSv1_3_client_method;
  65179. server_cbf.method = wolfTLSv1_3_server_method;
  65180. break;
  65181. #endif
  65182. default:
  65183. printf("skipping\n");
  65184. continue;
  65185. }
  65186. test_get_signature_nid_siglag = params[i].siglag;
  65187. test_get_signature_nid_sig = params[i].sig_nid;
  65188. test_get_signature_nid_hash = params[i].hash_nid;
  65189. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  65190. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  65191. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  65192. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  65193. server_cbf.certPemFile = params[i].server_cert;
  65194. server_cbf.keyPemFile = params[i].server_key;
  65195. client_cbf.caPemFile = params[i].client_ca;
  65196. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  65197. &server_cbf, NULL), TEST_SUCCESS);
  65198. if (EXPECT_SUCCESS())
  65199. printf("passed\n");
  65200. }
  65201. #endif
  65202. return EXPECT_RESULT();
  65203. }
  65204. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65205. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  65206. {
  65207. #ifndef NO_MD5
  65208. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  65209. #elif !defined(NO_SHA)
  65210. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  65211. #elif !defined(NO_SHA256)
  65212. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  65213. #else
  65214. #error "We need a digest to hash the Signer object"
  65215. #endif
  65216. byte hashBuf[WC_MAX_DIGEST_SIZE];
  65217. wc_HashAlg hash;
  65218. size_t i;
  65219. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  65220. for (i = 0; i < CA_TABLE_SIZE; i++) {
  65221. Signer* cur;
  65222. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  65223. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  65224. sizeof(*cur)), 0);
  65225. }
  65226. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  65227. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  65228. return MakeWordFromHash(hashBuf);
  65229. }
  65230. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  65231. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  65232. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  65233. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  65234. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  65235. {
  65236. EXPECT_DECLS;
  65237. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  65238. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  65239. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  65240. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  65241. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  65242. return EXPECT_RESULT();
  65243. }
  65244. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  65245. {
  65246. EXPECT_DECLS;
  65247. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  65248. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  65249. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  65250. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  65251. return EXPECT_RESULT();
  65252. }
  65253. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  65254. {
  65255. EXPECT_DECLS;
  65256. WOLFSSL_CERT_MANAGER* cm;
  65257. (void)ssl;
  65258. /* WARNING: this approach bypasses the reference counter check in
  65259. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  65260. * cause unexpected behaviour when other active connections try accessing
  65261. * the caTable. */
  65262. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  65263. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  65264. WOLFSSL_SUCCESS);
  65265. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  65266. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  65267. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  65268. return EXPECT_RESULT();
  65269. }
  65270. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  65271. {
  65272. EXPECT_DECLS;
  65273. WOLFSSL_CTX* ctx;
  65274. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  65275. return EXPECT_RESULT();
  65276. }
  65277. #endif
  65278. static int test_tls_cert_store_unchanged(void)
  65279. {
  65280. EXPECT_DECLS;
  65281. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  65282. test_ssl_cbf client_cbf;
  65283. test_ssl_cbf server_cbf;
  65284. int i;
  65285. for (i = 0; i < 2; i++) {
  65286. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  65287. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  65288. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  65289. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  65290. sizeof(test_tls_cert_store_unchanged_before_hashes));
  65291. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  65292. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  65293. sizeof(test_tls_cert_store_unchanged_after_hashes));
  65294. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  65295. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  65296. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  65297. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  65298. switch (i) {
  65299. case 0:
  65300. client_cbf.on_ctx_cleanup =
  65301. test_tls_cert_store_unchanged_ctx_cleanup;
  65302. server_cbf.on_ctx_cleanup =
  65303. test_tls_cert_store_unchanged_ctx_cleanup;
  65304. break;
  65305. case 1:
  65306. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  65307. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  65308. break;
  65309. default:
  65310. Fail(("Should not enter here"), ("Entered here"));
  65311. }
  65312. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  65313. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  65314. server_cbf.caPemFile = caCertFile;
  65315. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  65316. &server_cbf, NULL), TEST_SUCCESS);
  65317. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  65318. test_tls_cert_store_unchanged_after_hashes,
  65319. sizeof(test_tls_cert_store_unchanged_after_hashes));
  65320. }
  65321. #endif
  65322. return EXPECT_RESULT();
  65323. }
  65324. static int test_wolfSSL_SendUserCanceled(void)
  65325. {
  65326. EXPECT_DECLS;
  65327. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  65328. size_t i;
  65329. struct {
  65330. method_provider client_meth;
  65331. method_provider server_meth;
  65332. const char* tls_version;
  65333. } params[] = {
  65334. #if defined(WOLFSSL_TLS13)
  65335. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  65336. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  65337. #ifdef WOLFSSL_DTLS13
  65338. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  65339. #endif
  65340. #endif
  65341. #ifndef WOLFSSL_NO_TLS12
  65342. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  65343. #ifdef WOLFSSL_DTLS
  65344. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  65345. #endif
  65346. #endif
  65347. #if !defined(NO_OLD_TLS)
  65348. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  65349. #ifdef WOLFSSL_DTLS
  65350. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  65351. #endif
  65352. #endif
  65353. };
  65354. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  65355. WOLFSSL_CTX *ctx_c = NULL;
  65356. WOLFSSL_CTX *ctx_s = NULL;
  65357. WOLFSSL *ssl_c = NULL;
  65358. WOLFSSL *ssl_s = NULL;
  65359. struct test_memio_ctx test_ctx;
  65360. WOLFSSL_ALERT_HISTORY h;
  65361. printf("Testing %s\n", params[i].tls_version);
  65362. XMEMSET(&h, 0, sizeof(h));
  65363. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  65364. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  65365. params[i].client_meth, params[i].server_meth), 0);
  65366. /* CH1 */
  65367. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  65368. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  65369. ExpectIntEQ(wolfSSL_SendUserCanceled(ssl_s), WOLFSSL_SHUTDOWN_NOT_DONE);
  65370. /* Alert closed connection */
  65371. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  65372. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_ZERO_RETURN);
  65373. /* Last alert will be close notify because user_canceled should be
  65374. * followed by a close_notify */
  65375. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  65376. ExpectIntEQ(h.last_rx.code, close_notify);
  65377. ExpectIntEQ(h.last_rx.level, alert_warning);
  65378. wolfSSL_free(ssl_c);
  65379. wolfSSL_free(ssl_s);
  65380. wolfSSL_CTX_free(ctx_c);
  65381. wolfSSL_CTX_free(ctx_s);
  65382. }
  65383. #endif
  65384. return EXPECT_RESULT();
  65385. }
  65386. /*----------------------------------------------------------------------------*
  65387. | Main
  65388. *----------------------------------------------------------------------------*/
  65389. typedef int (*TEST_FUNC)(void);
  65390. typedef struct {
  65391. const char *name;
  65392. TEST_FUNC func;
  65393. byte run:1;
  65394. byte fail:1;
  65395. } TEST_CASE;
  65396. #define TEST_DECL(func) { #func, func, 0, 0 }
  65397. int testAll = 1;
  65398. TEST_CASE testCases[] = {
  65399. TEST_DECL(test_fileAccess),
  65400. /*********************************
  65401. * wolfcrypt
  65402. *********************************/
  65403. TEST_DECL(test_ForceZero),
  65404. TEST_DECL(test_wolfCrypt_Init),
  65405. TEST_DECL(test_wc_LoadStaticMemory_ex),
  65406. /* Locking with Compat Mutex */
  65407. TEST_DECL(test_wc_SetMutexCb),
  65408. TEST_DECL(test_wc_LockMutex_ex),
  65409. /* Digests */
  65410. TEST_DECL(test_wc_InitMd5),
  65411. TEST_DECL(test_wc_Md5Update),
  65412. TEST_DECL(test_wc_Md5Final),
  65413. TEST_DECL(test_wc_InitSha),
  65414. TEST_DECL(test_wc_ShaUpdate),
  65415. TEST_DECL(test_wc_ShaFinal),
  65416. TEST_DECL(test_wc_InitSha256),
  65417. TEST_DECL(test_wc_Sha256Update),
  65418. TEST_DECL(test_wc_Sha256Final),
  65419. TEST_DECL(test_wc_Sha256FinalRaw),
  65420. TEST_DECL(test_wc_Sha256GetFlags),
  65421. TEST_DECL(test_wc_Sha256Free),
  65422. TEST_DECL(test_wc_Sha256GetHash),
  65423. TEST_DECL(test_wc_Sha256Copy),
  65424. TEST_DECL(test_wc_InitSha224),
  65425. TEST_DECL(test_wc_Sha224Update),
  65426. TEST_DECL(test_wc_Sha224Final),
  65427. TEST_DECL(test_wc_Sha224SetFlags),
  65428. TEST_DECL(test_wc_Sha224GetFlags),
  65429. TEST_DECL(test_wc_Sha224Free),
  65430. TEST_DECL(test_wc_Sha224GetHash),
  65431. TEST_DECL(test_wc_Sha224Copy),
  65432. TEST_DECL(test_wc_InitSha512),
  65433. TEST_DECL(test_wc_Sha512Update),
  65434. TEST_DECL(test_wc_Sha512Final),
  65435. TEST_DECL(test_wc_Sha512GetFlags),
  65436. TEST_DECL(test_wc_Sha512FinalRaw),
  65437. TEST_DECL(test_wc_Sha512Free),
  65438. TEST_DECL(test_wc_Sha512GetHash),
  65439. TEST_DECL(test_wc_Sha512Copy),
  65440. TEST_DECL(test_wc_InitSha512_224),
  65441. TEST_DECL(test_wc_Sha512_224Update),
  65442. TEST_DECL(test_wc_Sha512_224Final),
  65443. TEST_DECL(test_wc_Sha512_224GetFlags),
  65444. TEST_DECL(test_wc_Sha512_224FinalRaw),
  65445. TEST_DECL(test_wc_Sha512_224Free),
  65446. TEST_DECL(test_wc_Sha512_224GetHash),
  65447. TEST_DECL(test_wc_Sha512_224Copy),
  65448. TEST_DECL(test_wc_InitSha512_256),
  65449. TEST_DECL(test_wc_Sha512_256Update),
  65450. TEST_DECL(test_wc_Sha512_256Final),
  65451. TEST_DECL(test_wc_Sha512_256GetFlags),
  65452. TEST_DECL(test_wc_Sha512_256FinalRaw),
  65453. TEST_DECL(test_wc_Sha512_256Free),
  65454. TEST_DECL(test_wc_Sha512_256GetHash),
  65455. TEST_DECL(test_wc_Sha512_256Copy),
  65456. TEST_DECL(test_wc_InitSha384),
  65457. TEST_DECL(test_wc_Sha384Update),
  65458. TEST_DECL(test_wc_Sha384Final),
  65459. TEST_DECL(test_wc_Sha384GetFlags),
  65460. TEST_DECL(test_wc_Sha384FinalRaw),
  65461. TEST_DECL(test_wc_Sha384Free),
  65462. TEST_DECL(test_wc_Sha384GetHash),
  65463. TEST_DECL(test_wc_Sha384Copy),
  65464. TEST_DECL(test_wc_InitBlake2b),
  65465. TEST_DECL(test_wc_InitBlake2b_WithKey),
  65466. TEST_DECL(test_wc_InitBlake2s_WithKey),
  65467. TEST_DECL(test_wc_InitRipeMd),
  65468. TEST_DECL(test_wc_RipeMdUpdate),
  65469. TEST_DECL(test_wc_RipeMdFinal),
  65470. TEST_DECL(test_wc_InitSha3),
  65471. TEST_DECL(testing_wc_Sha3_Update),
  65472. TEST_DECL(test_wc_Sha3_224_Final),
  65473. TEST_DECL(test_wc_Sha3_256_Final),
  65474. TEST_DECL(test_wc_Sha3_384_Final),
  65475. TEST_DECL(test_wc_Sha3_512_Final),
  65476. TEST_DECL(test_wc_Sha3_224_Copy),
  65477. TEST_DECL(test_wc_Sha3_256_Copy),
  65478. TEST_DECL(test_wc_Sha3_384_Copy),
  65479. TEST_DECL(test_wc_Sha3_512_Copy),
  65480. TEST_DECL(test_wc_Sha3_GetFlags),
  65481. TEST_DECL(test_wc_InitShake256),
  65482. TEST_DECL(testing_wc_Shake256_Update),
  65483. TEST_DECL(test_wc_Shake256_Final),
  65484. TEST_DECL(test_wc_Shake256_Copy),
  65485. TEST_DECL(test_wc_Shake256Hash),
  65486. /* SM3 Digest */
  65487. TEST_DECL(test_wc_InitSm3Free),
  65488. TEST_DECL(test_wc_Sm3UpdateFinal),
  65489. TEST_DECL(test_wc_Sm3GetHash),
  65490. TEST_DECL(test_wc_Sm3Copy),
  65491. TEST_DECL(test_wc_Sm3FinalRaw),
  65492. TEST_DECL(test_wc_Sm3GetSetFlags),
  65493. TEST_DECL(test_wc_Sm3Hash),
  65494. TEST_DECL(test_wc_HashInit),
  65495. TEST_DECL(test_wc_HashSetFlags),
  65496. TEST_DECL(test_wc_HashGetFlags),
  65497. /* HMAC */
  65498. TEST_DECL(test_wc_Md5HmacSetKey),
  65499. TEST_DECL(test_wc_Md5HmacUpdate),
  65500. TEST_DECL(test_wc_Md5HmacFinal),
  65501. TEST_DECL(test_wc_ShaHmacSetKey),
  65502. TEST_DECL(test_wc_ShaHmacUpdate),
  65503. TEST_DECL(test_wc_ShaHmacFinal),
  65504. TEST_DECL(test_wc_Sha224HmacSetKey),
  65505. TEST_DECL(test_wc_Sha224HmacUpdate),
  65506. TEST_DECL(test_wc_Sha224HmacFinal),
  65507. TEST_DECL(test_wc_Sha256HmacSetKey),
  65508. TEST_DECL(test_wc_Sha256HmacUpdate),
  65509. TEST_DECL(test_wc_Sha256HmacFinal),
  65510. TEST_DECL(test_wc_Sha384HmacSetKey),
  65511. TEST_DECL(test_wc_Sha384HmacUpdate),
  65512. TEST_DECL(test_wc_Sha384HmacFinal),
  65513. /* CMAC */
  65514. TEST_DECL(test_wc_InitCmac),
  65515. TEST_DECL(test_wc_CmacUpdate),
  65516. TEST_DECL(test_wc_CmacFinal),
  65517. TEST_DECL(test_wc_AesCmacGenerate),
  65518. /* Cipher */
  65519. TEST_DECL(test_wc_AesGcmStream),
  65520. TEST_DECL(test_wc_Des3_SetIV),
  65521. TEST_DECL(test_wc_Des3_SetKey),
  65522. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  65523. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  65524. TEST_DECL(test_wc_Des3_EcbEncrypt),
  65525. TEST_DECL(test_wc_Chacha_SetKey),
  65526. TEST_DECL(test_wc_Chacha_Process),
  65527. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  65528. TEST_DECL(test_wc_Poly1305SetKey),
  65529. TEST_DECL(test_wc_CamelliaSetKey),
  65530. TEST_DECL(test_wc_CamelliaSetIV),
  65531. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  65532. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  65533. TEST_DECL(test_wc_Arc4SetKey),
  65534. TEST_DECL(test_wc_Arc4Process),
  65535. TEST_DECL(test_wc_Rc2SetKey),
  65536. TEST_DECL(test_wc_Rc2SetIV),
  65537. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  65538. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  65539. /* AES cipher and GMAC. */
  65540. TEST_DECL(test_wc_AesSetKey),
  65541. TEST_DECL(test_wc_AesSetIV),
  65542. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  65543. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  65544. TEST_DECL(test_wc_AesGcmSetKey),
  65545. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  65546. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  65547. TEST_DECL(test_wc_GmacSetKey),
  65548. TEST_DECL(test_wc_GmacUpdate),
  65549. TEST_DECL(test_wc_AesCcmSetKey),
  65550. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  65551. #if defined(WOLFSSL_AES_EAX) && \
  65552. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  65553. TEST_DECL(test_wc_AesEaxVectors),
  65554. TEST_DECL(test_wc_AesEaxEncryptAuth),
  65555. TEST_DECL(test_wc_AesEaxDecryptAuth),
  65556. #endif /* WOLFSSL_AES_EAX */
  65557. /* SM4 cipher */
  65558. TEST_DECL(test_wc_Sm4),
  65559. TEST_DECL(test_wc_Sm4Ecb),
  65560. TEST_DECL(test_wc_Sm4Cbc),
  65561. TEST_DECL(test_wc_Sm4Ctr),
  65562. TEST_DECL(test_wc_Sm4Gcm),
  65563. TEST_DECL(test_wc_Sm4Ccm),
  65564. /* RNG tests */
  65565. #ifdef HAVE_HASHDRBG
  65566. #ifdef TEST_RESEED_INTERVAL
  65567. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  65568. #endif
  65569. TEST_DECL(test_wc_RNG_GenerateBlock),
  65570. #endif
  65571. TEST_DECL(test_get_rand_digit),
  65572. TEST_DECL(test_wc_InitRngNonce),
  65573. TEST_DECL(test_wc_InitRngNonce_ex),
  65574. /* MP API tests */
  65575. TEST_DECL(test_get_digit_count),
  65576. TEST_DECL(test_mp_cond_copy),
  65577. TEST_DECL(test_mp_rand),
  65578. TEST_DECL(test_get_digit),
  65579. TEST_DECL(test_wc_export_int),
  65580. /* RSA */
  65581. TEST_DECL(test_wc_InitRsaKey),
  65582. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  65583. TEST_DECL(test_wc_RsaPublicKeyDecode),
  65584. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  65585. TEST_DECL(test_wc_RsaPrivateKeyDecodeRaw),
  65586. TEST_DECL(test_wc_MakeRsaKey),
  65587. TEST_DECL(test_wc_CheckProbablePrime),
  65588. TEST_DECL(test_wc_RsaPSS_Verify),
  65589. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  65590. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  65591. TEST_DECL(test_wc_RsaKeyToDer),
  65592. TEST_DECL(test_wc_RsaKeyToPublicDer),
  65593. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  65594. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  65595. TEST_DECL(test_wc_RsaEncryptSize),
  65596. TEST_DECL(test_wc_RsaSSL_SignVerify),
  65597. TEST_DECL(test_wc_RsaFlattenPublicKey),
  65598. TEST_DECL(test_RsaDecryptBoundsCheck),
  65599. /* DSA */
  65600. TEST_DECL(test_wc_InitDsaKey),
  65601. TEST_DECL(test_wc_DsaSignVerify),
  65602. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  65603. TEST_DECL(test_wc_MakeDsaKey),
  65604. TEST_DECL(test_wc_DsaKeyToDer),
  65605. TEST_DECL(test_wc_DsaKeyToPublicDer),
  65606. TEST_DECL(test_wc_DsaImportParamsRaw),
  65607. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  65608. TEST_DECL(test_wc_DsaExportParamsRaw),
  65609. TEST_DECL(test_wc_DsaExportKeyRaw),
  65610. /* DH */
  65611. TEST_DECL(test_wc_DhPublicKeyDecode),
  65612. /* wolfCrypt ECC tests */
  65613. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  65614. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  65615. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  65616. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  65617. !defined(HAVE_SELFTEST) && \
  65618. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  65619. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  65620. #endif
  65621. TEST_DECL(test_wc_ecc_make_key),
  65622. TEST_DECL(test_wc_ecc_init),
  65623. TEST_DECL(test_wc_ecc_check_key),
  65624. TEST_DECL(test_wc_ecc_get_generator),
  65625. TEST_DECL(test_wc_ecc_size),
  65626. TEST_DECL(test_wc_ecc_params),
  65627. TEST_DECL(test_wc_ecc_signVerify_hash),
  65628. TEST_DECL(test_wc_ecc_shared_secret),
  65629. TEST_DECL(test_wc_ecc_export_x963),
  65630. TEST_DECL(test_wc_ecc_export_x963_ex),
  65631. TEST_DECL(test_wc_ecc_import_x963),
  65632. TEST_DECL(test_wc_ecc_import_private_key),
  65633. TEST_DECL(test_wc_ecc_export_private_only),
  65634. TEST_DECL(test_wc_ecc_rs_to_sig),
  65635. TEST_DECL(test_wc_ecc_import_raw),
  65636. TEST_DECL(test_wc_ecc_import_unsigned),
  65637. TEST_DECL(test_wc_ecc_sig_size),
  65638. TEST_DECL(test_wc_ecc_ctx_new),
  65639. TEST_DECL(test_wc_ecc_ctx_reset),
  65640. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  65641. TEST_DECL(test_wc_ecc_ctx_set_info),
  65642. TEST_DECL(test_wc_ecc_encryptDecrypt),
  65643. TEST_DECL(test_wc_ecc_del_point),
  65644. TEST_DECL(test_wc_ecc_pointFns),
  65645. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  65646. TEST_DECL(test_wc_ecc_verify_hash_ex),
  65647. TEST_DECL(test_wc_ecc_mulmod),
  65648. TEST_DECL(test_wc_ecc_is_valid_idx),
  65649. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  65650. TEST_DECL(test_wc_ecc_sig_size_calc),
  65651. TEST_DECL(test_wc_EccPrivateKeyToDer),
  65652. /* SM2 elliptic curve */
  65653. TEST_DECL(test_wc_ecc_sm2_make_key),
  65654. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  65655. TEST_DECL(test_wc_ecc_sm2_create_digest),
  65656. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  65657. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  65658. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  65659. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  65660. /* Curve25519 */
  65661. TEST_DECL(test_wc_curve25519_init),
  65662. TEST_DECL(test_wc_curve25519_size),
  65663. TEST_DECL(test_wc_curve25519_export_key_raw),
  65664. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  65665. TEST_DECL(test_wc_curve25519_make_key),
  65666. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  65667. TEST_DECL(test_wc_curve25519_make_pub),
  65668. TEST_DECL(test_wc_curve25519_export_public_ex),
  65669. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  65670. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  65671. TEST_DECL(test_wc_curve25519_import_private),
  65672. /* ED25519 */
  65673. TEST_DECL(test_wc_ed25519_make_key),
  65674. TEST_DECL(test_wc_ed25519_init),
  65675. TEST_DECL(test_wc_ed25519_sign_msg),
  65676. TEST_DECL(test_wc_ed25519_import_public),
  65677. TEST_DECL(test_wc_ed25519_import_private_key),
  65678. TEST_DECL(test_wc_ed25519_export),
  65679. TEST_DECL(test_wc_ed25519_size),
  65680. TEST_DECL(test_wc_ed25519_exportKey),
  65681. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  65682. TEST_DECL(test_wc_Ed25519KeyToDer),
  65683. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  65684. /* Curve448 */
  65685. TEST_DECL(test_wc_curve448_make_key),
  65686. TEST_DECL(test_wc_curve448_shared_secret_ex),
  65687. TEST_DECL(test_wc_curve448_export_public_ex),
  65688. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  65689. TEST_DECL(test_wc_curve448_export_key_raw),
  65690. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  65691. TEST_DECL(test_wc_curve448_import_private),
  65692. TEST_DECL(test_wc_curve448_init),
  65693. TEST_DECL(test_wc_curve448_size),
  65694. /* Ed448 */
  65695. TEST_DECL(test_wc_ed448_make_key),
  65696. TEST_DECL(test_wc_ed448_init),
  65697. TEST_DECL(test_wc_ed448_sign_msg),
  65698. TEST_DECL(test_wc_ed448_import_public),
  65699. TEST_DECL(test_wc_ed448_import_private_key),
  65700. TEST_DECL(test_wc_ed448_export),
  65701. TEST_DECL(test_wc_ed448_size),
  65702. TEST_DECL(test_wc_ed448_exportKey),
  65703. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  65704. TEST_DECL(test_wc_Ed448KeyToDer),
  65705. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  65706. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  65707. /* Dilithium */
  65708. TEST_DECL(test_wc_dilithium),
  65709. TEST_DECL(test_wc_dilithium_make_key),
  65710. TEST_DECL(test_wc_dilithium_sign),
  65711. TEST_DECL(test_wc_dilithium_verify),
  65712. TEST_DECL(test_wc_dilithium_check_key),
  65713. TEST_DECL(test_wc_dilithium_der),
  65714. /* Signature API */
  65715. TEST_DECL(test_wc_SignatureGetSize_ecc),
  65716. TEST_DECL(test_wc_SignatureGetSize_rsa),
  65717. /* PEM and DER APIs. */
  65718. TEST_DECL(test_wc_PemToDer),
  65719. TEST_DECL(test_wc_AllocDer),
  65720. TEST_DECL(test_wc_CertPemToDer),
  65721. TEST_DECL(test_wc_KeyPemToDer),
  65722. TEST_DECL(test_wc_PubKeyPemToDer),
  65723. TEST_DECL(test_wc_PemPubKeyToDer),
  65724. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  65725. TEST_DECL(test_wc_CheckCertSigPubKey),
  65726. /* wolfCrypt ASN tests */
  65727. TEST_DECL(test_ToTraditional),
  65728. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  65729. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  65730. /* Certificate */
  65731. TEST_DECL(test_wc_SetSubjectRaw),
  65732. TEST_DECL(test_wc_GetSubjectRaw),
  65733. TEST_DECL(test_wc_SetIssuerRaw),
  65734. TEST_DECL(test_wc_SetIssueBuffer),
  65735. TEST_DECL(test_wc_SetSubjectKeyId),
  65736. TEST_DECL(test_wc_SetSubject),
  65737. TEST_DECL(test_CheckCertSignature),
  65738. TEST_DECL(test_wc_ParseCert),
  65739. TEST_DECL(test_wc_ParseCert_Error),
  65740. TEST_DECL(test_MakeCertWithPathLen),
  65741. TEST_DECL(test_MakeCertWith0Ser),
  65742. TEST_DECL(test_MakeCertWithCaFalse),
  65743. TEST_DECL(test_wc_SetKeyUsage),
  65744. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  65745. TEST_DECL(test_wc_SetSubjectBuffer),
  65746. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  65747. /* wolfcrypt PKCS#7 */
  65748. TEST_DECL(test_wc_PKCS7_New),
  65749. TEST_DECL(test_wc_PKCS7_Init),
  65750. TEST_DECL(test_wc_PKCS7_InitWithCert),
  65751. TEST_DECL(test_wc_PKCS7_EncodeData),
  65752. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  65753. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  65754. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  65755. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  65756. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  65757. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  65758. TEST_DECL(test_wc_PKCS7_Degenerate),
  65759. TEST_DECL(test_wc_PKCS7_BER),
  65760. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  65761. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  65762. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  65763. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  65764. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  65765. /* wolfCrypt PKCS#12 */
  65766. TEST_DECL(test_wc_i2d_PKCS12),
  65767. /*
  65768. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  65769. * avoid memory leaks.
  65770. */
  65771. TEST_DECL(test_wolfCrypt_Cleanup),
  65772. TEST_DECL(test_wolfSSL_Init),
  65773. TEST_DECL(test_dual_alg_support),
  65774. /*********************************
  65775. * OpenSSL compatibility API tests
  65776. *********************************/
  65777. /* If at some point a stub get implemented this test should fail indicating
  65778. * a need to implement a new test case
  65779. */
  65780. TEST_DECL(test_stubs_are_stubs),
  65781. /* ASN.1 compatibility API tests */
  65782. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  65783. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  65784. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  65785. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  65786. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  65787. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  65788. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  65789. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  65790. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  65791. TEST_DECL(test_wolfSSL_ASN1_get_object),
  65792. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  65793. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  65794. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  65795. TEST_DECL(test_wolfSSL_ASN1_STRING),
  65796. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  65797. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  65798. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  65799. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  65800. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  65801. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  65802. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  65803. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  65804. TEST_DECL(test_wolfSSL_ASN1_TIME),
  65805. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  65806. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  65807. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  65808. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  65809. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  65810. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  65811. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  65812. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  65813. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  65814. TEST_DECL(test_wolfSSL_lhash),
  65815. TEST_DECL(test_wolfSSL_certs),
  65816. TEST_DECL(test_wolfSSL_private_keys),
  65817. TEST_DECL(test_wolfSSL_PEM_def_callback),
  65818. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  65819. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  65820. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  65821. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  65822. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  65823. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  65824. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  65825. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  65826. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  65827. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  65828. #ifndef NO_BIO
  65829. TEST_DECL(test_wolfSSL_BIO),
  65830. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  65831. TEST_DECL(test_wolfSSL_PEM_read_bio),
  65832. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  65833. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  65834. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  65835. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  65836. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  65837. #endif
  65838. /* EVP API testing */
  65839. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  65840. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  65841. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  65842. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  65843. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  65844. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  65845. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  65846. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  65847. TEST_DECL(test_wolfSSL_EVP_shake128),
  65848. TEST_DECL(test_wolfSSL_EVP_shake256),
  65849. TEST_DECL(test_wolfSSL_EVP_sm3),
  65850. TEST_DECL(test_EVP_blake2),
  65851. #ifdef OPENSSL_ALL
  65852. TEST_DECL(test_wolfSSL_EVP_md4),
  65853. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  65854. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  65855. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  65856. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  65857. #endif
  65858. TEST_DECL(test_EVP_MD_do_all),
  65859. TEST_DECL(test_wolfSSL_EVP_MD_size),
  65860. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  65861. TEST_DECL(test_wolfSSL_EVP_Digest),
  65862. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  65863. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  65864. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  65865. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  65866. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  65867. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  65868. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  65869. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  65870. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  65871. TEST_DECL(test_wolfssl_EVP_chacha20),
  65872. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  65873. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  65874. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  65875. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  65876. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  65877. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  65878. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  65879. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  65880. #ifdef OPENSSL_ALL
  65881. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  65882. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  65883. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  65884. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  65885. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  65886. TEST_DECL(test_wolfSSL_EVP_rc4),
  65887. TEST_DECL(test_wolfSSL_EVP_enc_null),
  65888. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  65889. TEST_DECL(test_wolfSSL_EVP_mdc2),
  65890. TEST_DECL(test_evp_cipher_aes_gcm),
  65891. #endif
  65892. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  65893. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  65894. #ifdef OPENSSL_EXTRA
  65895. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  65896. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  65897. #endif
  65898. #ifdef OPENSSL_ALL
  65899. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  65900. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  65901. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  65902. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  65903. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  65904. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  65905. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  65906. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  65907. #endif
  65908. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  65909. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  65910. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  65911. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  65912. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  65913. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  65914. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  65915. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  65916. #ifndef NO_BIO
  65917. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  65918. #endif
  65919. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  65920. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  65921. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  65922. #ifndef NO_BIO
  65923. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  65924. #endif /* !NO_BIO */
  65925. #endif
  65926. #ifdef OPENSSL_ALL
  65927. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  65928. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  65929. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  65930. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  65931. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  65932. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  65933. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  65934. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  65935. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  65936. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  65937. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  65938. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  65939. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  65940. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  65941. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  65942. #endif
  65943. TEST_DECL(test_EVP_PKEY_rsa),
  65944. TEST_DECL(test_EVP_PKEY_ec),
  65945. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  65946. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  65947. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  65948. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  65949. TEST_DECL(test_EVP_PKEY_cmp),
  65950. #ifdef OPENSSL_ALL
  65951. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  65952. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  65953. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  65954. #endif
  65955. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  65956. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  65957. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  65958. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  65959. #endif
  65960. #ifndef NO_BIO
  65961. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  65962. TEST_DECL(test_wolfSSL_GetLoggingCb),
  65963. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  65964. TEST_DECL(test_wc_ERR_remove_state),
  65965. TEST_DECL(test_wc_ERR_print_errors_fp),
  65966. #endif
  65967. TEST_DECL(test_wolfSSL_configure_args),
  65968. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  65969. TEST_DECL(test_wolfSSL_set1_curves_list),
  65970. TEST_DECL(test_wolfSSL_curves_mismatch),
  65971. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  65972. TEST_DECL(test_wolfSSL_OtherName),
  65973. TEST_DECL(test_wolfSSL_FPKI),
  65974. TEST_DECL(test_wolfSSL_URI),
  65975. TEST_DECL(test_wolfSSL_TBS),
  65976. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  65977. TEST_DECL(test_X509_STORE_untrusted),
  65978. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  65979. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  65980. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  65981. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  65982. TEST_DECL(test_wolfSSL_X509_Name_canon),
  65983. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  65984. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  65985. TEST_DECL(test_wolfSSL_X509_NID),
  65986. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  65987. TEST_DECL(test_wolfSSL_get0_param),
  65988. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  65989. TEST_DECL(test_wolfSSL_set1_host),
  65990. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  65991. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  65992. TEST_DECL(test_wolfSSL_X509_STORE),
  65993. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  65994. TEST_DECL(test_X509_STORE_get0_objects),
  65995. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  65996. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  65997. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  65998. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  65999. TEST_DECL(test_wolfSSL_X509_cmp_time),
  66000. TEST_DECL(test_wolfSSL_X509_time_adj),
  66001. /* X509 tests */
  66002. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  66003. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  66004. TEST_DECL(test_wolfSSL_X509_check_host),
  66005. TEST_DECL(test_wolfSSL_X509_check_email),
  66006. TEST_DECL(test_wolfSSL_X509_check_private_key),
  66007. TEST_DECL(test_wolfSSL_X509),
  66008. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  66009. TEST_DECL(test_wolfSSL_X509_sign),
  66010. TEST_DECL(test_wolfSSL_X509_sign2),
  66011. TEST_DECL(test_wolfSSL_X509_verify),
  66012. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  66013. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  66014. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  66015. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  66016. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  66017. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  66018. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  66019. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  66020. TEST_DECL(test_wolfSSL_X509_NAME),
  66021. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  66022. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  66023. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  66024. TEST_DECL(test_wolfSSL_X509_set_name),
  66025. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  66026. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  66027. TEST_DECL(test_wolfSSL_X509_set_version),
  66028. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  66029. TEST_DECL(test_wolfSSL_X509_CRL),
  66030. TEST_DECL(test_wolfSSL_i2d_X509),
  66031. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  66032. TEST_DECL(test_wolfSSL_PEM_read_X509),
  66033. TEST_DECL(test_wolfSSL_X509_check_ca),
  66034. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  66035. TEST_DECL(test_wolfSSL_X509_bad_altname),
  66036. TEST_DECL(test_wolfSSL_X509_name_match),
  66037. TEST_DECL(test_wolfSSL_X509_name_match2),
  66038. TEST_DECL(test_wolfSSL_X509_name_match3),
  66039. TEST_DECL(test_wolfSSL_X509_max_altnames),
  66040. TEST_DECL(test_wolfSSL_X509_max_name_constraints),
  66041. TEST_DECL(test_wolfSSL_make_cert),
  66042. #ifndef NO_BIO
  66043. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  66044. TEST_DECL(test_wolfSSL_X509_INFO),
  66045. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  66046. #endif
  66047. #ifdef OPENSSL_ALL
  66048. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  66049. #endif
  66050. TEST_DECL(test_wolfSSL_X509_CA_num),
  66051. TEST_DECL(test_wolfSSL_X509_get_version),
  66052. #ifndef NO_BIO
  66053. TEST_DECL(test_wolfSSL_X509_print),
  66054. TEST_DECL(test_wolfSSL_X509_CRL_print),
  66055. #endif
  66056. TEST_DECL(test_X509_get_signature_nid),
  66057. /* X509 extension testing. */
  66058. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  66059. TEST_DECL(test_wolfSSL_X509_get_ext),
  66060. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  66061. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  66062. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  66063. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  66064. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  66065. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  66066. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  66067. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  66068. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  66069. TEST_DECL(test_wolfSSL_X509V3_EXT),
  66070. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  66071. TEST_DECL(test_wolfSSL_X509_cmp),
  66072. TEST_DECL(test_GENERAL_NAME_set0_othername),
  66073. TEST_DECL(test_othername_and_SID_ext),
  66074. TEST_DECL(test_wolfSSL_dup_CA_list),
  66075. /* OpenSSL sk_X509 API test */
  66076. TEST_DECL(test_sk_X509),
  66077. /* OpenSSL sk_X509_CRL API test */
  66078. TEST_DECL(test_sk_X509_CRL),
  66079. /* OpenSSL X509 REQ API test */
  66080. TEST_DECL(test_X509_REQ),
  66081. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  66082. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  66083. TEST_DECL(test_X509_LOOKUP_add_dir),
  66084. /* RAND compatibility API */
  66085. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  66086. TEST_DECL(test_wolfSSL_RAND_bytes),
  66087. TEST_DECL(test_wolfSSL_RAND),
  66088. /* BN compatibility API */
  66089. TEST_DECL(test_wolfSSL_BN_CTX),
  66090. TEST_DECL(test_wolfSSL_BN),
  66091. TEST_DECL(test_wolfSSL_BN_init),
  66092. TEST_DECL(test_wolfSSL_BN_enc_dec),
  66093. TEST_DECL(test_wolfSSL_BN_word),
  66094. TEST_DECL(test_wolfSSL_BN_bits),
  66095. TEST_DECL(test_wolfSSL_BN_shift),
  66096. TEST_DECL(test_wolfSSL_BN_math),
  66097. TEST_DECL(test_wolfSSL_BN_math_mod),
  66098. TEST_DECL(test_wolfSSL_BN_math_other),
  66099. TEST_DECL(test_wolfSSL_BN_rand),
  66100. TEST_DECL(test_wolfSSL_BN_prime),
  66101. /* OpenSSL PKCS5 API test */
  66102. TEST_DECL(test_wolfSSL_PKCS5),
  66103. /* OpenSSL PKCS8 API test */
  66104. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  66105. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  66106. /* OpenSSL PKCS7 API test */
  66107. TEST_DECL(test_wolfssl_PKCS7),
  66108. TEST_DECL(test_wolfSSL_PKCS7_certs),
  66109. TEST_DECL(test_wolfSSL_PKCS7_sign),
  66110. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  66111. #ifndef NO_BIO
  66112. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  66113. #ifdef HAVE_SMIME
  66114. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  66115. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  66116. #endif /* HAVE_SMIME */
  66117. #endif /* !NO_BIO */
  66118. /* OpenSSL PKCS12 API test */
  66119. TEST_DECL(test_wolfSSL_PKCS12),
  66120. /* Can't memory test as callbacks use Assert. */
  66121. TEST_DECL(test_error_queue_per_thread),
  66122. TEST_DECL(test_wolfSSL_ERR_put_error),
  66123. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  66124. #ifndef NO_BIO
  66125. TEST_DECL(test_wolfSSL_ERR_print_errors),
  66126. #endif
  66127. TEST_DECL(test_OBJ_NAME_do_all),
  66128. TEST_DECL(test_wolfSSL_OBJ),
  66129. TEST_DECL(test_wolfSSL_OBJ_cmp),
  66130. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  66131. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  66132. #ifdef OPENSSL_ALL
  66133. TEST_DECL(test_wolfSSL_OBJ_ln),
  66134. TEST_DECL(test_wolfSSL_OBJ_sn),
  66135. #endif
  66136. #ifndef NO_BIO
  66137. TEST_DECL(test_wolfSSL_BIO_gets),
  66138. TEST_DECL(test_wolfSSL_BIO_puts),
  66139. TEST_DECL(test_wolfSSL_BIO_dump),
  66140. /* Can't memory test as server hangs. */
  66141. TEST_DECL(test_wolfSSL_BIO_should_retry),
  66142. TEST_DECL(test_wolfSSL_BIO_write),
  66143. TEST_DECL(test_wolfSSL_BIO_printf),
  66144. TEST_DECL(test_wolfSSL_BIO_f_md),
  66145. TEST_DECL(test_wolfSSL_BIO_up_ref),
  66146. TEST_DECL(test_wolfSSL_BIO_reset),
  66147. TEST_DECL(test_wolfSSL_BIO_get_len),
  66148. #endif
  66149. TEST_DECL(test_wolfSSL_check_domain),
  66150. TEST_DECL(test_wolfSSL_cert_cb),
  66151. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  66152. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  66153. TEST_DECL(test_wolfSSL_sigalg_info),
  66154. /* Can't memory test as tcp_connect aborts. */
  66155. TEST_DECL(test_wolfSSL_SESSION),
  66156. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  66157. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  66158. TEST_DECL(test_wolfSSL_ticket_keys),
  66159. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  66160. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  66161. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  66162. TEST_DECL(test_wolfSSL_verify_mode),
  66163. TEST_DECL(test_wolfSSL_verify_depth),
  66164. TEST_DECL(test_wolfSSL_verify_result),
  66165. TEST_DECL(test_wolfSSL_msg_callback),
  66166. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  66167. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  66168. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  66169. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  66170. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  66171. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  66172. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  66173. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  66174. TEST_DECL(test_wolfSSL_PEM_read),
  66175. TEST_DECL(test_wolfSSL_OpenSSL_version),
  66176. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  66177. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  66178. TEST_DECL(test_CONF_modules_xxx),
  66179. #ifdef OPENSSL_ALL
  66180. TEST_DECL(test_wolfSSL_TXT_DB),
  66181. TEST_DECL(test_wolfSSL_NCONF),
  66182. #endif
  66183. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  66184. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  66185. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  66186. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  66187. TEST_DECL(test_CRYPTO_THREADID_xxx),
  66188. TEST_DECL(test_ENGINE_cleanup),
  66189. /* test the no op functions for compatibility */
  66190. TEST_DECL(test_no_op_functions),
  66191. /* OpenSSL error API tests */
  66192. TEST_DECL(test_ERR_load_crypto_strings),
  66193. #ifdef OPENSSL_ALL
  66194. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  66195. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  66196. TEST_DECL(test_wolfSSL_CTX_ctrl),
  66197. #endif /* OPENSSL_ALL */
  66198. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  66199. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  66200. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  66201. /*********************************
  66202. * Crypto API tests
  66203. *********************************/
  66204. TEST_DECL(test_wolfSSL_MD4),
  66205. TEST_DECL(test_wolfSSL_MD5),
  66206. TEST_DECL(test_wolfSSL_MD5_Transform),
  66207. TEST_DECL(test_wolfSSL_SHA),
  66208. TEST_DECL(test_wolfSSL_SHA_Transform),
  66209. TEST_DECL(test_wolfSSL_SHA224),
  66210. TEST_DECL(test_wolfSSL_SHA256),
  66211. TEST_DECL(test_wolfSSL_SHA256_Transform),
  66212. TEST_DECL(test_wolfSSL_SHA512_Transform),
  66213. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  66214. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  66215. TEST_DECL(test_wolfSSL_HMAC_CTX),
  66216. TEST_DECL(test_wolfSSL_HMAC),
  66217. TEST_DECL(test_wolfSSL_CMAC),
  66218. TEST_DECL(test_wolfSSL_DES),
  66219. TEST_DECL(test_wolfSSL_DES_ncbc),
  66220. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  66221. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  66222. TEST_DECL(test_wolfSSL_AES_encrypt),
  66223. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  66224. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  66225. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  66226. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  66227. TEST_DECL(test_wolfSSL_RC4),
  66228. TEST_DECL(test_wolfSSL_RSA),
  66229. TEST_DECL(test_wolfSSL_RSA_DER),
  66230. TEST_DECL(test_wolfSSL_RSA_print),
  66231. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  66232. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  66233. TEST_DECL(test_wolfSSL_RSA_get0_key),
  66234. TEST_DECL(test_wolfSSL_RSA_meth),
  66235. TEST_DECL(test_wolfSSL_RSA_verify),
  66236. TEST_DECL(test_wolfSSL_RSA_sign),
  66237. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  66238. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  66239. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  66240. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  66241. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  66242. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  66243. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  66244. TEST_DECL(test_wolfSSL_RSA_ex_data),
  66245. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  66246. TEST_DECL(test_wolfSSL_RSA_To_Der),
  66247. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  66248. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  66249. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  66250. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  66251. TEST_DECL(test_wolfSSL_DH),
  66252. TEST_DECL(test_wolfSSL_DH_dup),
  66253. TEST_DECL(test_wolfSSL_DH_check),
  66254. TEST_DECL(test_wolfSSL_DH_prime),
  66255. TEST_DECL(test_wolfSSL_DH_1536_prime),
  66256. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  66257. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  66258. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  66259. TEST_DECL(test_wolfSSL_d2i_DHparams),
  66260. TEST_DECL(test_wolfSSL_DH_LoadDer),
  66261. TEST_DECL(test_wolfSSL_i2d_DHparams),
  66262. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  66263. TEST_DECL(test_wolfSSL_EC_GROUP),
  66264. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  66265. TEST_DECL(test_wolfSSL_EC_POINT),
  66266. TEST_DECL(test_wolfSSL_SPAKE),
  66267. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  66268. TEST_DECL(test_EC_i2d),
  66269. TEST_DECL(test_wolfSSL_EC_curve),
  66270. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  66271. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  66272. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  66273. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  66274. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  66275. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  66276. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  66277. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  66278. TEST_DECL(test_ECDSA_size_sign),
  66279. TEST_DECL(test_ECDH_compute_key),
  66280. #endif
  66281. #ifdef OPENSSL_EXTRA
  66282. TEST_DECL(test_EC25519),
  66283. TEST_DECL(test_ED25519),
  66284. TEST_DECL(test_EC448),
  66285. TEST_DECL(test_ED448),
  66286. #endif
  66287. TEST_DECL(test_DSA_do_sign_verify),
  66288. #ifdef OPENSSL_ALL
  66289. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  66290. TEST_DECL(test_wolfSSL_DSA_SIG),
  66291. #endif
  66292. TEST_DECL(test_openssl_generate_key_and_cert),
  66293. TEST_DECL(test_wolfSSL_FIPS_mode),
  66294. TEST_DECL(test_openssl_FIPS_drbg),
  66295. /*********************************
  66296. * CertManager API tests
  66297. *********************************/
  66298. TEST_DECL(test_wolfSSL_CertManagerAPI),
  66299. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  66300. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  66301. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  66302. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  66303. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  66304. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  66305. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  66306. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  66307. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  66308. TEST_DECL(test_wolfSSL_CertManagerCRL),
  66309. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  66310. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  66311. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  66312. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  66313. !defined(WOLFSSL_NO_CLIENT_AUTH))
  66314. TEST_DECL(test_various_pathlen_chains),
  66315. #endif
  66316. /*********************************
  66317. * SSL/TLS API tests
  66318. *********************************/
  66319. TEST_DECL(test_wolfSSL_Method_Allocators),
  66320. #ifndef NO_WOLFSSL_SERVER
  66321. TEST_DECL(test_wolfSSL_CTX_new),
  66322. #endif
  66323. TEST_DECL(test_server_wolfSSL_new),
  66324. TEST_DECL(test_client_wolfSSL_new),
  66325. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  66326. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  66327. TEST_DECL(test_for_double_Free),
  66328. #endif
  66329. TEST_DECL(test_wolfSSL_set_options),
  66330. #ifdef WOLFSSL_TLS13
  66331. /* TLS v1.3 API tests */
  66332. TEST_DECL(test_tls13_apis),
  66333. TEST_DECL(test_tls13_cipher_suites),
  66334. #endif
  66335. TEST_DECL(test_wolfSSL_tmp_dh),
  66336. TEST_DECL(test_wolfSSL_ctrl),
  66337. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  66338. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  66339. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  66340. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  66341. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  66342. #endif
  66343. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  66344. TEST_DECL(test_wolfSSL_security_level),
  66345. TEST_DECL(test_wolfSSL_SSL_in_init),
  66346. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  66347. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  66348. TEST_DECL(test_CONF_CTX_FILE),
  66349. TEST_DECL(test_CONF_CTX_CMDLINE),
  66350. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  66351. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  66352. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  66353. /* Bad certificate signature tests */
  66354. TEST_DECL(test_EccSigFailure_cm),
  66355. TEST_DECL(test_RsaSigFailure_cm),
  66356. #endif /* NO_CERTS */
  66357. /* PKCS8 testing */
  66358. TEST_DECL(test_wolfSSL_no_password_cb),
  66359. TEST_DECL(test_wolfSSL_PKCS8),
  66360. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  66361. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  66362. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  66363. TEST_DECL(test_wolfSSL_get_finished),
  66364. /* Uses Assert in handshake callback. */
  66365. TEST_DECL(test_wolfSSL_CTX_add_session),
  66366. /* Large number of memory allocations. */
  66367. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  66368. /* Large number of memory allocations. */
  66369. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  66370. /* Large number of memory allocations. */
  66371. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  66372. /* Large number of memory allocations. */
  66373. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  66374. /* Large number of memory allocations. */
  66375. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  66376. /* Large number of memory allocations. */
  66377. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  66378. #endif
  66379. TEST_DECL(test_SSL_CIPHER_get_xxx),
  66380. TEST_DECL(test_wolfSSL_ERR_strings),
  66381. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  66382. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  66383. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  66384. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  66385. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  66386. /* Large number of memory allocations. */
  66387. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  66388. TEST_DECL(test_wolfSSL_CertRsaPss),
  66389. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  66390. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  66391. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  66392. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  66393. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  66394. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  66395. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  66396. TEST_DECL(test_multiple_crls_same_issuer),
  66397. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  66398. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  66399. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  66400. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  66401. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  66402. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  66403. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  66404. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  66405. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  66406. TEST_DECL(test_SetTmpEC_DHE_Sz),
  66407. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  66408. #ifdef WOLFSSL_DTLS
  66409. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  66410. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  66411. #endif
  66412. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  66413. /* Uses Assert in handshake callback. */
  66414. TEST_DECL(test_wolfSSL_dtls_plaintext),
  66415. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  66416. defined(HAVE_IO_TESTS_DEPENDENCIES)
  66417. TEST_DECL(test_wolfSSL_read_write),
  66418. /* Can't memory test as server hangs if client fails before second connect.
  66419. */
  66420. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  66421. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  66422. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  66423. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  66424. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  66425. /* Can't memory test as server hangs. */
  66426. TEST_DECL(test_wolfSSL_dtls_export),
  66427. /* Uses Assert in handshake callback. */
  66428. TEST_DECL(test_wolfSSL_tls_export),
  66429. #endif
  66430. TEST_DECL(test_wolfSSL_dtls_export_peers),
  66431. TEST_DECL(test_wolfSSL_SetMinVersion),
  66432. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  66433. /* wolfSSL handshake APIs. */
  66434. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  66435. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  66436. TEST_DECL(test_wolfSSL_BUF),
  66437. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  66438. /* Can't memory test as server hangs. */
  66439. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  66440. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  66441. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  66442. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  66443. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  66444. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  66445. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  66446. /* Can't memory test as server hangs. */
  66447. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  66448. TEST_DECL(test_wolfSSL_Tls13_postauth),
  66449. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  66450. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  66451. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  66452. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  66453. TEST_DECL(test_wolfSSL_THREADID_hash),
  66454. /* TLS extensions tests */
  66455. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  66456. #ifdef HAVE_SNI
  66457. TEST_DECL(test_wolfSSL_UseSNI_params),
  66458. /* Uses Assert in handshake callback. */
  66459. TEST_DECL(test_wolfSSL_UseSNI_connection),
  66460. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  66461. #endif /* HAVE_SNI */
  66462. #endif
  66463. TEST_DECL(test_wolfSSL_UseTrustedCA),
  66464. TEST_DECL(test_wolfSSL_UseMaxFragment),
  66465. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  66466. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  66467. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  66468. /* Uses Assert in handshake callback. */
  66469. TEST_DECL(test_wolfSSL_UseALPN_connection),
  66470. TEST_DECL(test_wolfSSL_UseALPN_params),
  66471. #endif
  66472. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  66473. /* Uses Assert in handshake callback. */
  66474. TEST_DECL(test_wolfSSL_set_alpn_protos),
  66475. #endif
  66476. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  66477. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  66478. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  66479. TEST_DECL(test_tls_ext_duplicate),
  66480. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  66481. defined(HAVE_IO_TESTS_DEPENDENCIES)
  66482. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  66483. /* Uses Assert in handshake callback. */
  66484. TEST_DECL(test_wolfSSL_Tls13_ECH),
  66485. #endif
  66486. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  66487. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  66488. /* OCSP Stapling */
  66489. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  66490. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  66491. TEST_DECL(test_self_signed_stapling),
  66492. /* Multicast */
  66493. TEST_DECL(test_wolfSSL_mcast),
  66494. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  66495. TEST_DECL(test_wolfSSL_msgCb),
  66496. TEST_DECL(test_wolfSSL_either_side),
  66497. TEST_DECL(test_wolfSSL_DTLS_either_side),
  66498. /* Uses Assert in handshake callback. */
  66499. TEST_DECL(test_wolfSSL_dtls_fragments),
  66500. /* Uses Assert in handshake callback. */
  66501. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  66502. /* Uses Assert in handshake callback. */
  66503. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  66504. /* Uses Assert in handshake callback. */
  66505. TEST_DECL(test_wolfSSL_dtls_bad_record),
  66506. /* Uses Assert in handshake callback. */
  66507. TEST_DECL(test_wolfSSL_dtls_stateless),
  66508. TEST_DECL(test_generate_cookie),
  66509. #ifndef NO_BIO
  66510. /* Can't memory test as server hangs. */
  66511. TEST_DECL(test_wolfSSL_BIO_connect),
  66512. /* Can't memory test as server Asserts in thread. */
  66513. TEST_DECL(test_wolfSSL_BIO_accept),
  66514. TEST_DECL(test_wolfSSL_BIO_tls),
  66515. #endif
  66516. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  66517. TEST_DECL(test_DhCallbacks),
  66518. #endif
  66519. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  66520. TEST_DECL(test_export_keying_material),
  66521. #endif
  66522. /* Can't memory test as client/server Asserts in thread. */
  66523. TEST_DECL(test_ticket_and_psk_mixing),
  66524. /* Can't memory test as client/server Asserts in thread. */
  66525. TEST_DECL(test_prioritize_psk),
  66526. /* Can't memory test as client/server hangs. */
  66527. TEST_DECL(test_wc_CryptoCb),
  66528. /* Can't memory test as client/server hangs. */
  66529. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  66530. #if !defined(NO_FILESYSTEM) && \
  66531. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  66532. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  66533. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  66534. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  66535. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  66536. #ifdef HAVE_MAX_FRAGMENT
  66537. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  66538. #endif /* HAVE_MAX_FRAGMENT */
  66539. #ifndef NO_RSA
  66540. TEST_DECL(test_wolfSSL_dtls_stateless2),
  66541. #if !defined(NO_OLD_TLS)
  66542. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  66543. #endif /* !defined(NO_OLD_TLS) */
  66544. #endif /* ! NO_RSA */
  66545. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  66546. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  66547. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  66548. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  66549. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  66550. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  66551. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  66552. && defined(WOLFSSL_TLS13) && \
  66553. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  66554. TEST_DECL(test_ticket_nonce_malloc),
  66555. #endif
  66556. TEST_DECL(test_ticket_ret_create),
  66557. TEST_DECL(test_wrong_cs_downgrade),
  66558. TEST_DECL(test_extra_alerts_wrong_cs),
  66559. TEST_DECL(test_extra_alerts_skip_hs),
  66560. TEST_DECL(test_extra_alerts_bad_psk),
  66561. TEST_DECL(test_tls13_bad_psk_binder),
  66562. /* Can't memory test as client/server Asserts. */
  66563. TEST_DECL(test_harden_no_secure_renegotiation),
  66564. TEST_DECL(test_override_alt_cert_chain),
  66565. TEST_DECL(test_rpk_set_xxx_cert_type),
  66566. TEST_DECL(test_tls13_rpk_handshake),
  66567. TEST_DECL(test_dtls13_bad_epoch_ch),
  66568. TEST_DECL(test_short_session_id),
  66569. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  66570. /* Can't memory test as client/server hangs. */
  66571. TEST_DECL(test_dtls_msg_from_other_peer),
  66572. TEST_DECL(test_dtls_ipv6_check),
  66573. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  66574. TEST_DECL(test_dtls_no_extensions),
  66575. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  66576. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  66577. TEST_DECL(test_session_ticket_no_id),
  66578. TEST_DECL(test_session_ticket_hs_update),
  66579. TEST_DECL(test_dtls_downgrade_scr_server),
  66580. TEST_DECL(test_dtls_downgrade_scr),
  66581. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  66582. TEST_DECL(test_dtls_client_hello_timeout),
  66583. TEST_DECL(test_dtls_dropped_ccs),
  66584. TEST_DECL(test_dtls_seq_num_downgrade),
  66585. TEST_DECL(test_certreq_sighash_algos),
  66586. TEST_DECL(test_revoked_loaded_int_cert),
  66587. TEST_DECL(test_dtls_frag_ch),
  66588. TEST_DECL(test_dtls13_frag_ch_pq),
  66589. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  66590. TEST_DECL(test_dtls_old_seq_number),
  66591. TEST_DECL(test_tls13_pq_groups),
  66592. TEST_DECL(test_tls13_early_data),
  66593. TEST_DECL(test_tls_multi_handshakes_one_record),
  66594. TEST_DECL(test_write_dup),
  66595. TEST_DECL(test_read_write_hs),
  66596. TEST_DECL(test_get_signature_nid),
  66597. TEST_DECL(test_tls_cert_store_unchanged),
  66598. TEST_DECL(test_wolfSSL_SendUserCanceled),
  66599. /* This test needs to stay at the end to clean up any caches allocated. */
  66600. TEST_DECL(test_wolfSSL_Cleanup)
  66601. };
  66602. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  66603. static void TestSetup(void)
  66604. {
  66605. /* Stub, for now. Add common test setup code here. */
  66606. }
  66607. static void TestCleanup(void)
  66608. {
  66609. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  66610. /* Clear any errors added to the error queue during the test run. */
  66611. wolfSSL_ERR_clear_error();
  66612. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  66613. }
  66614. /* Print out all API test cases with numeric identifier.
  66615. */
  66616. void ApiTest_PrintTestCases(void)
  66617. {
  66618. int i;
  66619. printf("All Test Cases:\n");
  66620. for (i = 0; i < TEST_CASE_CNT; i++) {
  66621. printf("%3d: %s\n", i + 1, testCases[i].name);
  66622. }
  66623. }
  66624. /* Add test case with index to the list to run.
  66625. *
  66626. * @param [in] idx Index of test case to run starting at 1.
  66627. * @return 0 on success.
  66628. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  66629. */
  66630. int ApiTest_RunIdx(int idx)
  66631. {
  66632. if (idx < 1 || idx > TEST_CASE_CNT) {
  66633. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  66634. return BAD_FUNC_ARG;
  66635. }
  66636. testAll = 0;
  66637. testCases[idx-1].run = 1;
  66638. return 0;
  66639. }
  66640. /* Add test case with name to the list to run.
  66641. *
  66642. * @param [in] name Name of test case to run.
  66643. * @return 0 on success.
  66644. * @return BAD_FUNC_ARG when name is not a known test case name.
  66645. */
  66646. int ApiTest_RunName(char* name)
  66647. {
  66648. int i;
  66649. for (i = 0; i < TEST_CASE_CNT; i++) {
  66650. if (XSTRCMP(testCases[i].name, name) == 0) {
  66651. testAll = 0;
  66652. testCases[i].run = 1;
  66653. return 0;
  66654. }
  66655. }
  66656. printf("Test case name not found: %s\n", name);
  66657. printf("Use --list to see all test case names.\n");
  66658. return BAD_FUNC_ARG;
  66659. }
  66660. /* Converts the result code to a string.
  66661. *
  66662. * @param [in] res Test result code.
  66663. * @return String describing test result.
  66664. */
  66665. static const char* apitest_res_string(int res)
  66666. {
  66667. const char* str = "invalid result";
  66668. switch (res) {
  66669. case TEST_SUCCESS:
  66670. str = "passed";
  66671. break;
  66672. case TEST_FAIL:
  66673. str = "failed";
  66674. break;
  66675. case TEST_SKIPPED:
  66676. str = "skipped";
  66677. break;
  66678. }
  66679. return str;
  66680. }
  66681. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66682. static double gettime_secs(void)
  66683. #if defined(_MSC_VER) && defined(_WIN32)
  66684. {
  66685. /* there's no gettimeofday for Windows, so we'll use system time */
  66686. #define EPOCH_DIFF 11644473600LL
  66687. FILETIME currentFileTime;
  66688. GetSystemTimePreciseAsFileTime(&currentFileTime);
  66689. ULARGE_INTEGER uli = { 0, 0 };
  66690. uli.LowPart = currentFileTime.dwLowDateTime;
  66691. uli.HighPart = currentFileTime.dwHighDateTime;
  66692. /* Convert to seconds since Unix epoch */
  66693. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  66694. }
  66695. #else
  66696. {
  66697. struct timeval tv;
  66698. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  66699. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  66700. }
  66701. #endif
  66702. #endif
  66703. int ApiTest(void)
  66704. {
  66705. int i;
  66706. int ret;
  66707. int res = 0;
  66708. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66709. double timeDiff;
  66710. #endif
  66711. printf(" Begin API Tests\n");
  66712. fflush(stdout);
  66713. /* we must perform init and cleanup if not all tests are running */
  66714. if (!testAll) {
  66715. #ifdef WOLFCRYPT_ONLY
  66716. if (wolfCrypt_Init() != 0) {
  66717. printf("wolfCrypt Initialization failed\n");
  66718. res = 1;
  66719. }
  66720. #else
  66721. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  66722. printf("wolfSSL Initialization failed\n");
  66723. res = 1;
  66724. }
  66725. #endif
  66726. }
  66727. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66728. if (res == 0) {
  66729. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  66730. printf("failed to create tmp dir\n");
  66731. res = 1;
  66732. }
  66733. else {
  66734. tmpDirNameSet = 1;
  66735. }
  66736. }
  66737. #endif
  66738. if (res == 0) {
  66739. for (i = 0; i < TEST_CASE_CNT; ++i) {
  66740. EXPECT_DECLS;
  66741. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66742. currentTestName = testCases[i].name;
  66743. #endif
  66744. /* When not testing all cases then skip if not marked for running.
  66745. */
  66746. if (!testAll && !testCases[i].run) {
  66747. continue;
  66748. }
  66749. TestSetup();
  66750. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  66751. fflush(stdout);
  66752. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66753. timeDiff = gettime_secs();
  66754. #endif
  66755. ret = testCases[i].func();
  66756. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66757. timeDiff = gettime_secs() - timeDiff;
  66758. #endif
  66759. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  66760. if (ret != TEST_SKIPPED) {
  66761. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  66762. }
  66763. else
  66764. #endif
  66765. {
  66766. printf(" %s\n", apitest_res_string(ret));
  66767. }
  66768. fflush(stdout);
  66769. /* if return code is < 0 and not skipped then assert error */
  66770. Expect((ret > 0 || ret == TEST_SKIPPED),
  66771. ("Test failed\n"),
  66772. ("ret %d", ret));
  66773. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  66774. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  66775. TestCleanup();
  66776. }
  66777. }
  66778. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  66779. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  66780. wc_ecc_fp_free(); /* free per thread cache */
  66781. #endif
  66782. if (!testAll) {
  66783. #ifdef WOLFCRYPT_ONLY
  66784. wolfCrypt_Cleanup();
  66785. #else
  66786. wolfSSL_Cleanup();
  66787. #endif
  66788. }
  66789. (void)testDevId;
  66790. if (res != 0) {
  66791. printf("\nFAILURES:\n");
  66792. for (i = 0; i < TEST_CASE_CNT; ++i) {
  66793. if (testCases[i].fail) {
  66794. printf(" %3d: %s\n", i + 1, testCases[i].name);
  66795. }
  66796. }
  66797. printf("\n");
  66798. fflush(stdout);
  66799. }
  66800. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  66801. if (tmpDirNameSet) {
  66802. printf("\nBinary dumps of the memio streams can be found in the\n"
  66803. "%s directory. This can be imported into\n"
  66804. "Wireshark by transforming the file with\n"
  66805. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  66806. "And then loading test_output.dump.hex into Wireshark using\n"
  66807. "the \"Import from Hex Dump...\" option and selecting the\n"
  66808. "TCP encapsulation option.\n", tmpDirName);
  66809. }
  66810. #endif
  66811. printf(" End API Tests\n");
  66812. fflush(stdout);
  66813. return res;
  66814. }